题意:找出所有形如 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. [bzoj4562][Haoi2016]食物链_记忆化搜索_动态规划

    食物链 bzoj-4562 Haoi-2016 题目大意:给你n个点,m条边的DAG,求所有的满足条件的链,使得每条链的起点是一个入度为0的点,中点是一条出度为0的点. 注释:$1\le n\le 1 ...

  2. 第三篇:SpringBoot - 数据库结构版本管理与迁移

    SpringBoot支持了两种数据库结构版本管理与迁移,一个是flyway,一个是liquibase.其本身也支持sql script,在初始化数据源之后执行指定的脚本,本章是基于 Liquibase ...

  3. 我是这样记录javascript知识的------Day31

    在陆续研究了几个javascript的几个小应用后,也算对javascript有了更深一点的认识,头脑中大约都有些印象,总体上说却有些模糊,这时.我知道,是时候看看w3cshool的这部分介绍了. 没 ...

  4. Android之UtilsRequesServicetHelp工具类

    package com.example.getnetutil; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; ...

  5. linux中字符串转换函数 simple_strtoul

    Linux内核中提供的一些字符串转换函数: lib/vsprintf.c 1. unsigned long long simple_strtoull(const char *cp, char **en ...

  6. php study80端口被占用

    php study80端口被占用 在网上找了各种办法,说是用命令查看占用端口的软件,将其停止,我发现我的端口是被system进程占用,而且这进程还结束不了. 1.打开PHPstudy,如图:打开端口常 ...

  7. php gd

    imagecopy() 函数用于拷贝图像或图像的一部分. imagecopyresized() 函数用于拷贝部分图像并调整大小. imagecopy() imagecopy() 函数用于拷贝图像或图像 ...

  8. kafka+storm 单机运行

    环境: 1.kafka+zookeeper 2.window平台 3.eclipse 设置: 1.kafka和zookeeper安装,另一篇有介绍(https://www.cnblogs.com/51 ...

  9. 将我们的parser转换成Monad

    还记得我们上一篇delegate类型的parser吗 ,在开始本篇之前,强烈建议你复习一下这个parser定义  public delegate Maybe<Tuple<T,string& ...

  10. [Codeforces]Good Bye 2017

    A - New Year and Counting Cards #pragma comment(linker, "/STACK:102400000,102400000") #inc ...