题意:找出所有形如 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的更多相关文章

  1. 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 ...

  2. Project Euler 104:Pandigital Fibonacci ends 两端为全数字的斐波那契数

    Pandigital Fibonacci ends The Fibonacci sequence is defined by the recurrence relation: F[n] = F[n-1 ...

  3. Project Euler 41 Pandigital prime( 米勒测试 + 生成全排列 )

    题意:如果一个n位数恰好使用了1至n每个数字各一次,我们就称其为全数字的.例如,2143就是一个4位全数字数,同时它恰好也是一个素数. 最大的全数字的素数是多少? 思路: 最大全排列素数可以从 n = ...

  4. Project Euler 38 Pandigital multiples

    题意: 将192分别与1.2.3相乘: 192 × 1 = 192192 × 2 = 384192 × 3 = 576 连接这些乘积,我们得到一个1至9全数字的数192384576.我们称192384 ...

  5. Python练习题 039:Project Euler 011:网格中4个数字的最大乘积

    本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...

  6. [project euler] program 4

    上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...

  7. Python练习题 029:Project Euler 001:3和5的倍数

    开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...

  8. Project Euler 9

    题意:三个正整数a + b + c = 1000,a*a + b*b = c*c.求a*b*c. 解法:可以暴力枚举,但是也有数学方法. 首先,a,b,c中肯定有至少一个为偶数,否则和不可能为以上两个 ...

  9. 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 ...

随机推荐

  1. SIGPROF和SIGALRM使用总结

    这几天,被公司的两个模块的程序好好的搞了一下,开始以为是SHELL的问题,仔细研究了以下,原来没有想象的那么复杂!!!       关键在使用的信号SIGALRM 上,两个进程都用可SIGALRM 信 ...

  2. Linux如何把以下文件夹修改为root权限?

    inux 修改文件目录所有者例:要将当前目录下名 title 的文件夹及其子文件的所有者改为geust组的su用户,方法如下:#chown -R su.geust title-R 递归式地改变指定目录 ...

  3. nyoj--170--网络的可靠性(水题)

    网络的可靠性 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 A公司是全球依靠的互联网解决方案提供商,也是2010年世博会的高级赞助商.它将提供先进的网络协作技术,展示其 ...

  4. Node.js:template

    ylbtech-Node.js: 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:http://ylbtech ...

  5. Spring配置之OpenSessionInViewFilter

    转自:https://www.cnblogs.com/blogonfly/articles/3991782.html 参考: OpenSessionInViewFilter作用及配置:http://w ...

  6. day63-webservice 07.07.如何修改cxf配置文件路径

    为什么第一次访问http://localhost:8080/cxf-web-server/service有点慢?证明第一次访问的时候CXFServlet被初始化了被加载了.一般是让CXFServlet ...

  7. ubuntu的home目录下,Desktop等目录消失不见

    第一步:创建相应的文件夹 首先当然是创建几个相应的英文文件夹喽,比如:Desktop.Downloads. Documents. Music. Pictures. Videos  , Template ...

  8. IO编程 - 转载自廖雪峰的博文

    IO在计算机中指Input/Output,也就是输入和输出.由于程序和运行时数据是在内存中驻留,由CPU这个超快的计算核心来执行,涉及到数据交换的地方,通常是磁盘.网络等,就需要IO接口. 比如你打开 ...

  9. 学习java的方式

  10. java中字符串比较==和equals

    1 总体来说java中字符串的比较是==比较引用,equals 比较值的做法.(equals 对于其他引用类型比较的是地址,这是因为object的equals方法比较的是引用),但是不同的声明方法字符 ...