10万级洁净车间价格:pascal程序的难题4道,寻找高手解!

来源:百度文库 编辑:神马品牌网 时间:2024/05/05 17:54:10
1、输入X和Y, 输出最大公约数。
2、输入一个二进制书,输出用十进制表示。
3、求1——100之间的所有偶数的和(包括100)
4、输入15个数 统计其中正、负、0的个数。

第一个:
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.
第四题:
var
a,b,c,d,i:longint;
begin
b:=0; c:=0; d:=0;
for a:=1 to 15 do
begin
read(i);
if i>0 then inc(b) else if i=0 then inc(c) else inc(d);
end;
writeln(b);
writeln(c);
writeln(d);
end.

先写最后一个吧

procedure count(var arr:array[1..15] of integer);
var i,K1,K2,K3:integer;
begin
K1:=0;
K2:=0;
K3:=0;
for i:=1 to 15 do
begin
if arr[i]>0 then
inc(K1)
else if arr[i]=0 then
inc(K2)
else if arr[i]<0 then
inc(K3);
end;
writeln(k1);
writeln(k2);
writeln(k3);

end;

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.