奥奇传说四格漫画:有关编程的问题,请懂的人进

来源:百度文库 编辑:高校问答 时间:2024/04/28 11:18:43
三个问题
是PASCAL的
1\输入X,Y,求它们的最大公约数
2\输入一个二进制数,输出其十进制表示
3\求1~100间所有偶数和(包括100)

求以上三个程序该怎么写?拜托了...

ar a,b,c,d,e:integer;
begin
readln(a,b);
if a<>b then
begin
if a<b then begin d:=a;a:=b;b:=d;end;//保证a>b
c:=a mod b;
while c<>0 do//辗转相除
begin
a:=b;b:=c;
c:=a mod b;
end;
e:=b;
writeln('e=',e);
end else writeln('e=',1);
end.
原理:如果两个数有最大公约数A,那么这两个数,以及这两个数的差,还有大数除以小数的余数,必然都是A的倍数。
所以当最后两个数刚好能整除时,较小的数就是最大公约数。
2.
var total,l,i,temp:integer;st:string;
begin
readln(st);
total:=0;l:=length(st);
for i:=l downto 1 do
if st[i]='1' then begin
temp:=1;
for j:=1 to l-i do temp:=temp*2;
inc(total,temp);
end;
writeln(total);
end.
3.
var total,i:integer;
begin
total:=0;
for i:=1 to 50 do inc(total,i);
writeln(total*2);
end.//1到100的偶数就是2 4 6... 分别是1 2 3...的2倍
//所以直接由1加到50就可以了
4.var zs,fs,lin,i:byte;c:integer;
zs:=0;fs:=0;lin:=0;
for i:=1 to 15 do begin
read(c);
if c>0 then inc(zs) else
if c<0 then inc(fs) else
inc(lin);
end;
writeln('Total num above 0:',zs);
writeln('Total num below 0:',fs);
writlen('Total num equal to 0:',lin);
end.
回答者:zhymaoiing - 魔法学徒 一级 4-23 12:48

1.
var a,b,c,d,e:integer;
begin
readln(a,b);
if a<>b then
begin
if a<b then begin d:=a;a:=b;b:=d;end;//保证a>b
c:=a mod b;
while c<>0 do//辗转相除
begin
a:=b;b:=c;
c:=a mod b;
end;
e:=b;
writeln('e=',e);
end else writeln('e=',1);
end.
原理:如果两个数有最大公约数A,那么这两个数,以及这两个数的差,还有大数除以小数的余数,必然都是A的倍数。
所以当最后两个数刚好能整除时,较小的数就是最大公约数。
2.
var total,l,i,temp:integer;st:string;
begin
readln(st);
total:=0;l:=length(st);
for i:=l downto 1 do
if st[i]='1' then begin
temp:=1;
for j:=1 to l-i do temp:=temp*2;
inc(total,temp);
end;
writeln(total);
end.
3.
var total,i:integer;
begin
total:=0;
for i:=1 to 50 do inc(total,i);
writeln(total*2);
end.//1到100的偶数就是2 4 6... 分别是1 2 3...的2倍
//所以直接由1加到50就可以了
4.var zs,fs,lin,i:byte;c:integer;
zs:=0;fs:=0;lin:=0;
for i:=1 to 15 do begin
read(c);
if c>0 then inc(zs) else
if c<0 then inc(fs) else
inc(lin);
end;
writeln('Total num above 0:',zs);
writeln('Total num below 0:',fs);
writlen('Total num equal to 0:',lin);
end.

第一个:
var
a,x,y,min:longint;
begin
readln(x,y);
if x>y then min:=y else min:=x;
for a:=min downto 1 do
if (x mod a)=0 and (y mod a=0) then
begin
writeln(a);
break;
end;
end.
第二个:
var
a,b,sum:longint;
s:string;
begin
readln(s);
b:=1;
sum:=0;
for a:=length(s) downto 1 do
begin
if s[a]='1' then inc(sum,b);
b:=b*2;
end;
writeln(sum);
end.
第三个:
var
a,sum:longint;
begin
sum:=0;
for a:=1 to 50 do
inc(sum,a*2);
writeln(sum);
end.

1.
var
x,y,g:longint;
beign
read(x,y);
if x>y
then g:=y+1
else g:=x+1;
repeat
g:=g-1;
until (x mod g=0) and (y mod g=0);
writeln(g);{g是最大公因数}
end.
2.
var a,l,i,temp:integer;st:string;
begin
readln(st);
total:=0;l:=length(st);
for i:=l downto 1 do
if st[i]='1' then begin
temp:=1;
for j:=1 to l-i do temp:=temp*2;
inc(a,temp);
end;
writeln(a);
end.
3.
var
o,i:integer;
begin
o:=0;
for i:=1 to 100 do
if i mod 2=0
then o:=o+i;
writeln(o);
end.

1:
Label 1;
Var
a,b,c,d,e:Longint;
Begin
Read(a);
Read(b);
If a<b Then
d:=a
Else
d:=b;
For c:=d Donwto 1 Do
If a Mod c = 0 Then
If b Mod c = 0 Then
Begin
Writeln(c);
Goto 1;
End;
1:
Wrteln;
End.

2:
Const
a=2;
Var
ch:Char;
b,c,d:Longint;
Begin
Read(ch);
While (ch in ['0'..'9']) Do
Begin
If (ch in ['0'..'9']) Then
b:=Ord(ch)-Ord('0');
c:=c*a+b;
Read(ch);
End;
Writeln(c);
End.

3:
Var
a,b:Longint;
Begin
For a:=1 To 100 Do
If a Mod 2 = 0 Then
b:=b+a;
Writeln(b);
End.