二分答案的应用

题目要求:

找一个数字x,使得x^x 的位数大于等于n,(x-1)^(x-1)的位数小于n

program sky;
var
  n:qword;

function doing(l,r:qword):qword;
  var
    mid,temp1,temp2:qword;
  begin
    if l=r then exit(l);
    mid:=(l+r+1)>>1;
    temp1:=trunc(mid*(ln(mid)/ln(10)))+1;{计算位数}
    temp2:=trunc((mid-1)*(ln(mid-1)/ln(10)))+1;{计算x-1的位数}
    if (temp1>=n) and (temp2<n) then exit(mid);{分情况二分}
    if temp1<n then exit(doing(mid,r));
    if temp2>n then exit(doing(l,mid-1));{减一下mid,不重不漏}
  end;

begin
  assign(input,'number.in');reset(input);
  assign(output,'number.out');rewrite(output);
  read(n);
  write(doing(0,n+100));{从0开始,避免遗漏}
  close(input);close(output);
end.

posted @ 2011-08-12 12:05  SunSky...  阅读(134)  评论(0编辑  收藏  举报