用sql模拟:从12个乒乓球中找到假乒乓球

有12个乒乓球,其中一个是假的,其余的都是真的,用天平秤来称重,你能用此天平秤秤3次,就能分出哪一个乒乓球是假的?并且假乒乓球比真的轻还是重?

--创建测试表,测试表中一行代表一种可能

点击(此处)折叠或打开
  1. create table scott.test as select *
  2. from ( select case when rownum=1 then 1 else 2 end a,
  3.     case when rownum=2 then 1 else 2 end b,
  4.     case when rownum=3 then 1 else 2 end c,
  5.     case when rownum=4 then 1 else 2 end d,
  6.     case when rownum=5 then 1 else 2 end e,
  7.     case when rownum=6 then 1 else 2 end f,
  8.     case when rownum=7 then 1 else 2 end g,
  9.     case when rownum=8 then 1 else 2 end h,
  10.     case when rownum=9 then 1 else 2 end i,
  11.     case when rownum=10 then 1 else 2 end j,
  12.     case when rownum=11 then 1 else 2 end k,
  13.     case when rownum=12 then 1 else 2 end l
  14. from dual connect by rownum<=12
  15.  union all
  16.  select case when rownum=1 then 3 else 2 end a,
  17.     case when rownum=2 then 3 else 2 end b,
  18.     case when rownum=3 then 3 else 2 end c,
  19.     case when rownum=4 then 3 else 2 end d,
  20.     case when rownum=5 then 3 else 2 end e,
  21.     case when rownum=6 then 3 else 2 end f,
  22.     case when rownum=7 then 3 else 2 end g,
  23.     case when rownum=8 then 3 else 2 end h,
  24.     case when rownum=9 then 3 else 2 end i,
  25.     case when rownum=10 then 3 else 2 end j,
  26.     case when rownum=11 then 3 else 2 end k,
  27.     case when rownum=12 then 3 else 2 end l
  28. from dual connect by rownum<=12) a;
--找出假乒乓球

点击(此处)折叠或打开

  1. select case
  2.     when a+b+c+d>e+f+g+h then --第1步
  3.         case when a+i+j+k>e+b+c+d --第2步
  4.         then
  5.             case when a!=i then ('a重') --第3步
  6.                 when a=i then ('e轻') end
  7.         when a+i+j+k=e+b+c+d --第2步
  8.         then
  9.             case when f>g then ('g轻') --第3步
  10.                  when f=g then ('h轻')
  11.                  when f<g then ('f轻') end
  12.         when a+i+j+k<e+b+c+d --第2步
  13.         then
  14.             case when b>c then ('b重') --第3步
  15.                  when b=c then ('d重')
  16.                  when b<c then ('c重') end end
  17.     when a+b+c+d=e+f+g+h --第1步
  18.     then
  19.         case when a+i+j+k>e+b+c+d --第2步
  20.         then
  21.             case when i>j then ('i重') --第3步
  22.             when i=j then ('k重')
  23.             when i<j then ('j重') end
  24.         when a+i+j+k=e+b+c+d --第2步
  25.         then
  26.             case when a>l then ('l轻') --第3步
  27.             when a<l then ('l重') end
  28.         when a+i+j+k<e+b+c+d --第2步
  29.         then
  30.             case when i>j then ('j轻') --第3步
  31.             when i=j then ('k轻')
  32.             when i<j then ('i轻') end end
  33.     when a+b+c+d<e+f+g+h --第1步
  34.     then
  35.         case when a+i+j+k>e+b+c+d --第2步
  36.         then
  37.             case when b>c then ('c轻') --第3步
  38.             when b=c then ('d轻')
  39.             when b<c then ('b轻') end
  40.         when a+i+j+k=e+b+c+d --第2步
  41.         then
  42.             case when f>g then ('f重') --第3步
  43.             when f=g then ('h重')
  44.             when f<g then ('g重') end
  45.         when a+i+j+k<e+b+c+d --第2步
  46.         then
  47.             case when a!=i then ('a轻') --第3步
  48.             when a=i then ('e重') end end
  49.      end 假乒乓球,
  50.      test.*
  51. from scott.test;
--测试结果

点击(此处)折叠或打开

  1. /*
  2. 假乒乓球    A    B    C    D    E    F    G    H    I    J    K    L
    a轻    1    2    2    2    2    2    2    2    2    2    2    2
    b轻    2    1    2    2    2    2    2    2    2    2    2    2
    c轻    2    2    1    2    2    2    2    2    2    2    2    2
    d轻    2    2    2    1    2    2    2    2    2    2    2    2
    e轻    2    2    2    2    1    2    2    2    2    2    2    2
    f轻    2    2    2    2    2    1    2    2    2    2    2    2
    g轻    2    2    2    2    2    2    1    2    2    2    2    2
    h轻    2    2    2    2    2    2    2    1    2    2    2    2
    i轻    2    2    2    2    2    2    2    2    1    2    2    2
    j轻    2    2    2    2    2    2    2    2    2    1    2    2
    k轻    2    2    2    2    2    2    2    2    2    2    1    2
    l轻    2    2    2    2    2    2    2    2    2    2    2    1
    a重    3    2    2    2    2    2    2    2    2    2    2    2
    b重    2    3    2    2    2    2    2    2    2    2    2    2
    c重    2    2    3    2    2    2    2    2    2    2    2    2
    d重    2    2    2    3    2    2    2    2    2    2    2    2
    e重    2    2    2    2    3    2    2    2    2    2    2    2
    f重    2    2    2    2    2    3    2    2    2    2    2    2
    g重    2    2    2    2    2    2    3    2    2    2    2    2
    h重    2    2    2    2    2    2    2    3    2    2    2    2
    i重    2    2    2    2    2    2    2    2    3    2    2    2
    j重    2    2    2    2    2    2    2    2    2    3    2    2
    k重    2    2    2    2    2    2    2    2    2    2    3    2
    l重    2    2    2    2    2    2    2    2    2    2    2    3
  3. */

请使用浏览器的分享功能分享到微信等