target=15;
i=1;
source=2;
now=source;
while now<target do begin
now*=source;
i++;
end;
if now=target then print(i次幂) else print(i-1次幂之后余target-now/source)
最笨的方法:
var
num, temp: Integer;
mi, remain: Integer;
begin
mi := 1;
num := 15;
temp := 2;
while num >= temp do
begin
temp := temp * 2;
Inc(mi);
end;
Dec(mi);
remain := num - temp div 2;
ShowMessage(IntToStr(num) + '=2的'+ IntToStr(mi)+'次幂余' + IntToStr(remain));
end;