Project Euler 38 Pandigital multiples


题意:

将192分别与1、2、3相乘:


192 × 1 = 192
192 × 2 = 384
192 × 3 = 576



连接这些乘积,我们得到一个1至9全数字的数192384576。我们称192384576为192和(1,2,3)的连接乘积。


同样地,将9分别与1、2、3、4、5相乘,得到1至9全数字的数918273645,即是9和(1,2,3,4,5)的连接乘积。


对于n > 1,所有某个整数和(1,2, … ,n)的连接乘积所构成的数中,最大的1至9全数字的数是多少?


/*************************************************************************
    > File Name: euler038.c
    > Author:    WArobot 
    > Blog:      http://www.cnblogs.com/WArobot/ 
    > Created Time: 2017年06月27日 星期二 09时57分22秒
 ************************************************************************/

#include <stdio.h>
#include <math.h>
#include <inttypes.h>

#define max(a,b) ((a)>(b)?(a):(b))

bool IsPandigitalMultiples (int32_t n , int32_t* result) {
    int32_t ret = 0 , a[10] = {0};
    int32_t num = 0 , i = 1 , x;
    while (num < 9) {
        x = n * i;
        while (x) {
            if (a[x % 10])      return false;
            if (x % 10 == 0)    return false;
            a[x % 10] = (++num);                // 将记录位数和判断数组代码相结合,省去了a[x % 10] = 1 and ++num
            x /= 10;
        }
        ret *= (int32_t)pow(10 , (int32_t)floor(log10(n * i) + 1));
        ret += n * i; 
        ++i;
    }
    (*result) = ret;
    return true;
}
int32_t main() {
    int32_t ans = 0 , result;
    for (int32_t i = 1 ; i <= 10000 ; i++) {
        if (!IsPandigitalMultiples(i , &result))    continue;
        if (ans < result)   ans = result;
        printf("%d\n",i);
    }
    printf("%d\n",ans);
    return 0;
}

转载于:https://www.cnblogs.com/WArobot/p/7083791.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值