Project Euler:Problem 32 Pandigital products
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.
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.
#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的更多相关文章
- 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 ...
- 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 ...
- Project Euler:Problem 55 Lychrel numbers
If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindr ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Project Euler:Problem 58 Spiral primes
Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length ...
随机推荐
- 【AIX】查看当前目录下文件与文件夹大小
使用命令: du –sg ./* #以G为单位 du –sm ./* #以M为单位 du –sk ./* #以k为单位
- 【DB2】SQL优化
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAA
- V-rep学习笔记:机器人模型创建2—添加关节
下面接着之前经过简化并调整好视觉效果的模型继续工作流,为了使模型能受控制运动起来必须在合适的位置上添加相应的运动副/关节.一般情况下我们可以查阅手册或根据设计图纸获得这些关节的准确位置和姿态,知道这些 ...
- ubuntu普通账户获取root权限的方法以及su和su -的区别
打开命令窗口 输入命令:sudo passwd root 然后按照上图中的操作进行即可. su和su -的命令的不同: su命令和su -命令最大的本质区别就是:前者只是切换了root身份,但Shel ...
- Docker Swarm Mode无法增加管理节点
这两天用Docker Swarm Mode,加入新的管理节点会报以下错误(在/var/log/messages文件中可以看到): Handler for POST /v1.37/swarm/join ...
- Office2013中文激活版
国内的WPS专业版也是很不错的,习惯的Office.office2013很不错的办公利器 00.PPT 01.Word 02.Excel Download: 链接: https://pan.baidu ...
- 【laravel5.*】添加ide_helper.php 助手
1.参照文档:https://github.com/barryvdh/laravel-ide-helper#automatic-phpdoc-generation-for-laravel-facade ...
- HttpClient库设置超时
HttpClient库API跟Lucene一样,每个版本的API都变化很大,这有点让人头疼.就好比创建一个HttpClient对象吧,每一个版本的都不一样. 3.X是正常的Java语法 HttpCli ...
- IPsec ISAKMP(转)
IPsec ISAKMP 2010-08-10 11:47:01 标签:IPsec 职场 休闲 ISAKMP Interne 安全连接和密钥管理协议(ISAKMP)是 IPsec 体系结构中的一种主要 ...
- Android--------从一个包中的Avtivity创建另外另外一个包的Context
Android中有Context的概念,想必大家都知道.Context可以做很多事情,打开activity.发送广播.打开本包下文件夹和数据库.获取classLoader.获取资源等等.如果我们得到了 ...