疏水作用力名词解释:请高手们解决几道PASCAL初级问题

来源:百度文库 编辑:神马品牌网 时间:2024/05/13 15:39:14
1:输入x,y,输出其最大公约数
2:输入一个二进制数,输出十进制
3:求1到100间所有偶数的和(包括100)
4:输入15个数,统计其中正,负,零的个数循环

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;
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.

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.

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.

3.题 从1加到100 再加上100 ,然后除以2,不用判断是否问奇偶数,速度也会比判断是否奇偶数快。 ^_^

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.

"回答者:匿名 4-23 13:53" 你干吗要抄袭我的