Project Euler 32 Pandigital products
题意:找出所有形如 39 × 186 = 7254 这种,由 1 ~ 9,9个数字构成的等式的和,注意相同的积不计算两次
思路:如下面两种方法
方法一:暴力枚举间断点
/*************************************************************************
> File Name: euler032.cpp
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年05月24日 星期三 20时08分07秒
************************************************************************/
#include <stdio.h>
#include <inttypes.h>
#include <set>
#include <algorithm>
int32_t CalNum(int32_t s,int32_t e,int32_t* num){ // 计算[s,e]之间num值
int32_t sum = 0;
for(int32_t i = s ; i <= e ; i++) sum = sum * 10 + num[i];
return sum;
}
bool check(int32_t x1,int32_t x2,int32_t* num){ // 判断是否符合题意
int tmp1 = CalNum(0,x1,num) , tmp2 = CalNum(x1+1,x2,num) , tmp3 = CalNum(x2+1,8,num);
return tmp1 * tmp2 == tmp3;
}
void solve(){
int32_t num[9] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 };
std::set<int32_t> st; // 采用set去重
do{
for(int32_t i = 0 ; i < 6 ; i++){
for(int32_t j = i + 1 ; j < 8 ; j++){
if( check( i , j , num ) ){ // 暴力枚举两个间断点的位置
st.insert( CalNum( j + 1 , 8 , num ) );
}
}
}
}while( std::next_permutation( num , num + 9 ) );
int32_t sum = 0;
std::set<int32_t>::iterator it;
for( it = st.begin(); it != st.end() ; it++) sum += *it;
printf("%d\n",sum);
}
int main(){
solve();
return 0;
}
方法二:暴力枚举出 a 、b ,判断是否符合题意
/*************************************************************************
> File Name: euler032t2.c
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年06月24日 星期六 21时55分16秒
************************************************************************/
#include <stdio.h>
#include <math.h>
#include <inttypes.h>
#define MAX_N 10000000
int32_t canAdd[ MAX_N + 1 ] = {0};
int32_t HowManyDigs(int32_t i , int32_t j) {
int32_t digs = 0;
digs += (int32_t)log10( i ) + 1;
digs += (int32_t)log10( j ) + 1;
digs += (int32_t)log10( i * j ) + 1;
return digs;
}
bool AppearOnce(int32_t x , int32_t* num) {
while(x) {
if( ( x % 10 == 0 ) || ( num[ x % 10 - 1 ] ) ) return false;
num[ x % 10 - 1 ] = 1;
x /= 10;
}
return true;
}
bool IsDigital(int32_t a , int32_t b) {
int32_t num[9] = {0};
bool ok = true;
ok = ( ok && AppearOnce(a,num) );
ok = ( ok && AppearOnce(b,num) );
ok = ( ok && AppearOnce(a*b,num) );
return ok;
}
void solve() {
int32_t sum = 0;
for(int32_t i = 1 ; i < 100 ; i++) {
for(int32_t j = i + 1 ; ; j++) {
int32_t digs = HowManyDigs( i , j );
if( digs < 9 ) continue;
if( digs > 9 ) break;
if( IsDigital( i , j ) && !canAdd[i*j] ) {
sum += i * j;
canAdd[ i * j ] = 1;
}
}
}
printf("%d\n",sum);
}
int32_t main() {
solve();
return 0;
}
Project Euler 32 Pandigital products的更多相关文章
- 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 o ...
- Project Euler 104:Pandigital Fibonacci ends 两端为全数字的斐波那契数
Pandigital Fibonacci ends The Fibonacci sequence is defined by the recurrence relation: F[n] = F[n-1 ...
- Project Euler 41 Pandigital prime( 米勒测试 + 生成全排列 )
题意:如果一个n位数恰好使用了1至n每个数字各一次,我们就称其为全数字的.例如,2143就是一个4位全数字数,同时它恰好也是一个素数. 最大的全数字的素数是多少? 思路: 最大全排列素数可以从 n = ...
- Project Euler 38 Pandigital multiples
题意: 将192分别与1.2.3相乘: 192 × 1 = 192192 × 2 = 384192 × 3 = 576 连接这些乘积,我们得到一个1至9全数字的数192384576.我们称192384 ...
- Python练习题 039:Project Euler 011:网格中4个数字的最大乘积
本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...
- [project euler] program 4
上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...
- Python练习题 029:Project Euler 001:3和5的倍数
开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...
- Project Euler 9
题意:三个正整数a + b + c = 1000,a*a + b*b = c*c.求a*b*c. 解法:可以暴力枚举,但是也有数学方法. 首先,a,b,c中肯定有至少一个为偶数,否则和不可能为以上两个 ...
- Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.
In Problem 42 we dealt with triangular problems, in Problem 44 of Project Euler we deal with pentago ...
随机推荐
- 大红数星星 图论 XD网络赛
问题 A: 大红数星星 时间限制: 3 Sec 内存限制: 128 MB提交: 1066 解决: 67[提交][状态][讨论版] 题目描述 “三角形十分的美丽,相信大家小学就学过三角形具有稳定性, ...
- mongodb-主从复制
1 主从复制: 一个概念,在sqlserver或者说是mysql也有 2 主从复制解决了哪些问题??? 读写压力:以前是一个mongodb去承载海量的读和写,这样的话终有瓶颈的.使用一主多从, 从服务 ...
- [Javascript Crocks] Recover from a Nothing with the `coalesce` Method
The alt method allows us to recover from a nothing with a default Maybe, but sometimes our recovery ...
- AOJ 0121 Seven Puzzle {广度优先搜索}(*)
原题 题意 题意是有一个输入,比方: 1 0 2 3 4 5 6 7 摆成例如以下形状: 1 0 2 3 4 5 6 7 0表示空格.其它数字能够移动到0的位置.最后须要到例如以下形状: 0 1 2 ...
- 将Latex tex文档转换成 word文档(上)
有时候逼不得已,必须得将自己精心排版好的latex 文档 转换成word 给别人编辑 以下提供一个方法 下载 Tex2Word 工具,地址我的网盘 安装 解压后安装,使用默认安装路径 安装过程中.点击 ...
- B1012 [JSOI2008]最大数maxnumber 分块||RMQ
这个题有毒,卡最大值...我开1 << 30爆零让我以为我分块错了...gg,然后去写RMQ,但是这个题st表是真简单啊.后来刘胜与巨佬一眼看出来我最大值不够大...然后1LL<&l ...
- shp系列(六)——利用C++进行Dbf文件的写(创建)
上一篇介绍了shp文件的创建,接下来介绍dbf的创建. 推荐结合读取dbf的博客一起看! 推荐结合读取dbf的博客一起看! 推荐结合读取dbf的博客一起看! 1.Dbf头文件的创建 Dbf头文件的结构 ...
- ps -aux ,ps aux ,ps -ef 的区别
Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...
- C# 数组动态添加新元素的 方法
经常在开发中 会对字符串 进行split 拆分操作.. 得到数组后再去做相应的事情! 但有时候,需求决定了 数组的长度 不是固定的, 而C# 数组 是不允许动态添加新的元素的.. 这事情让我也纠结了 ...
- A - Presents
Problem description Little Petya very much likes gifts. Recently he has received a new laptop as a N ...