博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Project Euler Problem 32 Pandigital products
阅读量:6703 次
发布时间:2019-06-25

本文共 2828 字,大约阅读时间需要 9 分钟。

Problem 32

We shall say that an n-digit number is pandigital if it makes use of all the digits 1 ton 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.

C++:

#include 
#include
#include
using namespace std;int main(){ int digits[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; long left1, left2, mid1_4, mid2_4, right4; set
s; for(;;) { left1 = digits[0]; left2 = digits[1] + digits[0] * 10; mid1_4 = digits[1] * 1000 + digits[2] * 100 + digits[3] * 10 + digits[4]; mid2_4 = digits[2] * 100 + digits[3] * 10 + digits[4]; right4 = digits[5] * 1000 + digits[6] * 100 + digits[7] * 10 + digits[8]; if((left1 * mid1_4 == right4) || (left2 * mid2_4 == right4)) s.insert(right4); if(!next_permutation(digits, digits+9)) break; } long total = 0; for(set
::iterator iter=s.begin(); iter!=s.end(); iter++) total += *iter; cout << total << endl; return 0;}

C++:

#include 
#include
#include
using namespace std;const long N1 = 100;const long N2 = 2000;bool ispandigital(long a, long b, long product){ int digits[10], d; memset(digits, 0, sizeof(digits)); while(a) { d = a % 10; if(digits[d]) return false; digits[d] = 1; a /= 10; } if(digits[0]) return false; while(b) { d = b % 10; if(digits[d]) return false; digits[d] = 1; b /= 10; } if(digits[0]) return false; while(product) { d = product % 10; if(digits[d]) return false; digits[d] = 1; product /= 10; } if(digits[0]) return false; for(int i=1; i<10; i++) if(digits[i] == 0) return false; return true;}int main(){ long long total = 0; long product; set
s; for(long i=1; i<=N1; i++) for(long j=i+1; j<=N2; j++) { product = i * j; if(product > 1000 && ispandigital(i, j, product)) s.insert(product); } total = 0; for(set
::iterator iter=s.begin(); iter!=s.end(); iter++) total += *iter; cout << total << endl; return 0;}

转载于:https://www.cnblogs.com/tigerisland/p/7563994.html

你可能感兴趣的文章
《零基础 Java 开发 》全书目录
查看>>
阿里云发布ET工业大脑开放平台,交付只需6天!
查看>>
Centos7服务器运维服务系统安装过程中遇到的问题总结
查看>>
百度电话一面
查看>>
CentOS6.6源码编译升级GCC至4.8.2
查看>>
你所必须掌握的三种异步编程方法callbacks,listeners,promise
查看>>
树 - (二叉查找树,红黑树,B树)- 红黑树
查看>>
C++雾中风景1:友元类与面向对象
查看>>
SVN:Previous operation has not finished; run 'cleanup' if it was interrupted
查看>>
记录一次批量插入的优化历程
查看>>
金忠孝:新IT时代,人工智能在汽车行业中的应用
查看>>
世界头一个!美国建立无人机数据中心
查看>>
Django logging模板使用样例
查看>>
Python爬虫之40行代码爬取金庸所有武侠小说
查看>>
[开源] Gio.js -- 一个基于 Three.js 的 Web3D 地球数据可视化库
查看>>
从Java的前景与就业情况看,Java是你首选的编程语言,没有之一
查看>>
[Java 基础]运算符和表达式
查看>>
[20180224]理解exp direct导出操作.txt
查看>>
操作系统实验报告三
查看>>
响应式布局的常用解决方案对比(媒体查询、百分比、rem和vw/vh)
查看>>