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

[转载] 趣味题

[复制链接]
发表于 2010-6-13 09:52:48 | 显示全部楼层
楼上,为什么不是3个5位16进制呢?
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2010-6-13 10:26:29 | 显示全部楼层
如果是3个5位16进制数字,程序如下

  import List
  import Numeric (showHex)

  main = do
            print \$ zip [1..] [(x, y, z)|a<-[1..7], b<-[1..15]\\[a], c<-[1..15]\\[a, b], d<-[1..15]\\[a, b, c], e<-[1..15]\\[a, b, c, d],
                               let x1 = (16^4) * a + (16^3) * b + (16^2) * c + 16 * d + e,
                               let x = showHex x1 "",
                               f<-[a+1..15-a]\\[b, c, d, e], g<-[1..15]\\[a, b, c, d, e, f], h<-[1..15]\\[a, b, c, d, e, f, g],
                               i<-[1..15]\\[a, b, c, d, e, f, g, h], j<-[1..15]\\[a, b, c, d, e, f, g, h, i],
                               let y1 = (16^4) * f + (16^3) * g + (16^2) * h + 16 * i + j,
                               let y = showHex y1 "",
                               let z1 = x1 + y1, z1 < 16^5,
                               let z = showHex z1 "",
                               (==16) \$ length \$ nub \$ z ++ x ++ y ++ "0"
                               ]

评分

参与人数 1鲜花 +8 收起 理由
wayne + 8 xiexie,俺攒下了

查看全部评分

毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2010-6-13 10:37:18 | 显示全部楼层


几万个答案,被我终止了,中午没事再运行吧
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2010-6-13 11:28:24 | 显示全部楼层
5位太大,4位已经足够测试算法速度
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2010-6-13 11:31:48 | 显示全部楼层
P(12,4)=11880
P(15,5)=360360
差30倍
如果算上第一个和第二个数组合
1000倍

1秒变成15分钟
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2010-6-13 13:02:04 | 显示全部楼层

  1. SQL> insert into t select x,to_number(x,'fm0xxx')n from(SELECT REPLACE(SYS_CONNECT_BY_PATH(rn,','),',') x
  2.   2               FROM (SELECT to_char(ROWNUM,'fmx') rn FROM DUAL CONNECT BY ROWNUM<=12)
  3.   3              WHERE LEVEL=4
  4.   4              CONNECT BY NOCYCLE rn<> PRIOR rn AND LEVEL<=4
  5.   5            );

  6. 已创建11880行。

  7. 已用时间:  00: 00: 00.18
  8. SQL> commit;

  9. 提交完成。

  10. 已用时间:  00: 00: 00.00
  11. SQL> exec DBMS_STATS.GATHER_TABLE_STATS('LT','T');

  12. PL/SQL 过程已成功完成。

  13. 已用时间:  00: 00: 00.11
  14. SQL> set autot on
  15. SQL> select count(*) from(
  16.   2  select a.x||'+'||b.x||'='||to_char(a.n+b.n,'fm0xxx')s
  17.   3  from t a,t b --,t c
  18.   4  where a.n<to_number('6000','fm0xxx')
  19.   5        and a.n<b.n
  20.   6        AND a.n+b.n <=to_number('cba9','fm0xxx')
  21.   7        and translate('123456789abc','\$'||a.x||b.x||to_char(a.n+b.n,'fm0xxx'),'\$')is null
  22.   8        );

  23.   COUNT(*)
  24. ----------
  25.       3408

  26. 已用时间:  00: 01: 45.01
复制代码
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2010-6-13 15:35:50 | 显示全部楼层
我的代码怎么变成了数学公式?
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2010-6-14 07:42:07 | 显示全部楼层
我的代码怎么变成了数学公式?
〇〇 发表于 2010-6-13 15:35


在美元符前加“\”即可屏蔽被误识别成TeX数学公式(我已经帮你修改好了)。
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2010-6-14 13:12:06 | 显示全部楼层
22# 无心人
你知道J语言吗

一种特别精简,同时可读性特别差的语言。
感觉Haskell的可读性也不是很好(针对外行来说的),
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2010-6-14 14:08:26 | 显示全部楼层
过程式语言的写法,和SQL时间差不多

  1. create or replace procedure pc5
  2. as
  3.    time_before BINARY_INTEGER;
  4.    time_after BINARY_INTEGER;
  5.    TYPE t_num IS TABLE OF PLS_INTEGER INDEX BY  PLS_INTEGER ;
  6.    x t_num;
  7.    a t_num;
  8.    b t_num;
  9.    c PLS_INTEGER;
  10.    c1 PLS_INTEGER;
  11.    r PLS_INTEGER;
  12.    l varchar(4);
  13. begin
  14. c:=0;
  15. time_before := DBMS_UTILITY.GET_TIME;
  16. for i in to_number('1234','fm0xxx')..to_number('cba9','fm0xxx') loop
  17. l:=to_char(i,'fm0xxx');
  18. if substr(l,1,1)<>substr(l,2,1)and
  19. substr(l,2,1)<>substr(l,3,1)and
  20. substr(l,3,1)<>substr(l,4,1)and
  21. substr(l,1,1)<>substr(l,3,1)and
  22. substr(l,2,1)<>substr(l,4,1)and
  23. substr(l,1,1)<>substr(l,4,1)and
  24. instr(l,0)=0 and instr(l,'d')=0 and instr(l,'e')=0 and instr(l,'f')=0 then
  25. c:=c+1;
  26. if i<to_number('cba9','fm0xxx')/2 then
  27. a(c):=i;
  28. c1:=c;
  29. end if;
  30. b(c):=i;
  31. x(i):=c;
  32. else
  33. x(i):=c;
  34. end if;
  35. end loop;
  36. r:=0;
  37. dbms_output.put_line('c='||c||'c1='||c1||a(1)||b(1));
  38. FOR I IN 1 .. c1 LOOP
  39. FOR J IN x(a(i))+1 .. x(to_number('cba9','fm0xxx')-a(i)) LOOP
  40. --if translate('123456789abc','\$'||to_char(a(i),'fm0xxx')||to_char(b(j),'fm0xxx')||to_char(a(i)+b(j),'fm0xxx'),'\$')is null then
  41. --dbms_output.put_line(to_char(a(i),'fm0xxx')||to_char(b(j),'fm0xxx')||to_char(a(i)+b(j),'fm0xxx'));
  42. --NULL;
  43. l:=to_char(a(i)+b(j),'fm0xxx');
  44. if substr(l,1,1)<>substr(l,2,1)and
  45. substr(l,2,1)<>substr(l,3,1)and
  46. substr(l,3,1)<>substr(l,4,1)and
  47. substr(l,1,1)<>substr(l,3,1)and
  48. substr(l,2,1)<>substr(l,4,1)and
  49. substr(l,1,1)<>substr(l,4,1)and
  50. instr(l,0)=0 and instr(l,'d')=0 and instr(l,'e')=0 and instr(l,'f')=0
  51. --length(translate('0def','\$'||l,'\$'))=4
  52. and length(translate(to_char(a(i),'fm0xxx'),'\$'||l,'\$'))=4
  53. and length(translate(to_char(b(j),'fm0xxx'),'\$'||l,'\$'))=4
  54. and length(translate(to_char(a(i),'fm0xxx'),'\$'||to_char(b(j),'fm0xxx'),'\$'))=4 then
  55. r:=r+1;
  56. --dbms_output.put_line(to_char(a(i),'fm0xxx')||to_char(b(j),'fm0xxx')||to_char(a(i)+b(j),'fm0xxx'));
  57. end if;
  58. end loop;
  59. if r>=100 then
  60.   exit;
  61. end if;
  62. end loop;

  63. time_after := DBMS_UTILITY.GET_TIME;
  64. DBMS_OUTPUT.PUT_LINE ('output:'||(time_after - time_before)||'rows:'||r);
  65. end;
  66. /

复制代码
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
您需要登录后才可以回帖 登录 | 欢迎注册

本版积分规则

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

GMT+8, 2024-5-3 02:10 , Processed in 0.044168 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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