poj 1338

2013-09-08 09:56

记录数组w[I],代表第I个答案的值

用三个指针指向W数组,代表当前的W[A2]*2,W[A3]*3,W[A5]*5更新答案

每次取最小值,然后后移对应指针(可能一次移两个指针),可保证W数组的数不重复且递增

var
a2, a3, a5              :longint;
    w                       :array[0..1600] of int64;
    i                       :longint;function min(a,b:int64):int64;begin
    if a>b then min:=b else min:=a;end;begin
    a2:=1; a3:=1; a5:=1;//指针
    w[1]:=1;
    for i:=2 to 1500 do
    begin
        w[i]:=min(w[a2]*2,min(w[a3]*3,w[a5]*5));//更新答案(递推)
        if w[i]=w[a2]*2 then inc(a2);//移指针
        if w[i]=w[a3]*3 then inc(a3);
        if w[i]=w[a5]*5 then inc(a5);
    end;
    read(i);
    while i<>0 do
    begin
        if i<>0 then writeln(w[i]);
        read(i); 
    end;end.

 

posted on 2013-11-20 14:16  BLADEVIL  阅读(281)  评论(0编辑  收藏  举报