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 ...
随机推荐
- Xamarin.Android之定位
一.前言 打开我们手中的应用,可以发现越来越多的应用使用了定位,从而使我们的生活更加方便,所以本章我们将学习如何在Xamarin中进行定位的开发. 二.准备工作 因为我们的虚拟机是运行在电脑本地的,自 ...
- jQuery 绑定事件及移除绑定事件方法和元素事件列表
1.jQuery Event 事件: ready(fn); $(document).ready()注意在body中没有onload事件,否则该函数不能执行.在每个页面中可以有很多个函数被加载 ...
- alias别名使用
rhel系列的别名使用,方便操作! 功能说明:设置指令的别名.语 法:alias [别名] = [指令名称]参 数 :若不加任何参数,则列出目前所有的别名设置.举 例 :ermao@lo ...
- Python-Flask实现基金自选网站
代码地址如下:http://www.demodashi.com/demo/14734.html 项目介绍 本项目的基金数据来自天天基金网,但该网站用户体验较差,内容冗余,故自己实现一个轻量级网站,从8 ...
- 网页屏蔽Backspace事件,输入框不屏蔽
document.onkeydown = function (e) { var code; if (!e){ var e = window.event;} if (e.keyCode){ code = ...
- 【转】【MySQL】MySQL的双机互信实战
[转]https://www.cnblogs.com/mchina/archive/2013/03/15/2956017.html MySQL双机实战原理:利用ssh传输文件,通过公.私钥的共享,实现 ...
- empty、isset、is_null的比较
直接上代码 <?php $a=0; $b='0'; $c=0.0; $d=''; $e=NULL; $f=array(); $g='\0'; $h=' ';//space $i=true; $j ...
- ios 7新特性
1:解决ios7.0中视图控制器中视图坐标布局问题 if ([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0) { self. ...
- linux创建用户名密码等操作
转自: https://www.linuxidc.com/Linux/2017-06/144916.htm 与大家分享下Linux系统中创建用户.设置密码.修改用户.删除用户的命令,希望对你有所帮助. ...
- 【LeetCode】213. House Robber II
House Robber II Note: This is an extension of House Robber. After robbing those houses on that stree ...