找回密码
 欢迎注册
楼主: shshsh_0510

[求助] 小学奥数求助

[复制链接]
发表于 2013-10-14 18:23:50 | 显示全部楼层
  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4. #define A a[0]
  5. #define B a[1]
  6. #define C a[2]
  7. #define D a[3]
  8. #define E a[4]
  9. #define F a[5]
  10. #define G a[6]
  11. #define H a[7]
  12. #define I a[8]
  13. #define NUM(x1,x2,x3,x4) (x1*1000+x2*100+x3*10+x4*1)
  14. int a[10]={1,2,3,4,5,6,7,8,9};
  15. int main()
  16. {
  17.         do{
  18.                 int x=NUM(0,0,A,B);
  19.                 int y=NUM(0,C,D,E);
  20.                 int z=NUM(F,G,H,I);
  21.                 if(x*y==z)
  22.                 {
  23.                         printf("   %d\n* %d\n-----\n %d\n   \n",x,y,z);
  24.                 }
  25.         }while(next_permutation(a,a+9));
  26.         return 0;
  27. }
复制代码

点评

这个代码参考了mathe的代码  发表于 2013-10-14 18:25
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2013-10-15 18:08:21 | 显示全部楼层
用Mathematica写9个循环,Compile后运行时间<0.1s,主要时间都花在编译上了,如果没有C编译器的话速度编译时间可以忽略,但运行慢了,大概在1s左右
  1. Clear["`*"];
  2. code1 = Block[{n = 9, cond, x},
  3.   cond = FromDigits[x /@ Range@2]*FromDigits[x /@ Range[3, 5]] == FromDigits[x /@ Range[6, 9]];
  4.   Fold[
  5.    do[If[And @@ Thread[Unequal[x[#2 + 1], Table[x[i], {i, #2}]]], #], {x[#2 + 1], 1, n}] &,
  6.    If[cond, Print@#] &@Table[x[i], {i, n}],
  7.    Range[n - 1, 0, -1]]
  8.   ]

  9. cf1 = With[{do = Do}, Compile[{Null}, #,  RuntimeOptions -> "Speed", CompilationTarget -> "C"]] &@code1;
  10. cf1[0] // AbsoluteTiming
复制代码
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
您需要登录后才可以回帖 登录 | 欢迎注册

本版积分规则

小黑屋|手机版|数学研发网 ( 苏ICP备07505100号 )

GMT+8, 2024-10-5 23:26 , Processed in 0.020693 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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