请稍候...
您现在的位置: 中华信息学竞赛网  >> 每日一练

【每日一练·总第十七期】小学第五期

发布日期:2008-8-11
浏览次数:1556
本资料需要注册并登录后才能下载!
·用户名  密码   验证码 点击获取新的验证码   找回密码
·您还未注册?请注册
您的账户余额为元,余额已不足,请充值
您的账户余额为元。此购买将从您的账户中扣除费用0.0元。
内容介绍>>
  如果一个两位整数是质数,而且把它的两位颠倒过来得到的数也是质数,则说此数是无暇数。输入一个数字序列,找出其中所有无暇数。(答案下期见)
 
大学第四期答案
 
【参考程序】
  program fenshifenjie;
  const nums=5;
  var
    t,m,dep:integer;
    n,maxold,max,j:longint;
    path:array[0..nums] of longint;
    maxok,p:boolean;
    sum,sum2:real;
  procedure print;
  var i:integer;
  begin
       t:=t+1;
       if maxok=true then begin maxold:=path[m];maxok:=false;end;
       write ('NO.',t);
       for i:=1 to m do write(' ',path[i]:4); writeln;
       if path[1]=path[m] then begin writeln('Ok!  total:',t:4);readln;halt;end;
  end;
  procedure input;
  begin
       writeln ('input N:'); readln(n);
       writeln ('input M(M<=',nums:1,'):'); readln(m);
       if (n<=0) or (m<=0) or (m>4) or (n>maxlongint)
            then begin writeln('Invalid Input!');readln;halt;end;
  end;
  function sum1(ab:integer):real;
  var a,b,c,d,s1,s2:real;
    i:integer;
  begin
       if ab=1 then
         sum1:=1/path[1]
       else
         begin
           a:=path[1];
           b:=1       ;
           c:=path[2];
           d:=1;
           for i:=1 to ab-1 do
              begin
               s2:=(c*b+a*d);
               s1:=(a*c);
               a:=s1;
               b:=s2;
               c:=path[i+2];
              end;
           sum1:=s2/s1;
          end;
  end;
  procedure back;
  begin
       dep:=dep-1;
       if dep<=m-2 then max:=maxold;
       sum:=sum-1/path[dep];
       j:=path[dep];
  end;
  procedure find;
  begin
    repeat
       dep:=dep+1;
       j:=path[dep-1]-1;
       p:=false;
        repeat
        j:=j+1;
        if (dep<>m) and (j<=max) then
          if (sum+1/j) >=1/n then p:=false
             else begin
                p:=true;
                path[dep]:=j;
                sum:=sum+1/path[dep];
                end
          else if j>max then back;
        if dep=m then begin
          path[dep]:=j;
          sum2:=sum1(m);
          if (sum2)>1/n then p:=false;
          if (sum2)=1/n then begin   print;
                        max:=j;
                        back;
                        end;
          if (sum2<1/n) then back;
          if (j>=max)  then back;
          end;
     until p
    until dep=0;
  end;
  begin
     INPUT;
     maxok:=true;
     for t:=0 to m do path[t]:=n;
     dep:=0; t:=0; sum:=0;
     max:=maxlongint;
     find;
     readln;
  end
 共有 0 条评论 (查看更多评论内容
客服热线:010-62029238 QQ咨询:564948255 QQ投诉/建议:393863190
中华信息学竞赛网 版权所有 2007-2009 www.100xinxi.com All rights reserved. 京ICP备09054306号