之前一直是Trunc(ln(n)/ln(2))做,今天做uva时一直wa,一直wa……发现精度问题很重要啊。
11591561 | 11714 | Blind Sorting | Accepted | PASCAL | 0.076 | 2013-04-11 13:00:25 |
11591467 | 11714 | Blind Sorting | Wrong answer | PASCAL | 0.068 | 2013-04-11 12:42:31 |
11591418 | 11714 | Blind Sorting | Wrong answer | PASCAL | 0.364 | 2013-04-11 12:33:54 |
用这样的写法勉强过了,求交流更好的写法
Function log2(P:cardinal):cardinal;inline; var i:longint; begin for i:=31 downto 0 do if (1 shl i) and p>0 then exit(i); end;
更好的写法
Function log2(P:longint):longint;inline; var k:real; begin k:=ln(n)/ln(2); if round(k)-k<1e-4 then exit(round(k)) else exit(trunc(k)); end;