欧拉计划38题

Pandigital multiples

Take the number 192 and multiply it by each of 1, 2, and 3:

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

By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call 192384576 the concatenated product of 192 and (1,2,3)

The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, and 5, giving the pandigital, 918273645, which is the concatenated product of 9 and (1,2,3,4,5).

What is the largest 1 to 9 pandigital 9-digit number that can be formed as the concatenated product of an integer with (1,2,…,n) where n>1?

全数字的倍数

将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全数字的数是多少?

 

        这个题目求,一个数n然后去乘以1-n(n>1),意思就是最少乘到2,然后得到的乘积乘1的乘积开始往后拼接,直到有9位,并且这9位包含了1-9全数字;题目要求的是得到的结果中1-9数字数是多少;       

        然后开始分析:

        1.如何拼接数字

        2.拼接完成判断数是否等于9位

        3.判断有9位后,是否是1-9全数字

        4.最后如果是判断比之前的最大值是否还大,如果大就存储当前数;

        5.枚举的上界是什么,因为最少乘到二,所以最高只能为4位数,因为为5位数的话,1乘以5位数有5位数,2乘以5位数还是有5位数,拼接起来就有10位数不能满足题目条件了;

        有了这几个条件可以开始去着手写代码,有了方法就可以把问题分为几个模块来实现:


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

int get_len(int x) {
    return log10(x) + 1;//获取位数 
}

long long is_all_number(int x) {
    int n = 1;
    long long temp = 0;
    while (get_len(temp) != 9) {//拼接数字
        int sum = n * x;
        temp = temp * (int)pow(10, (int)log10(sum) + 1) + sum;//留够0的位置进行加上,就相当于拼接上了数字
        n++;
        if (get_len(temp) > 9) return 0;//如果位数大于9不满住题目条件直接返回0
    }
    int num[10] = {0};
    num[0] = 1;
    long long s = temp;
    while (s) {//判断是否是全数字
        if (num[s % 10]) return 0;//出现0或者出现重复数字直接返回0
        num[s % 10] = 1;
        s /= 10;
    }
    return temp;
}



int main() {
    long long ans = 0;
    for (int i = 1; i < 10000; i++) {
        long long s = is_all_number(i);
        if (!s) continue;
        ans = fmax(ans, s);//取最大值
    } 
    printf("%lld\n", ans);
    return 0;
}

最终答案: 932718654

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

初猿°

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值