Pandigital multiples
Take the number 192 and multiply it by each of 1, 2, and 3:
192×1=192
192×2=384
192×3=576By 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