题意:找出所有形如 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. spring-logback

    <?xml version="1.0" encoding="UTF-8"?><!-- 说明: 1.日志级别及文件 日志记录采用分级记录,级别与 ...

  2. 自己定义ShareSDK分享平台界面

    自己定义ShareSDK分享平台界面 执行效果图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ2FvX2NodW4=/font/5a6L5L2T/fo ...

  3. OpenStack开发基础-oslo.config

    The cfg Module cfg Module来自于OpenStack中的重要的基础组件oslo.config,通过cfg Module能够用来通过命令行或者是配置文件来配置一些options,对 ...

  4. [寒江孤叶丶的Cocos2d-x之旅_32]微信输入框风格的IOS平台的EditBox

    原创文章,欢迎转载.转载请注明:文章来自[寒江孤叶丶的Cocos2d-x之旅系列] 博客地址:http://blog.csdn.net/qq446569365 偶然看到一个游戏注冊账号时候,输入框是弹 ...

  5. PDF.NET支持最新的SQLite数据库

    最近项目中用到了SQLite,之前项目中用的是PDF.NET+MySQL的组合,已经写了不少代码,如果能把写好的代码直接用在SQLite上就好了,PDF.NET支持大部分主流的数据库,这个当然可以,只 ...

  6. putty配色方案【转】

    本文转载自:http://blog.csdn.net/hfut_jf/article/details/53636080 putty默认的配色方案简直毫无人道主义可言,所以找了个,好多了,转载自http ...

  7. Mail发送封装类

    代码实现: MailSmtp ms = ","xxxx"); //可选参数 ms.SetCC("610262374@qq.com");//抄送可以多个 ...

  8. golang 初体验

    1.下载golang https://code.google.com/p/go/downloads/list 在windows下安装,下载windows32版本 2.安装 安装完毕,默认在C:\Go ...

  9. 【NOIP2011 Day 2】观光公交

    [问题描述] 小城Y市,拥有n个景点.由于慕名而来的游客越来越多,Y市特意安排了一辆观光公交车,为游客提供更便捷的交通服务.观光公交车在第0分钟出现在1号景点,随后依次前往2.3.4……n号景点.从第 ...

  10. 最详细的CentOS 6与7对比(一):常见设置对比

    本主题将从3个角度进行对比 常见设置(CentOS 6 vs CentOS 7) 服务管理(Sysvinit vs Upstart vs Systemd) 性能测试(cpu/mem/io/oltp) ...