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

[转载] 趣味题

[复制链接]
 楼主| 发表于 2010-6-19 13:54:43 | 显示全部楼层
把newkid的plsql翻译成c,毫秒级算出3408组abcd+efgh=hijk
  1. /*WITH n AS (
  2. SELECT ROWNUM n FROM DUAL CONNECT BY ROWNUM<=12
  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 BETWEEN 1 AND 7
  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(*)*8
  42. SELECT TO_CHAR(n1,'X') ||TO_CHAR(n10,'X')||TO_CHAR(n7,'X')||TO_CHAR(n4,'X')
  43. ||' + '
  44. ||TO_CHAR(n2,'X')||TO_CHAR(n11,'X')||TO_CHAR(n8,'X')||TO_CHAR(n5,'X')
  45. ||' = '
  46. ||TO_CHAR(n3,'X')||TO_CHAR(n12,'X')||TO_CHAR(n9,'X')||TO_CHAR(n6,'X') ------ 位数对调的其他组合略
  47. FROM t
  48. WHERE lvl=12
  49. ;
  50. 41 SELECT COUNT(*),lvl from t group by lvl order by 2;
  51. COUNT(*) LVL
  52. --------- ----------
  53. 7 1
  54. 46 2
  55. 55 3
  56. 495 4
  57. 1980 5
  58. 953 6
  59. 5718 7
  60. 14295 8
  61. 3901 9
  62. 11703 10
  63. 11703 11
  64. 426 12
  65. 已选择12行。
  66. */
  67. #include <stdio.h>
  68. #include <time.h>
  69. int main()
  70. {
  71. int i=0;
  72. int j=0;
  73. int k=0;
  74. int l=0;
  75. char a[65500][15];
  76. int b[15];
  77. int c=0;
  78. for(i=1;i<=7;i++)//level 1 第一个数第一位最大7
  79. {
  80. a[i][1]=i;
  81. a[i][2]=a[i][3]=a[i][4]=a[i][5]=a[i][6]=a[i][7]=a[i][8]=a[i][9]=a[i][10]=a[i][11]=a[i][12]=0;
  82. a[i][13]=1;
  83. a[i][14]=0;
  84. }
  85. b[0]=1;
  86. b[1]=8;
  87. i=b[1]-1;
  88. for(j=2;j<=12;j++)//level 2-12
  89. {
  90. printf("j=%d,%d-%d,count=%d\n",j,b[j-2],b[j-1]-1,b[j-1]-b[j-2]);
  91. for(k=1;k<=12;k++)//digit 1-12
  92. {
  93. for(l=b[j-2];l<b[j-1];l++) //l -> last level
  94. {
  95. if(
  96. (k!=a[l][1]&&k!=a[l][2]&&k!=a[l][3]&&k!=a[l][4]&&k!=a[l][5]&&k!=a[l][6]&&k!=a[l][7]&&k!=a[l][8]&&k!=a[l][9]&&k!=a[l][10]&&k!=a[l][11])
  97. &&((a[l][13]==3||a[l][13]==6||a[l][13]==9)
  98. ||(a[l][13]==11&&k==(a[l][10]+a[l][11]+a[l][14])%16&&a[l][3]==a[l][1]+a[l][2]+(a[l][10]+a[l][11])/16)
  99. ||(a[l][13]==10&&k>a[l][10])
  100. ||(a[l][13]==8&&k==(a[l][7]+a[l][8]+a[l][14])%16)
  101. ||(a[l][13]==7&&k>a[l][7])
  102. ||(a[l][13]==5&&k==(a[l][4]+a[l][5])%16)
  103. ||(a[l][13]==4&&k>a[l][4])
  104. ||(a[l][13]==2&&(k==a[l][1]+a[l][2]||k==a[l][1]+a[l][2]+1))
  105. ||(a[l][13]==1&&k>a[l][1]&&k+a[l][1]<=12)//15)
  106. )
  107. )
  108. {
  109. i++;
  110. a[i][1]=a[l][1];
  111. a[i][2]=(a[l][13]==1)?k:a[l][2];
  112. a[i][3]=(a[l][13]==2)?k:a[l][3];
  113. a[i][4]=(a[l][13]==3)?k:a[l][4];
  114. a[i][5]=(a[l][13]==4)?k:a[l][5];
  115. a[i][6]=(a[l][13]==5)?k:a[l][6];
  116. a[i][7]=(a[l][13]==6)?k:a[l][7];
  117. a[i][8]=(a[l][13]==7)?k:a[l][8];
  118. a[i][9]=(a[l][13]==8)?k:a[l][9];
  119. a[i][10]=(a[l][13]==9)?k:a[l][10];
  120. a[i][11]=(a[l][13]==10)?k:a[l][11];
  121. a[i][12]=(a[l][13]==11)?k:a[l][12];
  122. a[i][13]=a[l][13]+1;
  123. a[i][14]=
  124. (a[l][13]==5)?(a[l][4]+a[l][5])/16:(
  125. (a[l][13]==8?(a[l][7]+a[l][8]+a[l][14])/16:(
  126. (a[l][13]==11)?(a[l][10]+a[l][11]+a[l][14])/16:a[l][14])));
  127. if(a[i][13]==12)
  128. {
  129. c++;
  130. }
  131. } //if
  132. } //l
  133. } //k
  134. b[j]=i+1;
  135. //printf("%x%x%x%x+%x%x%x%x=%x%x%x%x\n",/*1 10 7 4 2 11 8 5 3 12 9 6*/a[i][1],a[i][10],a[i][7],a[i][4],a[i][2],a[i][11],a[i][8],a[i][5],a[i][3],a[i][12],a[i][9],a[i][6]);
  136. } //j
  137. printf("c=%d\n",c*8);
  138. return 1;
  139. }
复制代码
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
您需要登录后才可以回帖 登录 | 欢迎注册

本版积分规则

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

GMT+8, 2024-11-22 01:38 , Processed in 0.021876 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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