鱼C论坛

 找回密码
 立即注册
查看: 4902|回复: 16

题目32:找出所有能写成 pandigital 数字乘积的数字之和

[复制链接]
发表于 2015-4-23 23:40:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Pandigital products

We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital.

The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.

Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.

HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.

题目:

如果一个 n 位数使用了 1 到 n 中每个数字且只使用了一次,我们称其为 pandigital。例如,15234 这个五位数,是 1 到 5 pandigital 的。

7254 是一个不寻常的数,因为:39 × 186 = 7254 这个算式的乘数,被乘数和乘积组成了一个1到 9 的 pandigital 组合。

找出所有能够组合成 1 到 9 pandigital 的乘法算式中乘积的和。

提示: 有的乘积数字能够被多个乘法算式得到,所以在计算时要记得只计算它们一次。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-5-30 23:33:30 From FishC Mobile | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-8-29 16:42:22 | 显示全部楼层
4*1738=6952
4*1963=7852
12*483=5796
18*297=5346
27*198=5346
28*157=4396
39*186=7254
42*138=5796
48*159=7632

  1. bool mycalc(int a,int b,int c)
  2. {
  3.         string myResult;
  4.         char buf[25];
  5.         itoa(a,buf,10);
  6.         myResult+=buf;
  7.         itoa(b,buf,10);
  8.         myResult+=buf;
  9.         itoa(c,buf,10);
  10.         myResult+=buf;


  11.        
  12.         sort(myResult.begin(),myResult.end());
  13.         return myResult==string("123456789");
  14.        
  15. }
  16. int main()
  17. {
  18.     for (int n=2;n<=100000;n++)
  19.     {
  20.                 for (int m=2;m<=100000;m++)
  21.                 {
  22.                         int mul = n*m;
  23.                         if(mycalc(m,n,mul))
  24.                         {
  25.                                 std::cout<<n<<"*"<<m<<"="<<mul<<endl;
  26.                         }
  27.                 }
  28.     }
  29.         return 0;
  30.        
  31. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-9-2 15:51:51 | 显示全部楼层
  1. list1 = [1,2,3,4,5,6,7,8,9]
  2. list2 = []
  3. for i in range(1,100):
  4.       for j in range(100,10000):
  5.             temp = True
  6.             if len(str(i))+len(str(j))+len(str(i*j)) == 9:
  7.                   for each in list1:
  8.                         if str(each) not in (str(i)+str(j)+str(i*j)):
  9.                               temp = False
  10.                               break
  11.                   if temp:
  12.                         if i*j not in list2:
  13.                               list2.append(i*j)
  14. print(sum(list2))
复制代码

结果:45228
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2016-9-21 13:08:56 From FishC Mobile | 显示全部楼层
本帖最后由 jerryxjr1220 于 2016-10-12 12:41 编辑

楼上答案正确:45228
  1. def pan(n): return len(n)==9 and "123456789".strip(n)==""

  2. prod = set()
  3. for i in range(1,10):
  4.     for j in range(1234,9877):
  5.         if pan(str(i)+str(j)+str(i*j)): prod.add(i*j)

  6. for i in range(12,99):
  7.     for j in range(123,988):
  8.         if pan(str(i)+str(j)+str(i*j)): prod.add(i*j)

  9. print (sum(prod))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-1-13 16:10:40 | 显示全部楼层
  1. # encoding:utf-8
  2. # pandigital 之和
  3. from time import time
  4. def isDup(N):
  5.     if str(N).find('0') > -1 :
  6.         return False
  7.     if len(str(N)) == len(set(str(N))):
  8.         return True
  9.     return False
  10. def isPanDigital(n1, n2, n3):
  11.     l_t = []
  12.     l_t.extend(list(str(n1)))
  13.     l_t.extend(list(str(n2)))
  14.     l_t.extend(list(str(n3)))
  15.     l_t = set(l_t)
  16.     if len(l_t) == 9:
  17.         return True
  18.     return False
  19. def euler032():
  20.     l_result = []
  21.     for i in range(1, 10):
  22.         for j in range(1234, 9877):
  23.             if isDup(j):
  24.                 k = i * j
  25.                 if k > 9876:
  26.                     break
  27.                 if isDup(k):
  28.                     if isPanDigital(i, j, k):
  29.                         #print(i, j, k)
  30.                         l_result.append(k)
  31.     for i in range(12, 99):
  32.         for j in range(123, 988):
  33.             if isDup(j):
  34.                 k = i * j
  35.                 if k > 9876:
  36.                     break
  37.                 if isDup(k):
  38.                     if isPanDigital(i, j, k):
  39.                         #print(i, j, k)
  40.                         l_result.append(k)                    
  41.     print(sum(set(l_result)))            
  42. if __name__ == '__main__':
  43.     start = time()
  44.     # print(isDup(121))
  45.     euler032()
  46.     print('cost %.6f sec' % (time() - start))
复制代码

45228
cost 0.078008 sec
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-25 00:41:12 | 显示全部楼层
时间已过 30.829161 秒。
此代码使用matlab编程
Problem32所用时间为30.8314秒
Problem32的答案为45228
  1. %% Problem32
  2. %找出所有能写成pandigital 数字的乘积之和
  3. function Output = Problem32()
  4. tic

  5. value = [1 2 3 4 5 6 7 8 9];
  6. Judge = value * value';

  7. Record = [];
  8. All = perms([1 2 3 4 5 6 7 8 9]);
  9. [M, ~] = size(All);
  10. for aa = 1:M
  11.       %tempvector = str2num(num2str(aa)')';
  12.       tempvector = All(aa, :);
  13.       disp(tempvector)
  14.      if tempvector * tempvector' == Judge
  15.          t = tempvector;
  16.          a = t(1);
  17.          b = 1000*t(2) + 100*t(3) +10*t(4) + t(5);
  18.          c = 1000*t(6) + 100*t(7) +10*t(8) + t(9);      
  19.          if a * b == c
  20.              Record = union(Record, c);
  21.          end
  22.          d = t(1)*10 + t(2);
  23.          e = 100*t(3) + 10*t(4) +t(5);
  24.          f = 1000*t(6) + 100*t(7) + 10*t(8) + t(9);   
  25.          if d * e == f
  26.              Record = union(Record, f);
  27.          end
  28.      else
  29.          continue
  30.      end
  31. end

  32. toc

  33. Output = sum(Record);
  34. disp('此代码使用matlab编程')
  35. disp(['Problem32所用时间为',num2str(toc),'秒'])
  36. disp(['Problem32的答案为',num2str(Output)])
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-23 19:54:37 | 显示全部楼层
  1. #include <stdio.h>

  2. int is_pandigital(int a, int b, int product);

  3. int main()
  4. {
  5.     int a, b, product;
  6.     int sum = 0;
  7.     int results[128] = {0};
  8.     int i = 0, j;
  9.     int flag = 1;

  10.     for(a = 1; a < 1024 / 2; a++)
  11.     {
  12.         for(b = a; b < 4096; b++)
  13.         {
  14.             product = a * b;

  15.             if(is_pandigital(a, b, product))
  16.             {
  17.                 for(j = 0; j < i; j++)
  18.                 {
  19.                     if(results[j] == product)
  20.                     {
  21.                         flag = 0;
  22.                     }
  23.                 }

  24.                 if(flag)
  25.                 {
  26.                     results[i] = product;
  27.                     sum += product;
  28.                     printf("%d * %d = %d\n", a, b, product);
  29.                     i++;
  30.                 }

  31.                 flag = 1;
  32.             }
  33.         }
  34.     }

  35.     printf("\n%d\n", sum);

  36.     return 0;
  37. }

  38. int is_pandigital(int a, int b, int product)
  39. {
  40.     int count = 0;
  41.     int flag[10] = {0};
  42.     int i;

  43.     while(a > 0)
  44.     {
  45.         flag[a % 10]++;
  46.         a /= 10;
  47.         count++;
  48.         if(count > 9)
  49.         {
  50.             return 0;
  51.         }
  52.     }

  53.     while(b > 0)
  54.     {
  55.         flag[b % 10]++;
  56.         b = b / 10;
  57.         count++;
  58.         if(count > 9)
  59.         {
  60.             return 0;
  61.         }
  62.     }

  63.     while(product > 0)
  64.     {
  65.         flag[product % 10]++;
  66.         product = product / 10;
  67.         count++;
  68.         if(count > 9)
  69.         {
  70.             return 0;
  71.         }
  72.     }

  73.     for(i = 1; i < 10; i++)
  74.     {
  75.         if(flag[i] != 1)
  76.         {
  77.             return 0;
  78.         }
  79.     }

  80.     return 1;
  81. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-23 19:57:13 | 显示全部楼层
本帖最后由 永恒的蓝色梦想 于 2020-7-2 18:34 编辑

  1. def is_pandigital(a, b, product):
  2.     flag = [0] * 10
  3.     count = 0

  4.     while a > 0:
  5.         flag[a % 10] += 1
  6.         a //= 10
  7.         count += 1
  8.         if count > 9:
  9.             return 0

  10.     while b > 0:
  11.         flag[b % 10] += 1
  12.         b //= 10
  13.         count += 1
  14.         if count > 9:
  15.             return 0

  16.     while product > 0:
  17.         flag[product % 10] += 1
  18.         product //= 10
  19.         count += 1
  20.         if count > 9:
  21.             return 0

  22.     for each in flag[1:]:
  23.         if each != 1:
  24.             return 0

  25.     return 1

  26. def main():
  27.     results = []
  28.     answer = 0

  29.     for a in range(1, 512):
  30.         for b in range(a, 4096):
  31.             product = a * b

  32.             if is_pandigital(a, b, product):
  33.                 for each in results:
  34.                     if each == product:
  35.                         flag = 0

  36.                 if flag:
  37.                     results.append(product)
  38.                     answer += product
  39.                     print("%d * %d = %d" %(a, b, product))

  40.             flag = 1

  41.     print(answer)

  42.     return

  43. if __name__ == '__main__':
  44.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-29 16:28:34 | 显示全部楼层
  1. import java.util.Arrays;
  2. public class PandigitalProducts{
  3.         public static void main(String[] args){
  4.                 int[] m = new int[3];
  5.                 int[] n = new int[4];
  6.                 int[] result = new int[10];
  7.                 int[] consequence = new int[10];
  8.                 int r = 0,sum = 0;
  9.                 int count_1 = 0,count_2 = 0,count_3 = 0;
  10.                 int i = 0;
  11.                 for(int a = 1;a < 100;a ++){
  12.                         count_1 = Record(m,a);
  13.                         if(Repeat(m,count_1)){
  14.                                 continue;
  15.                         }
  16.                         for(int b = 101;b < 10000;b ++){
  17.                                 count_2 = Record(n,b);
  18.                                 if( Repeat(n,count_2) ){
  19.                                         continue;
  20.                                 }
  21.                                 if(Pandigital(m,n,count_2) ){
  22.                                         r = a * b;
  23.                                         count_3 = Record(result,r);
  24.                                         if(Repeat(result,count_3) || count_1+count_2+count_3 != 9){
  25.                                                 continue;
  26.                                         }
  27.                                         if(Pandigital(result,m,count_1) && Pandigital(result,n,count_2)){
  28.                                                 Arrays.sort(consequence);
  29.                                                 if(Arrays.binarySearch(consequence,r) < 0){
  30.                                                         sum += r;
  31.                                                 }
  32.                                                 System.out.println(a+"*"+b+"="+r);
  33.                                                 consequence[i++] = r;
  34.                                         }
  35.                                 }
  36.                                 else{
  37.                                         continue;
  38.                                 }
  39.                         }
  40.                        
  41.                 }
  42.                 System.out.println("去掉重复数字的和为"+sum);
  43.         }
  44.         public static int Record(int[] record,int x){
  45.                 int count = 0;
  46.                 for(int i = 0;i < record.length;i ++){
  47.                         record[i] = 0;
  48.                 }
  49.                 int temp = x;
  50.                 for(int i = 0;temp != 0;i ++){
  51.                         record[i] = temp % 10;
  52.                         count++;
  53.                         temp /= 10;
  54.                 }
  55.                 return count;
  56.         }
  57.         public static boolean Pandigital(int[] record,int[] judge,int count){
  58.                 boolean flag = true;
  59.         top:for(int i = 0;i < count;i ++){
  60.                         for(int j = 0;j < record.length;j ++){
  61.                                 if(judge[i] == record[j]){
  62.                                         flag = false;
  63.                                         break top;
  64.                                 }
  65.                         }
  66.                 }
  67.                 return flag;
  68.         }
  69.         public static boolean Repeat(int[] a,int count){
  70.                 boolean flag = false;
  71.                 for(int i = 0;i < count-1;i ++){
  72.                         for(int j = i+1;j < count;j ++){
  73.                                 if(a[i] == a [j] && !flag){
  74.                                         flag = true;
  75.                                         break;
  76.                                 }
  77.                         }
  78.                 }
  79.                 for(int i = 0;i < count;i ++){
  80.                         if(a[i] == 0){
  81.                                 flag = true;
  82.                         }
  83.                 }
  84.                 return flag;
  85.         }
  86. }
复制代码

4*1738=6952
4*1963=7852
12*483=5796
18*297=5346
27*198=5346
28*157=4396
39*186=7254
42*138=5796
48*159=7632
去掉重复数字的和为45228
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-13 12:03:04 | 显示全部楼层
本帖最后由 王小召 于 2019-6-13 12:08 编辑

6952 = 4 * 1738
7852 = 4 * 1963
5796 = 12 * 483
5346 = 18 * 297
5346 = 27 * 198
4396 = 28 * 157
7254 = 39 * 186
5796 = 42 * 138
7632 = 48 * 159
结果表单:[5346, 5796, 6952, 7852, 4396, 7632, 7254]
代数和:45228
用时:0.2964 秒
  1. import time

  2. def get_pandigital():
  3.     code_list = list(map(str, range(1, 10)))
  4.     target = []
  5.     # x < y
  6.     for x in range(1, 100000):
  7.         for y in range(x, 100000):
  8.             length = len(str(x) + str(y) + str(x * y))
  9.             # eg:100 * 100 = 10000 已经超过9个字母了所以100 * (101,102...)没必要继续了
  10.             if length > 9:
  11.                 break
  12.             else:
  13.                 if length == 9:
  14.                     l1 = [i for i in (str(x) + str(y) + str(x * y))]
  15.                     l1.sort()
  16.                     if code_list == l1:
  17.                         print("{} = {} * {}".format(x*y, x, y))
  18.                         target.append(x*y)
  19.     return list(set(target)), sum(list(set(target)))

  20. pg_list, summary = get_pandigital()
  21. print("结果表单:{}\n代数和:{}\n用时:{:.4f} 秒".format(pg_list, summary, time.process_time()))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-15 16:05:59 | 显示全部楼层
45228

Process returned 0 (0x0)   execution time : 0.020 s
Press any key to continue.
经分析,算式只可能有一位数*四位数=四位数 或 两位数*三位数=四位数 两种形式
暴力枚举后用set判重
  1. #include<iostream>
  2. #include<set>
  3. #include<cstring>
  4. using namespace std;
  5. int stat[10];

  6. bool parse(int x){
  7.   while(x){
  8.     int t = x % 10;
  9.     if (stat[t]) return false;
  10.     stat[t] = 1;
  11.     x /= 10;
  12.   }
  13.   return true;
  14. }

  15. bool pan(int x,int y,int z){
  16.   memset(stat,0,sizeof(stat));

  17.   if (!parse(x))  return false;
  18.   if (!parse(y))  return false;
  19.   if (!parse(z))  return false;
  20.   if (stat[0])    return false;

  21.   return true;
  22. }

  23. int main(){
  24.   set<int> s;
  25.   int sum = 0;

  26.   for (int a = 2;a < 10;a++){
  27.     for (int b = 1002;b < 10000;b++){
  28.       int c = a * b;
  29.       if (c >= 10000) break;
  30.       if (pan(a,b,c)) s.insert(c);
  31.     }
  32.   }

  33.   for (int a = 12;a < 100;a++){
  34.     for (int b = 102;b < 1000;b++){
  35.       int c = a * b;
  36.       if (c >= 10000) break;
  37.       if (pan(a,b,c)) s.insert(c);
  38.     }
  39.   }

  40.   for (set<int>::iterator it = s.begin();it != s.end();it++){
  41.     sum += *it;
  42.   }
  43.   cout << sum << endl;

  44.   return 0;
  45. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-22 11:50:25 | 显示全部楼层
  1. start = time.time()
  2. d = 0
  3. check = []
  4. for a in range(1, 100):
  5.         for b in range(1,10000):
  6.                 c = a*b
  7.                 check1 = list(str(a)+str(b)+str(c))
  8.                 check1.sort()
  9.                 check2 = list(set(check1))
  10.                 check2.sort()
  11.                 if '0' not in str(a) and '0' not in str(b) and '0' not in str(c) and check1 == check2 and len(check2) == 9:
  12.                         print("%d * %d = %d" %(a,b,c))
  13.                         check.append(c)
  14. check = list(set(check))
  15. for each in check:
  16.         d+= each
  17. print("去除重复代数后的代数和为%d" %d)
  18. end = time.time()
  19. print("共用时%f秒" %(end - start))
复制代码

4 * 1738 = 6952
4 * 1963 = 7852
12 * 483 = 5796
18 * 297 = 5346
27 * 198 = 5346
28 * 157 = 4396
39 * 186 = 7254
42 * 138 = 5796
48 * 159 = 7632
去除重复代数后的代数和为45228
共用时2.755275秒
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-14 15:13:29 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <string.h>

  3. int check_num(int);
  4. int check_num(int num)
  5. {
  6.         int i, j, a[10] = { 0 };//用于标记数字是否存在

  7.         j = num;
  8.         while (j)
  9.         {
  10.                 i = j % 10;
  11.                 if (i == 0)
  12.                 {
  13.                         return 0;
  14.                 }
  15.                 if (a[i] == 1)
  16.                 {
  17.                         return 0;
  18.                 }
  19.                 a[i] = 1;
  20.                 j /= 10;
  21.         }
  22.         return 1;
  23. }


  24. main()
  25. {
  26.         int i, j, k, sum = 0, e, x = 0, y;
  27.         char str[20];
  28.         int a[10000] = { 0 }; //用于标记数字是否已经加过

  29.         printf("k = ");
  30.         for (i = 1; i < 99; i++)
  31.         {
  32.                 for (j = 100; j < 9999; j++)
  33.                 {
  34.                         k = i * j;
  35.                         if (k > 10000)
  36.                         {
  37.                                 break;
  38.                         }
  39.                        
  40.                         if ((check_num(k) == 0) || (check_num(j) == 0) || (check_num(i) == 0))
  41.                         {
  42.                                 continue;
  43.                         }
  44.                        
  45.                         y = k;

  46.                         while (y)
  47.                         {
  48.                                 e = y % 10;
  49.                                 str[x++] = e + '0';
  50.                                 y /= 10;
  51.                         }
  52.                         y = j;
  53.                         while (y)
  54.                         {
  55.                                 e = y % 10;
  56.                                 str[x++] = e + '0';
  57.                                 y /= 10;
  58.                         }
  59.                         y = i;
  60.                         while (y)
  61.                         {
  62.                                 e = y % 10;
  63.                                 str[x++] = e + '0';
  64.                                 y /= 10;
  65.                         }
  66.                         str[x] = '\0';
  67.                         if (strlen(str) < 9)
  68.                         {
  69.                                 continue;
  70.                         }
  71.                         y = atoi(str);
  72.                        
  73.                         if (check_num(y))
  74.                         {
  75.                                 if (a[k] == 0)
  76.                                 {
  77.                                         sum += k;
  78.                                         printf("%d ", k);
  79.                                 }
  80.                                 a[k] = 1;       
  81.                         }
  82.                         x = 0;
  83.                 }
  84.         }
  85.         printf("\nsum = %d\n", sum);
  86. }
复制代码


答案:
k = 6952 7852 5796 5346 4396 7254 7632
sum = 45228
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-18 20:01:10 | 显示全部楼层
#找出所有能够组合成 1 到 9 pandigital 的乘法算式中乘积的和。
from time import *
'''
如果一个n位数使用了1到n中每个数字且只使用了一次,
我们称其为 pandigital。
例如,15234 这个五位数,是1到5pandigital 的。
7254 是一个不寻常的数,
因为:39 × 186 = 7254 这个算式的乘数,被乘数和乘积组成了一个1到9的 pandigital 组合。
找出所有能够组合成 1 到 9 pandigital 的乘法算式中乘积的和。
'''
#处理字符串
def deal_str(string):
    string_list = []
    for each in string:
        string_list.append(each)
    string_list.sort()
    return string_list

start = time()

numlist = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
res = []
c = 1

for a in range(1, 98):
    for b in range(123, 9876):
        c = a * b
        if deal_str(str(a) + str(b) + str(c)) == numlist:
            res.append([a, b, c])
        
#去除重复项
result = []
for each in res:
    if each[2] not in result:
        result.append(each[2])
    else:
        res.remove(each)
#计算
total = 0
for each in res:
    total += each[2]
end = time()
print(total)
print("用时%.2f秒" % (end - start))


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-3 13:19:07 | 显示全部楼层
  1. /*
  2. 答案:45228
  3. 耗时:0.0250623秒
  4. */
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <cstring>
  9. using namespace std;

  10. int main(void)
  11. {
  12.   char str[12] = "123456789";
  13.   vector<int> v;
  14.   do
  15.   {
  16.     char str1[8], str2[8], str3[8];
  17.     for (int i = 1; i <= 2; ++i)//对第一个乘数的长度进行枚举
  18.     {
  19.       memset(str1, 0, sizeof(str1));
  20.       strncpy(str1, str, i);//得到第一个乘数
  21.       int j = 5 - i;//求得第二个乘数的长度
  22.       memset(str2, 0, sizeof(str2));
  23.       strncpy(str2, str + i, j);//得到第二个乘数
  24.       memset(str3, 0, sizeof(str3));
  25.       strncpy(str3, str + 5, 4);//得到第三个乘数
  26.       int a1 = atoi(str1), a2 = atoi(str2), a3 = atoi(str3);
  27.       if (a1 * a2 == a3)
  28.         v.push_back(a3);//满足要求保存
  29.     }
  30.   } while (next_permutation(str, str + 9));//求出下一个组合
  31.   //去重
  32.   sort(v.begin(), v.end());
  33.   auto itr_end = unique(v.begin(), v.end());
  34.   //求和
  35.   int nSum = 0;
  36.   for (auto itr = v.begin(); itr != itr_end; ++itr)
  37.     nSum += *itr;
  38.   cout << nSum << endl;
  39.   return 0;
  40. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-12 13:42:06 | 显示全部楼层
简单想一想,可能的组合只有这两种:
* x **** = ****
** x *** = ****
剩下的就很简单了
另外,为提升速度,可以使用位运算而不是字符串来存储各个位。
代码:
  1. use std::collections::HashSet;
  2. fn bits(x: usize) -> u32 {
  3.     let mut x = x;
  4.     let mut ans = 0;
  5.     while x != 0 && x % 10 != 0 {
  6.         ans ^= 1 << (x % 10 - 1);
  7.         x /= 10;
  8.     }
  9.     ans
  10. }
  11. fn main() {
  12.     let mut v = HashSet::new();
  13.     for i in 1..=9 {
  14.         for j in 1..=9999 {
  15.             if i * j > 9999 {
  16.                 break;
  17.             }
  18.             if bits(i) + bits(j) + bits(i * j) == 0b111111111 {
  19.                 v.insert(i * j);
  20.                 println!("{} * {} = {}", i, j, i * j)
  21.             }
  22.         }
  23.     }
  24.     for i in 1..=99 {
  25.         for j in 1..=999 {
  26.             if i * j > 9999 {
  27.                 break;
  28.             }
  29.             if bits(i) + bits(j) + bits(i * j) == 0b111111111 {
  30.                 v.insert(i * j);
  31.                 println!("{} * {} = {}", i, j, i * j)
  32.             }
  33.         }
  34.     }
  35.     println!("{}\n{}", v.len(), v.iter().sum::<usize>());
  36. }
复制代码

输出:
  1. $ time ./main
  2. 4 * 1738 = 6952
  3. 4 * 1963 = 7852
  4. 12 * 483 = 5796
  5. 18 * 297 = 5346
  6. 27 * 198 = 5346
  7. 28 * 157 = 4396
  8. 39 * 186 = 7254
  9. 42 * 138 = 5796
  10. 48 * 159 = 7632
  11. 7
  12. 45228

  13. real        0m0.002s
  14. user        0m0.002s
  15. sys        0m0.000s
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-20 01:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表