楼主: 〇〇

[精华] puzzleup 2011

[复制链接]
论坛徽章:
519
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
41#
发表于 2011-9-8 00:07 | 只看该作者
第6题和第一题有些相似。


#7: Close and Different

Use all the nine numerals from 1 to 9 once and form four numbers (A, B, C, D) such that divisions A/B and C/D produce different but closest results. (ie. Absolute value of A/B-C/D should be minimum.)

Please enter the four numbers from the smallest to the largest, with commas between them.

Example: If the numbers were 12/789, 6/345, your answer should read 6,12,345,789

利用1-9构成四个数ABCD使得 A/B-C/D 的绝对值最小。

假如用暴力法共有 9! * (1*6+2*5+3*4+4*3+5*2+6*1) = 20321280 次计算,似乎也不是不可能。可以想办法再排除一些对称的排列。

使用道具 举报

回复
论坛徽章:
484
ITPUB北京香山2007年会纪念徽章
日期:2007-01-24 14:35:02ITPUB北京九华山庄2008年会纪念徽章
日期:2008-01-21 16:50:24ITPUB北京2009年会纪念徽章
日期:2009-02-09 11:42:452010新春纪念徽章
日期:2010-03-01 11:04:552010数据库技术大会纪念徽章
日期:2010-05-13 10:04:272010系统架构师大会纪念
日期:2010-09-04 13:35:54ITPUB9周年纪念徽章
日期:2010-10-08 09:28:512011新春纪念徽章
日期:2011-02-18 11:43:32ITPUB十周年纪念徽章
日期:2011-11-01 16:19:412012新春纪念徽章
日期:2012-01-04 11:49:54
42#
发表于 2011-9-8 01:26 | 只看该作者
原帖由 newkid 于 11-8-11 22:37 发表
奥数哥很久不来了,要不然请他指导一下。去年他说要研究哥德巴赫猜想,不知道搞出来没有,报纸上也没见报道。


我承认我笑了

使用道具 举报

回复
论坛徽章:
484
ITPUB北京香山2007年会纪念徽章
日期:2007-01-24 14:35:02ITPUB北京九华山庄2008年会纪念徽章
日期:2008-01-21 16:50:24ITPUB北京2009年会纪念徽章
日期:2009-02-09 11:42:452010新春纪念徽章
日期:2010-03-01 11:04:552010数据库技术大会纪念徽章
日期:2010-05-13 10:04:272010系统架构师大会纪念
日期:2010-09-04 13:35:54ITPUB9周年纪念徽章
日期:2010-10-08 09:28:512011新春纪念徽章
日期:2011-02-18 11:43:32ITPUB十周年纪念徽章
日期:2011-11-01 16:19:412012新春纪念徽章
日期:2012-01-04 11:49:54
43#
发表于 2011-9-8 01:31 | 只看该作者
原帖由 newkid 于 11-9-8 00:07 发表
第6题和第一题有些相似。


#7: Close and Different

Use all the nine numerals from 1 to 9 once and form four numbers (A, B, C, D) such that divisions A/B and C/D produce different but closest results. (ie. Absolute value of A/B-C/D should be minimum.)

Please enter the four numbers from the smallest to the largest, with commas between them.

Example: If the numbers were 12/789, 6/345, your answer should read 6,12,345,789

利用1-9构成四个数ABCD使得 A/B-C/D 的绝对值最小。

假如用暴力法共有 9! * (1*6+2*5+3*4+4*3+5*2+6*1) = 20321280 次计算,似乎也不是不可能。可以想办法再排除一些对称的排列。


A/B和 C/D要尽可能的接近,由于是求最小的绝对值,所以可以假设 A/B一定稍微微微的大于C/D
A/B>C/D => A*D > B*C ,然后呢 A*D和B*C尽可能的接近

使用道具 举报

回复
论坛徽章:
519
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
44#
发表于 2011-9-8 01:54 | 只看该作者
很不雅的暴力法:
WITH digits AS (
SELECT CAST(REPLACE(SYS_CONNECT_BY_PATH(lv,','),',') AS VARCHAR2(9)) AS str
  FROM (SELECT LEVEL lv FROM DUAL CONNECT BY LEVEL<=9)
WHERE LEVEL=9
CONNECT BY NOCYCLE PRIOR lv<>lv
)
SELECT SUBSTR(str,1,length_A) A
      ,SUBSTR(str,length_A+1,length_AB-length_A) B
      ,SUBSTR(str,length_AB+1,length_C) C
      ,SUBSTR(str,length_AB+length_C+1) D
  FROM ( SELECT *
           FROM digits
               ,(SELECT 2 AS length_AB FROM DUAL UNION ALL SELECT 3 FROM DUAL UNION ALL SELECT 4 FROM DUAL)
               ,(SELECT LEVEL length_A FROM DUAL CONNECT BY LEVEL<=3)
               ,(SELECT LEVEL length_C FROM DUAL CONNECT BY LEVEL<=6)
         WHERE length_A<length_AB AND length_C<9-length_AB
               AND SUBSTR(str,1,length_A)/SUBSTR(str,length_A+1,length_AB-length_A)
                  <>SUBSTR(str,length_AB+1,length_C)/SUBSTR(str,length_AB+length_C+1)
         ORDER BY ABS(SUBSTR(str,1,length_A)/SUBSTR(str,length_A+1,length_AB-length_A)
                  -SUBSTR(str,length_AB+1,length_C)/SUBSTR(str,length_AB+length_C+1)
                   )
        )
WHERE ROWNUM=1
;

A         B         C      D
--------  --------- ------ ---------
1         826       9      7435

Elapsed: 00:01:09.20

由于对称的关系可以假设AB的长度小于CD的长度,砍掉一半。

使用道具 举报

回复
论坛徽章:
548
生肖徽章2007版:猴
日期:2008-05-16 11:28:59生肖徽章2007版:马
日期:2008-10-08 17:01:01SQL大赛参与纪念
日期:2011-04-13 12:08:17授权会员
日期:2011-06-17 16:14:53ITPUB元老
日期:2011-06-21 11:47:01ITPUB官方微博粉丝徽章
日期:2011-07-01 09:45:27ITPUB十周年纪念徽章
日期:2011-09-27 16:30:472012新春纪念徽章
日期:2012-01-04 11:51:222012新春纪念徽章
日期:2020-11-30 22:13:24海蓝宝石
日期:2012-02-20 19:24:27
45#
发表于 2011-9-8 18:33 | 只看该作者
with t as ( select replace(sys_connect_by_path(rn,'/'),'/','') expression
              from (select rownum rn from dual connect by level <=9)
             where level=9
           connect by nocycle rn <> prior rn
          ),
     t1 as (              
          select expression,
                 minus_sign,
                 substr(expression,1,minus_sign) AB,
                 substr(expression,minus_sign+1) CD
           from t,
               (select rownum +1 minus_sign from dual connect by level <=6) a
           ),
     t2 as (
            select expression,minus_sign,first_div
            from t1,
                 (select rownum first_div
                    from dual
                 connect by level <= 7) b
            where minus_sign > first_div
           ),
     t3 as (
            select expression,minus_sign,first_div,second_div,
                   substr(substr(expression,1,minus_sign),1,first_div)     A,
                   substr(substr(expression,1,minus_sign),first_div +  1)  B,
                   substr(substr(expression,minus_sign+1),1,second_div)    C,
                   substr(substr(expression,minus_sign+1),second_div + 1)  D
              from t2,
                   (select rownum second_div
                      from dual
                   connect by level <= 6) c
            where  minus_sign + second_div < 9
           )
select A,B,C,D from t3 WHERE abs(A/B - C/D) = 0
              
全暴力,无裁剪: 绝对值最小为0,共472组。

986        2        3451        7
986        3451        2        7
984        123        56        7
984        56        123        7
98        14        532        76
98        532        14        76
98        1372        4        56
98        4        1372        56
96        12        584        73
96        584        12        73
96        174        32        58
96        32        174        58
96        1824        3        57
96        3        1824        57
96        1728        3        54
96        3        1728        54
954        1        7632        8
954        7632        1        8
953        1        7624        8
953        7624        1        8
942        7536        1        8
942        1        7536        8
932        7456        1        8
932        1        7456        8
93        186        27        54
93        27        186        54
927        3        1854        6
927        1854        3        6
92        4876        1        53
92        1        4876        53
91        2        3458        76
91        3458        2        76
9        7524        1        836
9        1        7524        836
9        153        46        782
9        46        153        782
9        327        18        654
9        18        327        654
9        1        5823        647
9        5823        1        647
9        1        5742        638
9        5742        1        638
9        1746        3        582
9        3        1746        582
9        18        273        546
9        273        18        546
9        267        18        534
9        18        267        534
9        28        153        476
9        153        28        476
897        156        23        4
897        23        156        4
87        1        5394        62
87        5394        1        62
836        1        7524        9
836        7524        1        9
8        1        7632        954
8        7632        1        954
8        1        7624        953
8        7624        1        953
8        1        7536        942
8        7536        1        942
8        1        7456        932
8        7456        1        932
8        6352        1        794
8        1        6352        794
8        46        132        759
8        132        46        759
8        1        5936        742
8        5936        1        742
8        5432        1        679
8        1        5432        679
8        5392        1        674
8        1        5392        674
8        4736        1        592
8        1        4736        592
8        4632        1        579
8        1        4632        579
8        1        4296        537
8        4296        1        537
8        1        3752        469
8        3752        1        469
8        3672        1        459
8        136        27        459
8        1        3672        459
8        27        136        459
8        2        1756        439
8        1756        2        439
8        2        1576        394
8        1576        2        394
794        1        6352        8
794        6352        1        8
793        1586        2        4
793        2        1586        4
792        3        1584        6
792        1584        3        6
79        158        32        64
79        32        158        64
79        158        23        46
79        23        158        46
782        153        46        9
782        46        153        9
769        1538        2        4
769        2        1538        4
7632        954        8        1
7632        8        954        1
7624        953        8        1
7624        8        953        1
76        14        532        98
76        532        14        98
76        2        3458        91
76        3458        2        91
759        132        46        8
759        46        132        8
7536        8        942        1
7536        942        8        1
7524        836        9        1
7524        9        836        1
7456        8        932        1
7456        932        8        1
742        1        5936        8
742        5936        1        8
74        18        259        63
74        259        18        63
73        12        584        96
73        584        12        96
73        29        146        58
73        146        29        58
729        1458        3        6
729        3        1458        6
7        3451        2        986
7        2        3451        986
7        56        123        984
7        123        56        984
7        329        14        658
7        14        329        658
7        154        29        638
7        29        154        638
7        14        293        586
7        293        14        586
7        14        269        538
7        269        14        538
7        2        1869        534
7        1869        2        534
69        23        174        58
69        174        23        58
69        138        27        54
69        27        138        54
679        1        5432        8
679        5432        1        8
679        1358        2        4
679        2        1358        4
674        1        5392        8
674        5392        1        8
67        1943        2        58
67        134        29        58
67        29        134        58
67        2        1943        58
658        14        329        7
658        329        14        7
654        18        327        9
654        327        18        9
647        1        5823        9
647        5823        1        9
64        158        32        79
64        32        158        79
638        1        5742        9
638        5742        1        9
638        154        29        7
638        29        154        7
6352        794        8        1
6352        8        794        1
63        18        259        74
63        259        18        74
62        5394        1        87
62        1        5394        87
6        1854        3        927
6        3        1854        927
6        1584        3        792
6        3        1584        792
6        1458        3        729
6        3        1458        729
6        2        1749        583
6        1749        2        583
5936        742        8        1
5936        8        742        1
592        1        4736        8
592        4736        1        8
586        14        293        7
586        293        14        7
584        73        96        12
584        96        73        12
583        1749        2        6
583        2        1749        6
5823        9        647        1
5823        647        9        1
582        3        1746        9
582        1746        3        9
58        174        32        96
58        32        174        96
58        146        29        73
58        29        146        73
58        174        23        69
58        23        174        69
58        29        134        67
58        134        29        67
58        2        1943        67
58        1943        2        67
579        1        4632        8
579        4632        1        8
5742        638        9        1
5742        9        638        1
57        1824        3        96
57        3        1824        96
56        4        1372        98
56        1372        4        98
56        984        7        123
56        7        984        123
546        18        273        9
546        273        18        9
5432        679        8        1
5432        8        679        1
54        1728        3        96
54        3        1728        96
54        186        27        93
54        27        186        93
54        138        27        69
54        27        138        69
5394        62        87        1
5394        87        62        1
5392        674        8        1
5392        8        674        1
538        14        269        7
538        269        14        7
537        4296        1        8
537        1        4296        8
534        18        267        9
534        267        18        9
534        1869        2        7
534        2        1869        7
532        76        98        14
532        98        76        14
53        1        4876        92
53        4876        1        92
4876        92        53        1
4876        53        92        1
476        153        28        9
476        28        153        9
4736        592        8        1
4736        8        592        1
469        3752        1        8
469        1        3752        8
4632        579        8        1
4632        8        579        1
46        23        158        79
46        158        23        79
46        9        782        153
46        782        9        153
46        8        759        132
46        759        8        132
459        1        3672        8
459        136        27        8
459        27        136        8
459        3672        1        8
439        2        1756        8
439        1756        2        8
4296        8        537        1
4296        537        8        1
4        23        156        897
4        156        23        897
4        1586        2        793
4        2        1586        793
4        1538        2        769
4        2        1538        769
4        1358        2        679
4        2        1358        679
4        98        56        1372
4        56        98        1372
394        2        1576        8
394        1576        2        8
3752        8        469        1
3752        469        8        1
3672        459        8        1
3672        8        459        1
3458        91        76        2
3458        76        91        2
3451        986        7        2
3451        7        986        2
329        7        658        14
329        658        7        14
327        9        654        18
327        654        9        18
32        58        96        174
32        96        58        174
32        79        64        158
32        64        79        158
3        6        927        1854
3        927        6        1854
3        96        57        1824
3        57        96        1824
3        9        582        1746
3        582        9        1746
3        96        54        1728
3        54        96        1728
3        6        792        1584
3        792        6        1584
3        729        6        1458
3        6        729        1458
293        7        586        14
293        586        7        14
29        638        7        154
29        7        638        154
29        58        73        146
29        73        58        146
29        58        67        134
29        67        58        134
28        9        476        153
28        476        9        153
273        9        546        18
273        546        9        18
27        54        93        186
27        93        54        186
27        54        69        138
27        69        54        138
27        8        459        136
27        459        8        136
269        538        7        14
269        7        538        14
267        9        534        18
267        534        9        18
259        63        74        18
259        74        63        18
23        69        58        174
23        58        69        174
23        46        79        158
23        79        46        158
23        4        897        156
23        897        4        156
2        76        91        3458
2        91        76        3458
2        986        7        3451
2        7        986        3451
2        58        67        1943
2        67        58        1943
2        534        7        1869
2        7        534        1869
2        439        8        1756
2        8        439        1756
2        583        6        1749
2        6        583        1749
2        793        4        1586
2        4        793        1586
2        8        394        1576
2        394        8        1576
2        4        769        1538
2        769        4        1538
2        679        4        1358
2        4        679        1358
1943        67        58        2
1943        58        67        2
1869        534        7        2
1869        7        534        2
186        93        54        27
186        54        93        27
1854        927        6        3
1854        6        927        3
1824        57        96        3
1824        96        57        3
18        654        9        327
18        9        654        327
18        546        9        273
18        9        546        273
18        534        9        267
18        9        534        267
18        74        63        259
18        63        74        259
1756        439        8        2
1756        8        439        2
1749        583        6        2
1749        6        583        2
1746        582        9        3
1746        9        582        3
174        96        58        32
174        58        96        32
174        58        69        23
174        69        58        23
1728        54        96        3
1728        96        54        3
1586        4        793        2
1586        793        4        2
1584        6        792        3
1584        792        6        3
158        64        79        32
158        79        64        32
158        46        79        23
158        79        46        23
1576        394        8        2
1576        8        394        2
156        4        897        23
156        897        4        23
154        638        7        29
154        7        638        29
1538        769        4        2
1538        4        769        2
153        782        9        46
153        9        782        46
153        476        9        28
153        9        476        28
146        58        73        29
146        73        58        29
1458        6        729        3
1458        729        6        3
14        76        98        532
14        98        76        532
14        7        658        329
14        658        7        329
14        586        7        293
14        7        586        293
14        7        538        269
14        538        7        269
138        69        54        27
138        54        69        27
1372        98        56        4
1372        56        98        4
136        8        459        27
136        459        8        27
1358        4        679        2
1358        679        4        2
134        58        67        29
134        67        58        29
132        8        759        46
132        759        8        46
123        7        984        56
123        984        7        56
12        96        73        584
12        73        96        584
1        954        8        7632
1        8        954        7632
1        8        953        7624
1        953        8        7624
1        942        8        7536
1        8        942        7536
1        9        836        7524
1        836        9        7524
1        932        8        7456
1        8        932        7456
1        794        8        6352
1        8        794        6352
1        8        742        5936
1        742        8        5936
1        647        9        5823
1        9        647        5823
1        9        638        5742
1        638        9        5742
1        679        8        5432
1        8        679        5432
1        87        62        5394
1        62        87        5394
1        8        674        5392
1        674        8        5392
1        53        92        4876
1        92        53        4876
1        592        8        4736
1        8        592        4736
1        8        579        4632
1        579        8        4632
1        537        8        4296
1        8        537        4296
1        8        469        3752
1        469        8        3752
1        8        459        3672
1        459        8        3672

使用道具 举报

回复
论坛徽章:
519
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
46#
发表于 2011-9-8 22:08 | 只看该作者
原作者要求 "different" but closest results 也就是绝对值不能为零,我忘记把different翻译出来了。

使用道具 举报

回复
论坛徽章:
548
生肖徽章2007版:猴
日期:2008-05-16 11:28:59生肖徽章2007版:马
日期:2008-10-08 17:01:01SQL大赛参与纪念
日期:2011-04-13 12:08:17授权会员
日期:2011-06-17 16:14:53ITPUB元老
日期:2011-06-21 11:47:01ITPUB官方微博粉丝徽章
日期:2011-07-01 09:45:27ITPUB十周年纪念徽章
日期:2011-09-27 16:30:472012新春纪念徽章
日期:2012-01-04 11:51:222012新春纪念徽章
日期:2020-11-30 22:13:24海蓝宝石
日期:2012-02-20 19:24:27
47#
发表于 2011-9-9 10:20 | 只看该作者
SQL> with t as ( select replace(sys_connect_by_path(rn,'/'),'/','') expression
  2                from (select rownum rn from dual connect by level <=9)
  3               where level=9
  4             connect by nocycle rn <> prior rn
  5            ),
  6       t1 as (
  7            select expression,
  8                   minus_sign
  9             from t,
10                 (select rownum +1 minus_sign from dual connect by level <=6) a
11             ),
12       t2 as (
13              select expression,minus_sign,first_div
14              from t1,
15                   (select rownum first_div
16                      from dual
17                   connect by level <= 7) b
18              where minus_sign > first_div
19             ),
20       t3 as (
21              select expression,minus_sign,first_div,second_div,
22                     substr(substr(expression,1,minus_sign),1,first_div)     A,
23                     substr(substr(expression,1,minus_sign),first_div +  1)  B,
24                     substr(substr(expression,minus_sign+1),1,second_div)    C,
25                     substr(substr(expression,minus_sign+1),second_div + 1)  D
26                from t2,
27                     (select rownum second_div
28                        from dual
29                     connect by level <= 6) c
30              where  minus_sign + second_div < 9
31             ),
32      t4 as (
33             select * from t3 where  A*D > B*C
34            )
35  select A,B,C,D,A/B - C/D
36    from t4
37   where A/B - C/D = (select min(A/B - C/D) from t4 )
38  /

A        B         C       D      A/B-C/D
-------- --------- ------- ------ ---------------------
1        826       9       7435   1.62831708544268E-7

3min 45s...

使用道具 举报

回复
论坛徽章:
22
ITPUB十周年纪念徽章
日期:2011-11-01 16:21:152014年世界杯参赛球队: 比利时
日期:2014-06-15 20:40:142014年世界杯参赛球队: 科特迪瓦
日期:2014-06-30 19:29:262014年世界杯参赛球队:西班牙
日期:2014-07-08 21:49:56ITPUB元老
日期:2014-08-04 21:10:48优秀写手
日期:2014-09-24 06:00:13itpub13周年纪念徽章
日期:2014-10-03 10:51:25itpub13周年纪念徽章
日期:2014-10-08 15:16:50itpub13周年纪念徽章
日期:2014-10-08 15:16:50itpub13周年纪念徽章
日期:2014-10-08 15:16:50
48#
发表于 2011-9-9 14:41 | 只看该作者
继续膜拜下,哈哈

使用道具 举报

回复
论坛徽章:
519
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
49#
发表于 2011-9-16 21:42 | 只看该作者
这个贴竟然从前天开始丢,幸亏我的浏览器缓存里还有:
#8:
Using all of the numbers once from 1 to 16, 4x4 magic squares are formed. What is the possible maximum value of the sum of four numbers in any 2x2 block on this 4x4 square.
Note: In a magic square, the numbers in all rows, all columns, and both diagonals sum to the same constant.

1-16排成4X4幻方,在这个4X4方阵中所有2X2小方阵之和的最大值是多少?
在一个幻方中所有行、列、两条对角线之和都相等。

这个难度不大。



__________________

世界上只有两种编程方法:Oracle的方法和错误的方法。

剑破冰山—Oracle开发艺术 即将隆重推出
http://www.china-pub.com/197199
http://www.huachu.com.cn/itbook/itbookinfo.asp?lbbh=10114321
只看该作者              引用  回复  报告  编辑   

newkid
资深新手
老程序员



来自 银河系
精华贴数 15
个人空间 0
技术积分 20742 (73)
社区积分 5 (22147)
注册日期 2004-6-26
论坛徽章:66                                       
                                       

                #50
使用道具  
发表于 2011-9-15 09:19  
四阶幻方:
WITH nums AS (SELECT LEVEL n, POWER(2,LEVEL-1) bit FROM DUAL CONNECT BY LEVEL<=16)
,num4 AS (
SELECT n1.n n1,n2.n n2,n3.n n3,n4.n n4
       ,n1.bit+n2.bit+n3.bit+n4.bit bit
   FROM nums n1,nums n2,nums n3,nums n4
WHERE n1.n+n2.n+n3.n+n4.n=34
        AND n1.n NOT IN (n2.n,n3.n,n4.n)
        AND n2.n NOT IN (n3.n,n4.n)
        AND n3.n<>n4.n
)
,num8 AS (
SELECT n1.n1,n1.n2,n1.n3,n1.n4,n2.n1 n5,n2.n2 n6,n2.n3 n7,n2.n4 n8
       ,n1.n1+n2.n1 s1
       ,n1.n2+n2.n2 s2
       ,n1.n3+n2.n3 s3
       ,n1.n4+n2.n4 s4
       ,n1.bit+n2.bit bit
       ,n1.n1+n2.n2 d1
       ,n1.n4+n2.n3 d2
       ,n1.n3+n2.n4 d3
       ,n1.n2+n2.n1 d4
   FROM num4 n1,num4 n2
WHERE BITAND(n1.bit,n2.bit)=0
)
,num16 AS (
SELECT *
   FROM num8 n1,num8 n2
WHERE n1.bit = POWER(2,16)-1-n2.bit
        AND n1.s1=34 - n2.s1
        AND n1.s2=34 - n2.s2
        AND n1.s3=34 - n2.s3
        AND n1.s4=34 - n2.s4
        AND n1.d1=34 - n2.d3
        AND n1.d2=34 - n2.d4
)
SELECT count(*) FROM num16;

   COUNT(*)
----------
       7040

Elapsed: 00:00:10.62

剩下的就很好办了,明天继续。



__________________

世界上只有两种编程方法:Oracle的方法和错误的方法。

剑破冰山—Oracle开发艺术 即将隆重推出
http://www.china-pub.com/197199
http://www.huachu.com.cn/itbook/itbookinfo.asp?lbbh=10114321

使用道具 举报

回复
论坛徽章:
519
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
50#
发表于 2011-9-16 21:45 | 只看该作者
solomon_007
所罗门



精华贴数 2
个人空间 0
技术积分 1766 (1395)
社区积分 35 (11109)
注册日期 2006-5-14
论坛徽章:24                                       
                                       

                #51
使用道具  
发表于 2011-9-15 13:45  
SQL> with t as (
   2             select
   3                    substr(str,instr(str,'/',1, 1)+1,instr(str,'/',1, 2)-instr(str,'/',1, 1) -1) c11,
   4                    substr(str,instr(str,'/',1, 2)+1,instr(str,'/',1, 3)-instr(str,'/',1, 2) -1) c12,
   5                    substr(str,instr(str,'/',1, 3)+1,instr(str,'/',1, 4)-instr(str,'/',1, 3) -1) c13,
   6                    substr(str,instr(str,'/',1, 4)+1,instr(str,'/',1, 5)-instr(str,'/',1, 4) -1) c21,
   7                    substr(str,instr(str,'/',1, 5)+1,instr(str,'/',1, 6)-instr(str,'/',1, 5) -1) c22,
   8                    substr(str,instr(str,'/',1, 6)+1,instr(str,'/',1, 7)-instr(str,'/',1, 6) -1) c23,
   9                    substr(str,instr(str,'/',1, 7)+1,instr(str,'/',1, 8)-instr(str,'/',1, 7) -1) c31,
10                    substr(str,instr(str,'/',1, 8)+1,instr(str,'/',1, 9)-instr(str,'/',1, 8) -1) c32,
11                    substr(str,instr(str,'/',1, 9)+1,instr(str,'/',1,10)-instr(str,'/',1, 9) -1) c33
12              from (
13                    select sys_connect_by_path(rn,'/')||'/' str
14                      from (select rownum rn from dual connect by level <=9)
15                     where level=9
16                     connect by nocycle rn <> prior rn
17                    )
18            )
19  select *
20    from t
21   where c11 + c12 + c13 = c21 + c22 + c23
22     and c11 + c12 + c13 = c31 + c32 + c33
23     and c11 + c12 + c13 = c11 + c21 + c31
24     and c11 + c12 + c13 = c12 + c22 + c32
25     and c11 + c12 + c13 = c13 + c23 + c33
26     and c11 + c12 + c13 = c11 + c22 + c33
27     and c11 + c12 + c13 = c31 + c22 + c13
28  /



C11 C12 C13 C21 C22 C23 C31 C32 C33
--- --- --- --- --- --- --- --- ---
2   7   6   9   5   1   4   3   8
2   9   4   7   5   3   6   1   8
4   3   8   9   5   1   2   7   6
4   9   2   3   5   7   8   1   6
6   1   8   7   5   3   2   9   4
6   7   2   1   5   9   8   3   4
8   1   6   3   5   7   4   9   2
8   3   4   1   5   9   6   7   2


我上面的方法只能找3阶幻方,4阶 16! 这个太大了,跑不出来。。。



__________________
猫言猫语:
              人生无非是笑笑人家,再被人家笑笑而已.
              男人是用来靠的,所以要可靠;女人是用来爱的,所以要可爱.
              发怒一分钟便失去六十秒的幸福.
              人品就象内裤,表面看不出来,如果你没有还显摆,就挺讨厌.
              ......
只看该作者              引用  回复  报告  编辑   

solomon_007
所罗门



精华贴数 2
个人空间 0
技术积分 1766 (1395)
社区积分 35 (11109)
注册日期 2006-5-14
论坛徽章:24                                       
                                       

                #52
使用道具  
发表于 2011-9-15 16:29  
看明白了, NEWKID 用的是每4个=34的数据为单位进行填充的,4 join 4 得8 ,8 join 8 得16 ,高!



__________________
猫言猫语:
              人生无非是笑笑人家,再被人家笑笑而已.
              男人是用来靠的,所以要可靠;女人是用来爱的,所以要可爱.
              发怒一分钟便失去六十秒的幸福.
              人品就象内裤,表面看不出来,如果你没有还显摆,就挺讨厌.
              ......
只看该作者              引用  回复  报告  编辑   

〇〇



精华贴数 10
个人空间 0
技术积分 49719 (25)
社区积分 9647 (262)
注册日期 2008-1-16
论坛徽章:229                                       
                                       

                #53
使用道具  
发表于 2011-9-15 16:42  


QUOTE:
原帖由 newkid 于 2011-9-14 22:03 发表
#8:
Using all of the numbers once from 1 to 16, 4x4 magic squares are formed. What is the possible maximum value of the sum of four numbers in any 2x2 block on this 4x4 square.
Note: In a magic square, the numbers in all rows, all columns, and both diagonals sum to the same constant.

1-16排成4X4幻方,在这个4X4方阵中所有2X2小方阵之和的最大值是多少?
在一个幻方中所有行、列、两条对角线之和都相等。

这个难度不大。

所有2X2小方阵之和的最大值:是否需要列举所有的幻方才能,还是有什么理论



__________________
我的新浪微博,欢迎大家加我:http://weibo.com/lu01


剑破冰山—Oracle开发艺术 已经上架销售
网购地址:互动|京东电子工业出版社书店卓越亚马逊当当华储
在线阅读:5lcto华储
源代码:博文视点ITPUB
只看该作者              引用  回复  报告  编辑   

solomon_007
所罗门



精华贴数 2
个人空间 0
技术积分 1766 (1395)
社区积分 35 (11109)
注册日期 2006-5-14
论坛徽章:24                                       
                                       

                #54
使用道具  
发表于 2011-9-15 17:20  
采用newkid的:

with nums  as (select level n, power(2,level-1) bit from dual connect by level<=16),
      num4  as (
                 select n1.n n1,
                        n2.n n2,
                        n3.n n3,
                        n4.n n4,
                        n1.bit+n2.bit+n3.bit+n4.bit bit
                   from nums n1,
                        nums n2,
                        nums n3,
                        nums n4
                 where n1.n+n2.n+n3.n+n4.n=34
                   and n1.n not in (n2.n,n3.n,n4.n)
                   and n2.n not in (n3.n,n4.n)
                   and n3.n<>n4.n
                ),
      num8 as (
               select n1.n1,n1.n2,n1.n3,n1.n4,n2.n1 n5,n2.n2 n6,n2.n3 n7,n2.n4 n8
                     ,n1.n1+n2.n1 s1
                     ,n1.n2+n2.n2 s2
                     ,n1.n3+n2.n3 s3
                     ,n1.n4+n2.n4 s4
                     ,n1.bit+n2.bit bit
                     ,n1.n1+n2.n2 d1
                     ,n1.n4+n2.n3 d2
                     ,n1.n3+n2.n4 d3
                     ,n1.n2+n2.n1 d4
                 from num4 n1,
                      num4 n2
               where bitand(n1.bit,n2.bit)=0
              ),
     num16 as (
               select n1.n1 c11,
                      n1.n2 c12,
                      n1.n3 c13,
                      n1.n4 c14,
                      n1.n5 c21,
                      n1.n6 c22,
                      n1.n7 c23,
                      n1.n8 c24,
                      n2.n1 c31,
                      n2.n2 c32,
                      n2.n3 c33,
                      n2.n4 c34,
                      n2.n5 c41,
                      n2.n6 c42,
                      n2.n7 c43,
                      n2.n8 c44  
                 from num8 n1,
                      num8 n2
                where n1.bit = power(2,16) - 1 - n2.bit
                  and n1.s1 = 34 - n2.s1
                  and n1.s2 = 34 - n2.s2
                  and n1.s3 = 34 - n2.s3
                  and n1.s4 = 34 - n2.s4
                  and n1.d1 = 34 - n2.d3
                  and n1.d2 = 34 - n2.d4
               ),
       res as (
               select c11,c12,c13,c14,c21,c22,c23,c24,c31,c32,c33,c34,c41,c42,c43,c44,
                      (c11+c12+c21+c22)+(c12+c13+c22+c23)+(c13+c14+c23+c24)
                     +(c21+c22+c31+c32)+(c22+c23+c32+c33)+(c23+c24+c33+c34)
                     +(c31+c32+c41+c42)+(c32+c33+c42+c43)+(c33+c34+c43+c44) s
                 from num16     
              )   
select *
from (select t.*,dense_rank() over(order by s) rk from res t)
where rk = 1                           
;


1        8        11        6        9        12        7        10        5        1        2        15        16        13        14        3        4        306        1
2        7        6        11        10        14        9        8        3        1        4        13        16        12        15        2        5        306        1
3        12        9        8        5        6        7        10        11        1        4        13        16        15        14        3        2        306        1
4        8        5        12        9        10        11        6        7        1        4        13        16        15        14        3        2        306        1
5        12        9        6        7        8        5        10        11        1        4        15        14        13        16        3        2        306        1
6        8        5        10        11        12        9        6        7        1        4        15        14        13        16        3        2        306        1
7        10        11        7        6        8        5        9        12        1        4        16        13        15        14        2        3
                 306        1

.......

(7040 组 )

是定值,恒等于306

[ 本帖最后由 solomon_007 于 2011-9-15 17:23 编辑 ]



__________________
猫言猫语:
              人生无非是笑笑人家,再被人家笑笑而已.
              男人是用来靠的,所以要可靠;女人是用来爱的,所以要可爱.
              发怒一分钟便失去六十秒的幸福.
              人品就象内裤,表面看不出来,如果你没有还显摆,就挺讨厌.
              ......
只看该作者              引用  回复  报告  编辑   

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表