〇〇 发表于 2010-6-14 14:09:45

在美元符前加“\”,怎么“\”都显示出来的

gxqcn 发表于 2010-6-14 14:42:43

你要等个几秒钟,它会自动消失的。
(如果是修改编辑自己的帖子,请强制刷新一下才能达到效果)

〇〇 发表于 2010-6-14 14:51:47

还是c效率高,没什么优化20秒
#include <stdio.h>
#include <time.h>
int main()
{
int i,j,k,r,t;
char buf;
char f;
r=0;
t=clock();
for(i=0x1234;i<=0xcba9/2;i++)
{
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;
for(j=i+1;j<=(0xcba9-i);j++)
{
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;
k=i+j;
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;
sprintf(buf,"%x%x%x",i,j,i+j);
for(k=1;k<=12;k++)f=0;
for(k=1;k<=12;k++)
if(++f[(buf-'0'>0 && buf-'0'<=9)?buf-'0':buf-'a'+10]>1)goto a;
r++;
a:
;
}
}
t=clock()-t;
printf("%d,%d",r,t);
return 1;
}

#include <stdio.h>
int main()
{
int i,j,k,r;
char buf;
char f;
r=0;
for(i=123;i<=987;i++)
{
if ((i%10==0) || (i/10%10==0)||(i/100%10==0))continue;
for(j=i+1;j<=(987-i);j++)
{
if ((j%10==0) || (j/10%10==0)||(j/100%10==0))continue;
k=i+j;
if ((k%10==0) || (k/10%10==0)||(k/100%10==0))continue;
sprintf(buf,"%d%d%d",i,j,i+j);
for(k=1;k<=9;k++)f=0;
for(k=1;k<=9;k++)
if(++f-'0']>1)goto a;
r++;
a:
;
}
}
printf("%d",r);
return 1;
}

〇〇 发表于 2010-6-14 22:23:01

完全翻译plsql 6秒
#include <stdio.h>
#include <time.h>
int main()
{
int i,j,k,r,t;
char buf;
char f;
unsigned short I;
unsigned short J;
r=0;
t=clock();
for(i=0x1234;i<=0xcba9;i++)
{
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))
J=r;
else
{
sprintf(buf,"%x",i);
if (buf!=buf && buf!=buf && buf!=buf && buf!=buf && buf!=buf && buf!=buf )
{
I=i;
J=r;
r++;
}
else
J=r;
}
}
r=0;
for(i=0;i<J;i++)
{
for(j=J+1];j<=J];j++)
{
k=I+I;
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;
sprintf(buf,"%x%x%x",I,I,k);
for(k=1;k<=12;k++)f=0;
for(k=1;k<=12;k++)
if(++f[(buf-'0'>0 && buf-'0'<=9)?buf-'0':buf-'a'+10]>1)goto a;
r++;
a:
;
}
}
t=clock()-t;
printf("%d,%d",r,t);
return 1;
}

无心人 发表于 2010-6-15 07:59:20

这题目用循环就能完成,而且保证不会取到重复数字

〇〇 发表于 2010-6-15 08:23:53

4.8s,觉得不改思路无法再优化了
#include <stdio.h>
#include <time.h>
unsigned short m1(unsigned short i)
{
        unsigned short a=i/0x1000%0x10+1;
        unsigned short b=i/0x100%0x10;
        unsigned short c=i/0x10%0x10;
        unsigned short d=i%0x10;
        unsigned short t=a;
        if(b==t)t=b+1;
        if(c==t)t=c+1;
        if(d==t)t=d+1;
        return t*0x1000;
}
unsigned short m(unsigned short in)
{
        int i,j;
        unsigned short t;
        char buf={0};
        buf=1;
        buf=1;
        buf=1;
        buf=1;
        t=0;//find highest digit
        for(i=in/0x1000%0x10;i<=0xc;i++)
        {
                if(buf==0)
                {
                        t=i;
                        buf=1;
                        break;
                }
        }
        if(t==0)
        return 0xcba9;
        for(i=1,j=0;i<=0xc&&j<3;i++)//find other digit
        if(buf==0)
        {t=(t<<4)+i;
        j++;
        }
        return t;
}
int main()
{
       
int i,j,k,r,t;
char buf;
char f;
unsigned short I;
unsigned short J;
r=0;
/*
printf("%x:%x\n",0x7234,m(0x7234));
printf("%x:%x\n",0x7235,m(0x7235));
printf("%x:%x\n",0x7238,m(0x7238));
printf("%x:%x\n",0x523a,m(0x523a));
printf("%x:%x\n",0x523b,m(0x523b));
printf("%x:%x\n",0x523c,m(0x523c));
return 1;
*/
t=clock();
for(i=0x1234;i<=0xcba9;i++)
{
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))
J=r;
else
{
sprintf(buf,"%x",i);
if (buf!=buf && buf!=buf && buf!=buf && buf!=buf && buf!=buf && buf!=buf )
{
I=i;
J=r;
r++;
}
else
J=r;
}
}
r=0;
for(i=0;i<J;i++)
{
for(j=J)];j<=J];j++)
{
k=I+I;
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;
sprintf(buf,"%x%x%x",I,I,k);
for(k=1;k<=12;k++)f=0;
for(k=1;k<=12;k++)
if(++f[(buf-'0'>0 && buf-'0'<=9)?buf-'0':buf-'a'+10]>1)goto a;
r++;
a:
;
}
}
t=clock()-t;
printf("%d,%d",r,t);
return 1;
}

〇〇 发表于 2010-6-16 07:28:51

newkid的递归解法,比c还快。
WITH n AS (
   SELECT ROWNUM n FROM DUAL CONNECT BY ROWNUM<=15
   )
,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<8
   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(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
      ;
COUNT(*)*8
----------
      3408

已用时间:00: 00: 01.35

hujunhua 发表于 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),

mathe 发表于 2010-6-17 12:33:32

这个可以按位从低位开始向高位搜索,而且两个加数对应位上的数字交换不改变结果,这个也可以减少一些搜索空间。
比如9个数字,那么两个加数最低位选择有$C_9^2=36$种不同选择,而其中有些选择可以直接淘汰,比如1+9,2+8,3+7,4+6,对于每种选择还可以直接计算出结果的最低位。然后接下去搜索两个加数的十位数,由于个位已经使用了3个数,余下有$C_6^2=15$种不同选择,而这时,根据结果的十位数估计至少可以淘汰掉一半;然后对于余下的继续搜索百位数,而搜索空间是一个树形的
页: 1 2 3 [4] 5 6
查看完整版本: 趣味题