Project Euler:Problem 32 Pandigital products

时间:2023-01-11 18:26:56

We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly
once; for example, the 5-digit number, 15234, is 1 through 5 pandigital.

The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.

Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.

HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.

We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly
once; for example, the 5-digit number, 15234, is 1 through 5 pandigital.

The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.

Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.

HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.

#include <iostream>
#include <map>
using namespace std; bool pand(int a, int b, int c)
{
map<int, int>mp;
int tmp[] = { a, b, c };
int num = 1;
if (b != 0)
num = 3;
for (int i = 0; i < num; i++)
{
int p = tmp[i];
while (p)
{
if (mp[p % 10] != 0)
return false;
else
{
mp[p % 10] = 1;
p = p / 10;
}
}
}
if (mp[0] != 0)
return false;
if (b == 0) //说明a中无反复
return true;
int count = 0;
for (int i = 1; i <= 9; i++)
{
if (mp[i] != 0)
count++;
}
if (count == 9)
return true;
else
return false;
} int main()
{
map<int, int>mp;
for (int i = 1; i <= 9876; i++)
{
if (pand(i,0,0))
{
for (int j = 1; j < i; j++)
{
if (i*j <= 10000)
{
if (pand(i, j, i*j))
mp[i*j] = 1;
}
}
}
}
map<int, int>::iterator iter;
int res = 0;
for (iter = mp.begin(); iter != mp.end(); iter++)
{
if (mp[iter->first] == 1)
res += iter->first;
}
cout << res << endl;
system("pause");
return 0;
}

Project Euler:Problem 32 Pandigital products的更多相关文章

  1. Project Euler:Problem 41 Pandigital prime

    We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly o ...

  2. Project Euler:Problem 87 Prime power triples

    The smallest number expressible as the sum of a prime square, prime cube, and prime fourth power is ...

  3. Project Euler:Problem 55 Lychrel numbers

    If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindr ...

  4. Project Euler:Problem 63 Powerful digit counts

    The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, is ...

  5. Project Euler:Problem 86 Cuboid route

    A spider, S, sits in one corner of a cuboid room, measuring 6 by 5 by 3, and a fly, F, sits in the o ...

  6. Project Euler:Problem 76 Counting summations

    It is possible to write five as a sum in exactly six different ways: 4 + 1 3 + 2 3 + 1 + 1 2 + 2 + 1 ...

  7. Project Euler:Problem 89 Roman numerals

    For a number written in Roman numerals to be considered valid there are basic rules which must be fo ...

  8. Project Euler:Problem 93 Arithmetic expressions

    By using each of the digits from the set, {1, 2, 3, 4}, exactly once, and making use of the four ari ...

  9. Project Euler:Problem 58 Spiral primes

    Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length ...

随机推荐

  1. Oracle查询表里的重复数据方法:

    一.背景 一张person表,有id和name的两个字段,id是唯一的不允许重复,id相同则认为是重复的记录. 二.解决 select id from group by id having count ...

  2. 马航MH17事件将把普京逼入绝境&quest;

    据7月22日报道,马克兰东部民间武装22日凌晨将失事客机的"黑匣子"交给马来西亚方面.乌*与民间武装允许在坠机地点附的小范围停火. 与此同一时候,联合国安理会21日通过决议,敦促 ...

  3. Eclipse背景颜色改动

     Eclipse背景颜色改动:  操作界面默认颜色为白色. 对于我们长期使用电脑编程的人来说.白色非常刺激我们的眼睛,所以我常常会改变workspace的背景色.使眼睛舒服一些.设置方法例如以下: ...

  4. 技术笔记1:java&period;sql&period;SQLException&colon; Access denied for user &&num;39&semi;root&&num;39&semi;&commat;&&num;39&semi;localhost&&num;39&semi; &lpar;using password)

    在myEclipse10中运行java项目的时候,遇到java.sql.SQLException: Access denied for user 'root'@'localhost' (using p ...

  5. Python 矩阵与矩阵以及矩阵与向量的乘法

    import numpy as np numpy模块的array相乘时,有两种方式:一是矩阵形式,二是挨个相乘. 需要用矩阵形式相乘时,则要用np.dot()函数. #矩阵与矩阵相乘a = np.ar ...

  6. Python with yield语句

    1.with 语句 语法: with expression as variable 需要引入一个上下文管理协议,实现的方法是为一个类定义 __enter__() 和 __exit__() 方法, 在执 ...

  7. 【One Day菜鸟到大鸟】MyBatis搭建环境

    一.概述     MyBatis是一个持久化框架和Hiberante差不多.比起JDBC来说MyBatis封装了JDBC让我们能够面向对象开发.比起Hiberante来说卸下了Hiberante那种重 ...

  8. 联想ThinkPad S3-S440虚拟机安装,ubuntu安装,Hadoop&lpar;2&period;7&period;1&rpar;详解及WordCount运行,spark集群搭建

    下载ubuntu操作系统版本 ubuntu-14.10-desktop-amd64.iso(64位) 安装过程出现错误: This kernel requires an X86-64 CPU,but ...

  9. js 值和类型

    js中变量是没有类型的,只有值才有类型. 变量随时可以持有任何类型的值. <!DOCTYPE html> <html lang="zh"> <head ...

  10. 关于ECDSA&sol;ECC&lpar;密钥加密传输&rpar;和ECDSA&sol;ECDH&lpar;密钥磋商&rpar;

    关于ECDSA/ECC(密钥加密传输)和ECDSA/ECDH(密钥磋商) 来源: https://blog.csdn.net/xueyepiaoling/article/details/6243337 ...