〇〇
发表于 2010-6-19 13:54:43
把newkid的plsql翻译成c,毫秒级算出3408组abcd+efgh=hijk
/*WITH n AS (
SELECT ROWNUM n FROM DUAL CONNECT BY ROWNUM<=12
)
,t (n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,lvl,roundup) AS (
SELECT n,0,0,0,0,0,0,0,0,0,0,0,1,0
FROM n
WHERE n BETWEEN 1 AND 7
UNION ALL
SELECT t.n1
,DECODE(t.lvl,1 ,n.n,t.n2 )
,DECODE(t.lvl,2 ,n.n,t.n3 )
,DECODE(t.lvl,3 ,n.n,t.n4 )
,DECODE(t.lvl,4 ,n.n,t.n5 )
,DECODE(t.lvl,5 ,n.n,t.n6 )
,DECODE(t.lvl,6 ,n.n,t.n7 )
,DECODE(t.lvl,7 ,n.n,t.n8 )
,DECODE(t.lvl,8 ,n.n,t.n9 )
,DECODE(t.lvl,9 ,n.n,t.n10)
,DECODE(t.lvl,10,n.n,t.n11)
,DECODE(t.lvl,11,n.n,t.n12)
,t.lvl+1
,CASE t.lvl
WHEN 5THEN FLOOR((t.n4 +t.n5 )/16)
WHEN 8THEN FLOOR((t.n7 +t.n8 +t.roundup)/16)
WHEN 11 THEN FLOOR((t.n10+t.n11+t.roundup)/16)
ELSE t.roundup
END
FROM t,n
WHERE n.n NOT IN (n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11)
AND (t.lvl IN (3,6,9)
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))
OR (t.lvl=10 AND n.n>t.n10)
OR (t.lvl=8 AND n.n = MOD(t.n7+t.n8+t.roundup,16))
OR (t.lvl=7 AND n.n>t.n7)
OR (t.lvl=5 AND n.n = MOD(t.n4+ t.n5,16))
OR (t.lvl=4 AND n.n>t.n4)
OR (t.lvl=2 AND n.n - t.n1- t.n2 IN (0,1))
OR (t.lvl=1 AND n.n>t.n1 AND n.n+t.n1<=15)
)
)
SELECT COUNT(*)*8
SELECT TO_CHAR(n1,'X')||TO_CHAR(n10,'X')||TO_CHAR(n7,'X')||TO_CHAR(n4,'X')
||' + '
||TO_CHAR(n2,'X')||TO_CHAR(n11,'X')||TO_CHAR(n8,'X')||TO_CHAR(n5,'X')
||' = '
||TO_CHAR(n3,'X')||TO_CHAR(n12,'X')||TO_CHAR(n9,'X')||TO_CHAR(n6,'X')------ 位数对调的其他组合略
FROM t
WHERE lvl=12
;
41SELECT COUNT(*),lvl from t group by lvl order by 2;
COUNT(*) LVL
--------- ----------
7 1
46 2
55 3
495 4
1980 5
953 6
5718 7
14295 8
3901 9
11703 10
11703 11
426 12
已选择12行。
*/
#include <stdio.h>
#include <time.h>
int main()
{
int i=0;
int j=0;
int k=0;
int l=0;
char a;
int b;
int c=0;
for(i=1;i<=7;i++)//level 1 第一个数第一位最大7
{
a=i;
a=a=a=a=a=a=a=a=a=a=a=0;
a=1;
a=0;
}
b=1;
b=8;
i=b-1;
for(j=2;j<=12;j++)//level 2-12
{
printf("j=%d,%d-%d,count=%d\n",j,b,b-1,b-b);
for(k=1;k<=12;k++)//digit 1-12
{
for(l=b;l<b;l++) //l -> last level
{
if(
(k!=a&&k!=a&&k!=a&&k!=a&&k!=a&&k!=a&&k!=a&&k!=a&&k!=a&&k!=a&&k!=a)
&&((a==3||a==6||a==9)
||(a==11&&k==(a+a+a)%16&&a==a+a+(a+a)/16)
||(a==10&&k>a)
||(a==8&&k==(a+a+a)%16)
||(a==7&&k>a)
||(a==5&&k==(a+a)%16)
||(a==4&&k>a)
||(a==2&&(k==a+a||k==a+a+1))
||(a==1&&k>a&&k+a<=12)//15)
)
)
{
i++;
a=a;
a=(a==1)?k:a;
a=(a==2)?k:a;
a=(a==3)?k:a;
a=(a==4)?k:a;
a=(a==5)?k:a;
a=(a==6)?k:a;
a=(a==7)?k:a;
a=(a==8)?k:a;
a=(a==9)?k:a;
a=(a==10)?k:a;
a=(a==11)?k:a;
a=a+1;
a=
(a==5)?(a+a)/16:(
(a==8?(a+a+a)/16:(
(a==11)?(a+a+a)/16:a)));
if(a==12)
{
c++;
}
} //if
} //l
} //k
b=i+1;
//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,a,a,a,a,a,a,a,a,a,a,a);
} //j
printf("c=%d\n",c*8);
return 1;
}