您的位置:首页 > 其它

二分答案的应用

2011-08-12 12:05 239 查看
题目要求:

找一个数字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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: