比如一个三位数192,192*2=384,192*3=576,这三个三位数正好取了1到9这9个数字,求出所以符合这个条件的数字。
其实这个题目是不可以用编程方法求出的,这样做只是为了提高编程技巧,不过我的程序效率不高,我希望看到高手写的程序以提高我的水平。
我用pl/sql求出另外3个数分别是219,273,327
希望大家贴出自己的求解方法,谢谢!
declare
type shuzi_type is table of number index by binary_integer;
shuzi shuzi_type;
ijk int;
begin
for i in 1..3 loop
  shuzi(1):=i;
  for j in 1..9 loop
    if j=i then continue;end if;
    if i>=3 and j>3 then exit;end if;
    shuzi(2):=j;
    for k in 1..9 loop
      if k=i or k=j then continue;end if;
      shuzi(3):=k;
      ijk:=shuzi(1)*100+shuzi(2)*10+shuzi(3);
      shuzi(4):=floor(ijk/50);
      if shuzi(4)=shuzi(1) or shuzi(4)=shuzi(2) or shuzi(4)=shuzi(3) then continue;end if;
      shuzi(5):=floor((ijk*2-shuzi(4)*100)/10);
      if shuzi(5)=shuzi(1) or shuzi(5)=shuzi(2) or shuzi(5)=shuzi(3) or shuzi(5)=shuzi(4) then continue;end if;
      shuzi(6):=mod(ijk*2,10);
      if shuzi(6)=shuzi(1) or shuzi(6)=shuzi(2) or shuzi(6)=shuzi(3) or shuzi(6)=shuzi(4) or shuzi(6)=shuzi(5) then continue;end if;
      shuzi(7):=floor(ijk*3/100);
      if shuzi(7)=shuzi(1) or shuzi(7)=shuzi(2) or shuzi(7)=shuzi(3) or shuzi(7)=shuzi(4) or shuzi(7)=shuzi(5) or shuzi(7)=shuzi(6) then continue;end if;
      shuzi(8):=mod(floor(ijk*3/10),10);
      if shuzi(8)=shuzi(1) or shuzi(8)=shuzi(2) or shuzi(8)=shuzi(3) or shuzi(8)=shuzi(4) or shuzi(8)=shuzi(5) or shuzi(8)=shuzi(6) or shuzi(8)=shuzi(7) then continue;end if;
      shuzi(9):=mod(ijk*3,10);
      if shuzi(9)=shuzi(1) or shuzi(9)=shuzi(3) or shuzi(9)=shuzi(2) or shuzi(9)=shuzi(4) or shuzi(9)=shuzi(5) or shuzi(9)=shuzi(6) or shuzi(9)=shuzi(7) or shuzi(9)=shuzi(8) then continue;end if;
dbms_output.put_line(ijk);
end loop;end loop;end loop;
end;

解决方案 »

  1.   

    SQL> SELECT *
      2    FROM (SELECT num, num || num * 2 || num * 3 numstr
      3            FROM (SELECT LEVEL + 100 - 1 num FROM dual CONNECT BY LEVEL <= 900))
      4   WHERE length(numstr) = 9 AND
      5    instr(numstr,'1')>0 AND 
      6    instr(numstr,'2')>0 AND
      7    instr(numstr,'3')>0 AND
      8    instr(numstr,'4')>0 AND
      9    instr(numstr,'5')>0 AND
     10    instr(numstr,'6')>0 AND
     11    instr(numstr,'7')>0 AND
     12    instr(numstr,'8')>0 AND
     13    instr(numstr,'9')>0;       NUM NUMSTR
    ---------- --------------------------------------
           192 192384576
           219 219438657
           273 273546819
           327 327654981已用时间:  00: 00: 00.01
    SQL> 
      

  2.   


    [SYS@orcl] SQL>with t1 as(
      2    select level c1 ,
      3           level||level*2||level*3 c2
      4      from dual
      5    connect by level <= 999
      6  ),t2 as(
      7    select c1,c2
      8      from t1
      9     where c1 > 99
     10  ),t3 as(
     11    select c1,c2,count(distinct column_value)
     12     from t2,
     13          table(cast(multiset(
     14                select substr(c2,level,1)
     15                  from dual
     16                connect by level <= length(c2)) AS sys.odcivarchar2list))
     17    where length(c2) = 9
     18    group by c1,c2
     19    having count(distinct column_value) = 9
     20  )select c1
     21     from t3
     22    where instr(c2,0,1,1) = 0;        C1
    ----------
           192
           219
           273
           327
      

  3.   

    谢谢二楼和三楼,这道题是《1/1000000的聪明人才会做的数学游戏》中的第69题,很有意思的一本书,我正把所有我觉得可以用编程求解的题都这样解一遍,其中也有八皇后问题和幻方问题,因为我对数论不了解所以很多题目都不会笔算,我说明白这些是因为1楼的仁兄很鄙视我的这种show的行为,但是我没有,我只想看到更高效更好地解法,我必须不断提高自己以免被淘汰。