Перевод из двоичной системы в десятичную.

program conv;


uses crt;


var s:string;
ff,f:text;
a:array[1..1000] of integer;
i,n,x,v,j:integer;


begin
clrscr;
assign(ff,'output.txt');
rewrite(ff);
assign(f,'input.txt');
{$I-}
reset(f);
{$I+}
if IOResult=0 then
begin
repeat
readln(f,s);
for i:=1 to 10 do
a[i]:=-1;
i:=1;
repeat
if copy(s,i,1)='0' then a[i]:=0;
if copy(s,i,1)='1' then a[i]:=1;
i:=i+1;
n:=i-1;
until (copy(s,i,1)='-1') or (i=length(s)+1);
for i:=1 to n do
begin
v:=1;
for j:=1 to (n-i) do
v:=v*2;
x:=x+a[i]*v;
end;
writeln(ff,x);
until EOF(f);
end
else
writeln(ff,'ERROR');
close(f);
close(ff);
end.

Эта задача была на одной из школьных олимпиад. Ее решение довольно простое...



Page: [1] [2] [3]
февраль, 2007
пн вт ср чт пт сб вс
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28

Page created in 0.06598 seconds Powered by LastoBlog