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

[转载] 趣味题

[复制链接]
 楼主| 发表于 2010-6-14 14:09:45 | 显示全部楼层
在美元符前加“\”,怎么“\”都显示出来的
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2010-6-14 14:42:43 | 显示全部楼层
你要等个几秒钟,它会自动消失的。 (如果是修改编辑自己的帖子,请强制刷新一下才能达到效果)
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2010-6-14 14:51:47 | 显示全部楼层
还是c效率高,没什么优化20秒
  1. #include <stdio.h>
  2. #include <time.h>
  3. int main()
  4. {
  5. int i,j,k,r,t;
  6. char buf[13];
  7. char f[13];
  8. r=0;
  9. t=clock();
  10. for(i=0x1234;i<=0xcba9/2;i++)
  11. {
  12. if ((i%0x10==0)||(i/0x10%0x10==0)||(i/0x100%0x10==0)||(i%0x10==0xd)||(i/0x10%0x10==0xd)||(i/0x100%0x10==0xd)||(i%0x10==0xf)||(i/0x10%0x10==0xf)||(i/0x100%0x10==0xf)||(i%0x10==0xe)||(i/0x10%0x10==0xe)||(i/0x100%0x10==0xe))continue;
  13. for(j=i+1;j<=(0xcba9-i);j++)
  14. {
  15. if ((j%0x10==0)||(j/0x10%0x10==0)||(j/0x100%0x10==0)||(j%0x10==0xd)||(j/0x10%0x10==0xd)||(j/0x100%0x10==0xd)||(j%0x10==0xf)||(j/0x10%0x10==0xf)||(j/0x100%0x10==0xf)||(j%0x10==0xe)||(j/0x10%0x10==0xe)||(j/0x100%0x10==0xe))continue;
  16. k=i+j;
  17. if ((k%0x10==0)||(k/0x10%0x10==0)||(k/0x100%0x10==0)||(k%0x10==0xd)||(k/0x10%0x10==0xd)||(k/0x100%0x10==0xd)||(k%0x10==0xf)||(k/0x10%0x10==0xf)||(k/0x100%0x10==0xf)||(k%0x10==0xe)||(k/0x10%0x10==0xe)||(k/0x100%0x10==0xe))continue;
  18. sprintf(buf,"%x%x%x",i,j,i+j);
  19. for(k=1;k<=12;k++)f[k]=0;
  20. for(k=1;k<=12;k++)
  21. if(++f[(buf[k-1]-'0'>0 && buf[k-1]-'0'<=9)?buf[k-1]-'0':buf[k-1]-'a'+10]>1)goto a;
  22. r++;
  23. a:
  24. ;
  25. }
  26. }
  27. t=clock()-t;
  28. printf("%d,%d",r,t);
  29. return 1;
  30. }
复制代码
  1. #include <stdio.h>
  2. int main()
  3. {
  4. int i,j,k,r;
  5. char buf[10];
  6. char f[10];
  7. r=0;
  8. for(i=123;i<=987;i++)
  9. {
  10. if ((i%10==0) || (i/10%10==0)||(i/100%10==0))continue;
  11. for(j=i+1;j<=(987-i);j++)
  12. {
  13. if ((j%10==0) || (j/10%10==0)||(j/100%10==0))continue;
  14. k=i+j;
  15. if ((k%10==0) || (k/10%10==0)||(k/100%10==0))continue;
  16. sprintf(buf,"%d%d%d",i,j,i+j);
  17. for(k=1;k<=9;k++)f[k]=0;
  18. for(k=1;k<=9;k++)
  19. if(++f[buf[k-1]-'0']>1)goto a;
  20. r++;
  21. a:
  22. ;
  23. }
  24. }
  25. printf("%d",r);
  26. return 1;
  27. }
复制代码
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2010-6-14 22:23:01 | 显示全部楼层
完全翻译plsql 6秒
  1. #include <stdio.h>
  2. #include <time.h>
  3. int main()
  4. {
  5. int i,j,k,r,t;
  6. char buf[13];
  7. char f[13];
  8. unsigned short I[12*11*90];
  9. unsigned short J[0xcbaa];
  10. r=0;
  11. t=clock();
  12. for(i=0x1234;i<=0xcba9;i++)
  13. {
  14. if ((i%0x10==0)||(i/0x10%0x10==0)||(i/0x100%0x10==0)||(i%0x10==0xd)||(i/0x10%0x10==0xd)||(i/0x100%0x10==0xd)||(i%0x10==0xf)||(i/0x10%0x10==0xf)||(i/0x100%0x10==0xf)||(i%0x10==0xe)||(i/0x10%0x10==0xe)||(i/0x100%0x10==0xe))
  15. J[i]=r;
  16. else
  17. {
  18. sprintf(buf,"%x",i);
  19. if (buf[0]!=buf[1] && buf[0]!=buf[2] && buf[0]!=buf[3] && buf[1]!=buf[2] && buf[1]!=buf[3] && buf[2]!=buf[3] )
  20. {
  21. I[r]=i;
  22. J[i]=r;
  23. r++;
  24. }
  25. else
  26. J[i]=r;
  27. }
  28. }
  29. r=0;
  30. for(i=0;i<J[0xcba9/2+1];i++)
  31. {
  32. for(j=J[I[i]+1];j<=J[0xcba9-I[i]];j++)
  33. {
  34. k=I[i]+I[j];
  35. if ((k%0x10==0)||(k/0x10%0x10==0)||(k/0x100%0x10==0)||(k%0x10==0xd)||(k/0x10%0x10==0xd)||(k/0x100%0x10==0xd)||(k%0x10==0xf)||(k/0x10%0x10==0xf)||(k/0x100%0x10==0xf)||(k%0x10==0xe)||(k/0x10%0x10==0xe)||(k/0x100%0x10==0xe))continue;
  36. sprintf(buf,"%x%x%x",I[i],I[j],k);
  37. for(k=1;k<=12;k++)f[k]=0;
  38. for(k=1;k<=12;k++)
  39. if(++f[(buf[k-1]-'0'>0 && buf[k-1]-'0'<=9)?buf[k-1]-'0':buf[k-1]-'a'+10]>1)goto a;
  40. r++;
  41. a:
  42. ;
  43. }
  44. }
  45. t=clock()-t;
  46. printf("%d,%d",r,t);
  47. return 1;
  48. }
复制代码
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2010-6-15 07:59:20 | 显示全部楼层
这题目用循环就能完成,而且保证不会取到重复数字
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2010-6-15 08:23:53 | 显示全部楼层
4.8s,觉得不改思路无法再优化了
  1. #include <stdio.h>
  2. #include <time.h>
  3. unsigned short m1(unsigned short i)
  4. {
  5. unsigned short a=i/0x1000%0x10+1;
  6. unsigned short b=i/0x100%0x10;
  7. unsigned short c=i/0x10%0x10;
  8. unsigned short d=i%0x10;
  9. unsigned short t=a;
  10. if(b==t)t=b+1;
  11. if(c==t)t=c+1;
  12. if(d==t)t=d+1;
  13. return t*0x1000;
  14. }
  15. unsigned short m(unsigned short in)
  16. {
  17. int i,j;
  18. unsigned short t;
  19. char buf[13]={0};
  20. buf[in/0x1000%0x10]=1;
  21. buf[in/0x100%0x10]=1;
  22. buf[in/0x10%0x10]=1;
  23. buf[in%0x10]=1;
  24. t=0;//find highest digit
  25. for(i=in/0x1000%0x10;i<=0xc;i++)
  26. {
  27. if(buf[i]==0)
  28. {
  29. t=i;
  30. buf[i]=1;
  31. break;
  32. }
  33. }
  34. if(t==0)
  35. return 0xcba9;
  36. for(i=1,j=0;i<=0xc&&j<3;i++)//find other digit
  37. if(buf[i]==0)
  38. {t=(t<<4)+i;
  39. j++;
  40. }
  41. return t;
  42. }
  43. int main()
  44. {
  45. int i,j,k,r,t;
  46. char buf[13];
  47. char f[13];
  48. unsigned short I[12*11*90];
  49. unsigned short J[0xcbaa];
  50. r=0;
  51. /*
  52. printf("%x:%x\n",0x7234,m(0x7234));
  53. printf("%x:%x\n",0x7235,m(0x7235));
  54. printf("%x:%x\n",0x7238,m(0x7238));
  55. printf("%x:%x\n",0x523a,m(0x523a));
  56. printf("%x:%x\n",0x523b,m(0x523b));
  57. printf("%x:%x\n",0x523c,m(0x523c));
  58. return 1;
  59. */
  60. t=clock();
  61. for(i=0x1234;i<=0xcba9;i++)
  62. {
  63. if ((i%0x10==0)||(i/0x10%0x10==0)||(i/0x100%0x10==0)||(i%0x10==0xd)||(i/0x10%0x10==0xd)||(i/0x100%0x10==0xd)||(i%0x10==0xf)||(i/0x10%0x10==0xf)||(i/0x100%0x10==0xf)||(i%0x10==0xe)||(i/0x10%0x10==0xe)||(i/0x100%0x10==0xe))
  64. J[i]=r;
  65. else
  66. {
  67. sprintf(buf,"%x",i);
  68. if (buf[0]!=buf[1] && buf[0]!=buf[2] && buf[0]!=buf[3] && buf[1]!=buf[2] && buf[1]!=buf[3] && buf[2]!=buf[3] )
  69. {
  70. I[r]=i;
  71. J[i]=r;
  72. r++;
  73. }
  74. else
  75. J[i]=r;
  76. }
  77. }
  78. r=0;
  79. for(i=0;i<J[0xcba9/2+1];i++)
  80. {
  81. for(j=J[m(I[i])];j<=J[0xcba9-I[i]];j++)
  82. {
  83. k=I[i]+I[j];
  84. if ((k%0x10==0)||(k/0x10%0x10==0)||(k/0x100%0x10==0)||(k%0x10==0xd)||(k/0x10%0x10==0xd)||(k/0x100%0x10==0xd)||(k%0x10==0xf)||(k/0x10%0x10==0xf)||(k/0x100%0x10==0xf)||(k%0x10==0xe)||(k/0x10%0x10==0xe)||(k/0x100%0x10==0xe))continue;
  85. sprintf(buf,"%x%x%x",I[i],I[j],k);
  86. for(k=1;k<=12;k++)f[k]=0;
  87. for(k=1;k<=12;k++)
  88. if(++f[(buf[k-1]-'0'>0 && buf[k-1]-'0'<=9)?buf[k-1]-'0':buf[k-1]-'a'+10]>1)goto a;
  89. r++;
  90. a:
  91. ;
  92. }
  93. }
  94. t=clock()-t;
  95. printf("%d,%d",r,t);
  96. return 1;
  97. }
复制代码
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2010-6-16 07:28:51 | 显示全部楼层
newkid的递归解法,比c还快。
  1. WITH n AS (
  2. SELECT ROWNUM n FROM DUAL CONNECT BY ROWNUM<=15
  3. )
  4. ,t (n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,lvl,roundup) AS (
  5. SELECT n,0,0,0,0,0,0,0,0,0,0,0,1,0
  6. FROM n
  7. WHERE n<8
  8. UNION ALL
  9. SELECT t.n1
  10. ,DECODE(t.lvl,1 ,n.n,t.n2 )
  11. ,DECODE(t.lvl,2 ,n.n,t.n3 )
  12. ,DECODE(t.lvl,3 ,n.n,t.n4 )
  13. ,DECODE(t.lvl,4 ,n.n,t.n5 )
  14. ,DECODE(t.lvl,5 ,n.n,t.n6 )
  15. ,DECODE(t.lvl,6 ,n.n,t.n7 )
  16. ,DECODE(t.lvl,7 ,n.n,t.n8 )
  17. ,DECODE(t.lvl,8 ,n.n,t.n9 )
  18. ,DECODE(t.lvl,9 ,n.n,t.n10)
  19. ,DECODE(t.lvl,10,n.n,t.n11)
  20. ,DECODE(t.lvl,11,n.n,t.n12)
  21. ,t.lvl+1
  22. ,CASE t.lvl
  23. WHEN 5 THEN FLOOR((t.n4 +t.n5 )/16)
  24. WHEN 8 THEN FLOOR((t.n7 +t.n8 +t.roundup)/16)
  25. WHEN 11 THEN FLOOR((t.n10+t.n11+t.roundup)/16)
  26. ELSE t.roundup
  27. END
  28. FROM t,n
  29. WHERE n.n NOT IN (n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11)
  30. AND (t.lvl IN (3,6,9)
  31. OR (t.lvl=11 AND n.n = MOD(t.n10+t.n11+t.roundup,16) AND t.n3=t.n1+t.n2+FLOOR((t.n10+t.n11)/16))
  32. OR (t.lvl=10 AND n.n>t.n10)
  33. OR (t.lvl=8 AND n.n = MOD(t.n7+t.n8+t.roundup,16))
  34. OR (t.lvl=7 AND n.n>t.n7)
  35. OR (t.lvl=5 AND n.n = MOD(t.n4+ t.n5,16))
  36. OR (t.lvl=4 AND n.n>t.n4)
  37. OR (t.lvl=2 AND n.n - t.n1- t.n2 IN (0,1))
  38. OR (t.lvl=1 AND n.n>t.n1 AND n.n+t.n1<=15)
  39. )
  40. )
  41. SELECT count(TO_CHAR(n1,'X') ||TO_CHAR(n10,'X')||TO_CHAR(n7,'X')||TO_CHAR(n4,'X')
  42. ||' + '
  43. ||TO_CHAR(n2,'X')||TO_CHAR(n11,'X')||TO_CHAR(n8,'X')||TO_CHAR(n5,'X')
  44. ||' = '
  45. ||TO_CHAR(n3,'X')||TO_CHAR(n12,'X')||TO_CHAR(n9,'X')||TO_CHAR(n6,'X')) ------ 位数对调的其他组合略
  46. FROM t
  47. WHERE lvl=12
  48. ;
  49. COUNT(*)*8
  50. ----------
  51. 3408
  52. 已用时间: 00: 00: 01.35
复制代码
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2010-6-16 10:03:38 | 显示全部楼层
abc+def=ghi 易得ghi≡0(mod9), 对于改进程序有用否? 4位16进制类似,可以mod15.
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2010-6-17 10:51:38 | 显示全部楼层
穷举是没有办法优化的,你要得到一个ghi才能判断是否ghi≡0(mod9),
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2010-6-17 12:33:32 | 显示全部楼层
这个可以按位从低位开始向高位搜索,而且两个加数对应位上的数字交换不改变结果,这个也可以减少一些搜索空间。 比如9个数字,那么两个加数最低位选择有$C_9^2=36$种不同选择,而其中有些选择可以直接淘汰,比如1+9,2+8,3+7,4+6,对于每种选择还可以直接计算出结果的最低位。然后接下去搜索两个加数的十位数,由于个位已经使用了3个数,余下有$C_6^2=15$种不同选择,而这时,根据结果的十位数估计至少可以淘汰掉一半;然后对于余下的继续搜索百位数,而搜索空间是一个树形的
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
您需要登录后才可以回帖 登录 | 欢迎注册

本版积分规则

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

GMT+8, 2024-11-24 12:15 , Processed in 0.026925 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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