pascal基础教程.doc
1.顺序结构101.求长方形的周长var a,b,c:integer;begin read(a,b); c:=(a+b)*2; write(c);end.102.两个变量的值的交换需要第三个变量的方法:var a,b,c:integer;begin a:=3; b:=4; c:=a; a:=b; b:=c; write(a,' ',b);end.不需要第三个变量:var a,b:integer;begin a:=3; b:=4; a:=a+b; b:=a-b; a:=a-b; write(a,' ',b);end.103.inc()和dec()var a :integer;begin a:=3; a:=a+1; writeln(a); inc(a); writeln(a); inc(a,3); writeln(a); a:=a-1; writeln(a); dec(a); writeln(a); dec(a,3); writeln(a);end.104.求一个四位数各位之和.var a,b,c,d,s:integer;begin read(s); a:=s mod 10; b:=s div 10 mod 10; c:=s div 100 mod 10; d:=s div 1000; write(a+b+c+d);end. 2.判断结构201.求三个数的最大值1.打擂台模式var a,b,c,s:integer;begin read(a,b,c); s:=a; if b>s then s:=b; if c>s then s:=c; write(s);end.2.else方式var a,b,c,s:integer;begin read(a,b,c); if a>=b then if a>=c then write(a) else write(c) else if b>=c then write(b) else write(c);end.202.判断是否闰年var n:integer;begin read(n); if (n mod 100<>0)and(n mod 4=0) or (n mod 400=0) then write('yes') else write('no');end.203.输入年和月,输出该月有多少天。var year,month,day:integer; runnian:boolean;begin readln(year,month); case month of 1,3,5,7,8,10,12: day:=31; 4,6,9,11: day:=30;以上处理31天和30天的情况 2:begin runnian:=(year mod 400=0) or (year mod 4=0) and (year mod 100<>0); case runnian of true: day:=28; false: day:=29; end; end; 以上处理2月的情况:闰年28天,平年29天 end; write(day);end.204.某超市为了促销,规定:购物不足50元的按原价付款,超过50不足100的按九折付款,超过100元的,超过部分按八折付款。编一程序完成超市的自动计费的工作。var n,s:real;begin read(n); if n>100 then s:=n*0.8 else if n>50 then s:=n*0.9 else s:=n; write(s);end.3.循环结构1.单重循环301.1+2+3+4+.+100(1)for.do型var n,i,s:integer;begin n:=100; for i:=1 to n do s:=s+i; write(s);end.(2).while .do型var n,i,s:integer;begin n:=100; i:=1; while i<=n do begin s:=s+i; i:=i+1; end; write(s);end.(3).repeat.until型var n,i,s:integer;begin n:=100; i:=1; repeat s:=s+i; i:=i+1; until i>n; write(s);end.302.1+3+5+7+.+99(1)for.do型var n,i,s:integer;begin n:=99; for i:=1 to n do if i mod 2=1 then s:=s+i; write(s);end.(2).while .do型var n,i,s:integer;begin n:=99; i:=1; while i<=n do begin s:=s+i; i:=i+2; end; write(s);end.(3).repeat.until型var n,i,s:integer;begin n:=99; i:=1; repeat s:=s+i; i:=i+2; until i>n; write(s);end.303.求使1+3+5+7+.+x>=1000成立的x的最小值var n,x,s:integer;begin s:=0; x:=1; while s<1000 do begin s:=s+x; x:=x+2; end; x:=x-2; write(x);end.304.输出一个任意位数(不超过9位)的反序var n,i,s:longint;begin read(n); while n>0 do begin s:=s*10 + n mod 10; n:=n div 10; end; write(s);end. 305.质数的判断var n,i:longint;f:boolean;begin read(n); f:=true; for i:=2 to trunc(sqrt(n) do if n mod i=0 then begin f:=false; break; end; if f=true then write('yes') else write('no');end.306.求最大公约数与最小公倍数 :var a,b,x,y,n,i:integer;begin read(a,b); x:=a;y:=b; n:=x mod y; while y mod n<>0 do begin x:=y; y:=n; n:=x mod y; end; writeln(n);求最大公约数 writeln(a*b div n);最小公倍数end.307.数列:1,1,3,1,3,5,1,3,5,7,1,3,5,7,9,.求第n 项的值。例如n=5时,为3. var s,n,i,j,k:integer;begin read(n); i:=1;当前数到第i项 s:=1;当前项的值 k:=1;当前项处于第k个周期 j:=1;当前周期中数到第j项 while i<n do begin i:=i+1; if j+1>k then begin j:=1; s:=1; k:=k+1; end else begin j:=j+1; s:=s+2; end; end; write(s);end. 308.求周期性数列第n项的值.数列:1,1,3,1,1,3,5,3,1,1,3,5,7,5,3,1,1,3,5,7,9,7,5,3,1.求第n项的值,如:n=5时,值为1第1周期 1 2第2周期 3 4第3周期 5 6第4周期 7 8第k周期 2*k-1 2*kvar s,i,j,k,t,n:integer;begin read(n); i:=1;当前数到第i项 k:=1;当前处于第k个周期 j:=1;处于当前周期中第j项 s:=1;当前项的值 while i<n do begin i:=i+1; if j+1>2*k-1 then begin j:=1; s:=1; k:=k+1; end else if j+1<=k then begin j:=j+1; s:=s+2; end else begin j:=j+1; s:=s-2; end; end; write(s);end.309.数列4:1,1,1,2,2,1,1,1,2,2,2,3,3,3,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4.的第n项,如n=4,值为2.var n,i,j,k,s:integer;begin read(n); i:=1;当前在总第i项 k:=1;当前项所在第k个周期 j:=1;处于当前周期中第j项 s:=1;当前项的值 while i<n do begin i:=i+1; if j+1>k*k then begin j:=1; k:=k+1; s:=1; end else if j mod k=0 then begin j:=j+1; s:=s+1; end else begin j:=j+1; end; /write(s:4); end; write(s:4);end.310.求数列1、5、17、53、161、。前20项的和。(提示: An=2+3×A(n-1))(答案:3486784380)var an,am,s:real; i:integer;begin writeln; am:=1; s:=1; write(am:20:0); for i:=2 to 20 do begin an:=am*3+2; s:=s+an; am:=an; write(am:20:0); end; writeln; write('totle:',s:0:0);end.双重循环311.打印图形*var i,j:integer;begin for i:=1 to 5 do begin for j:=1 to i do write('*'); writeln; end;end.312.打印图形 * * * *var i,j:integer;begin for i:=1 to 5 do begin write('':5-i); for j:=1 to 2*i-1 do write('*'); writeln; end;end.313.打印图形* * * * *解法一:var i,j:integer;begin for i:=5 downto 1 do begin write('':5-i); for j:=1 to 2*i-1 do write('*'); writeln; end;end.解法二:var i,j:integer;begin for i:=1 to 5 do begin write('':i-1); for j:=1 to 10-(2*i-1) do write('*'); writeln; end;end.314.打印图形:122333444455555var i,j:integer;begin for i:=1 to 5 do begin for j:=1 to i do write(i); writeln; end;end.315.打印图形:112123123412345var i,j:integer;begin for i:=1 to 5 do begin for j:=1 to i do write(j); writeln; end;end.打印图形:121321432154321解法一:var i,j:integer;begin for i:=1 to 5 do begin for j:=i downto 1 do write(j); writeln; end;end.解法二:var i,j:integer;begin for i:=1 to 5 do begin for j:=1 to i do write(i+1-j); writeln; end;end.思考:打印图形:244666888810101010打印图形:133555777799999打印图形:113135135713579打印图形: 1 313 53135 7531357975313579打印图形: 1 * 22222 *333333333continue 与break和exit.halt的区别317.var i,j,k,t,n,s:integer;begin read(n); for i:=1 to n do begin k:=k+i; for j:=1 to i do begin t:=t+j; if t>2 then break; s:=s+1; end; end; write('s=',s,'k=',k,'t=',t);end.输入:3输出:318.var i,j,k,t,n,s:integer;begin read(n); for i:=1 to n do begin k:=k+i; for j:=1 to i do begin t:=t+j; if t>2 then continue; s:=s+1; end; end; write('s=',s,'k=',k,'t=',t);end.输入:3输出: 319.var i,j,k,t,n,s:integer;begin read(n); for i:=1 to n do begin k:=k+i; for j:=1 to i do begin t:=t+j; s:=s+1; if t>2 then break; end; end; write('s=',s,'k=',k,'t=',t);end.输入:3输出:320.var i,j,k,t,n,s:integer;begin read(n); for i:=1 to n do begin k:=k+i; for j:=1 to i do begin t:=t+j; s:=s+1; if t>2 then continue; end; end; write('s=',s,'k=',k,'t=',t);end.输入:3输出: 321.var i,j,n,t,k:integer;begin read(n); for i:=1 to n do begin k:=k+2; for j:=1 to i do t:=t+i; end; write('k=',k,'t=',t);end.输入:3输出:6.var i,j,n,t,k:integer;begin read(n); for i:=1 to n do begin k:=k+2; for j:=1 to i do t:=t+j; end; write('k=',k,'t=',t);end.输入:3输出:7.var i,j,n,t,k:integer;begin read(n); for i:=1 to n do begin k:=k+2; for j:=1 to i do begin k:=k+1; t:=t+k; end; end; write('k=',k,'t=',t);end.输入:3输出:多重循环(枚举)322.四大湖排名情况四个学生上地理课,回答我国四大淡水湖大小时这样说:甲:“最大洞庭湖,最小洪泽湖,鄱阳湖第三。”乙:“最大洪泽湖,最小洞庭湖,鄱阳湖第二,太湖第三。”丙:“最小洪泽湖,洞庭湖第三。”丁:“最大鄱阳湖,最小太湖,洪泽湖第二,洞庭湖第三。”对于每个湖的大小,每个学生仅答对一个,请编程确定四个湖的大小。Var dongtinghu,hongzehu,boyanghu,taihu:integer;A,b,c,d:integer;Begin For dongtinghu:=1 to 4 do For hongzehu:=1 to 4 do If hongzehu<>dongtinghu then For boyanghu:=1 to 4 do If ( hongzehu<>boyanghu)and(dongtinghu<>boyanghu) then Begin Taihu:=1+2+3+4-dongtinghu-hongzehu-boyanghu; If taihu*hongzehu*boyanghu*dongtinghu=1*2*3*4 then Begin A:=ord(dongtinghu=1)+ord(hongzehu=4)+ord(boyanghu=3); B:=ord(hongzehu=1)+ord(dongtinghu=4)+ord(boyanghu=2)+ord(taihu=3); C:=ord(hongzehu=4)+ord(dongtinghu=3); D:=ord(boyanghu=1)+ord(taihu=4)+ord(hongzehu=2)+ord(dongtinghu=3) ; If (a=1) and ( b=1) and(c=1)and(d=1) then begin Writeln('dongtinghu:',dongtinghu,' hongzehu:',hongzehu,' taihu:',taihu,' boyanghu:',boyanghu); break; end; end; end;end.323.说谎问题 一维数组400.输入n个数,要求输出最大值和最小值. var a:array1.1000of integer; n,i,j,max,min:integer; begin readln(n); for i:=1 to n do read(ai); max:=a1; min:=a1; for i:=2 to n do begin if ai>max then max:=ai; if ai<min then min:=ai; end; write('最大值:',max,'最小值:',min); end.401.斐波列数列:1 1 2 3 5 8 13 21 34的第n项的值如:n=8时值为21。var a:array1.1000of integer;i,n:integer;begin read(n); a1:=1; a2:=1; for i:=3 to n do ai:=ai-1+ai-2; write(an);end. 402.求一个数各数数位上数字的和.var s,i:longint;a:array1.100of integer;begin read(s); fillchar(a,sizeof(a),0); while s>0 do begin i:=i+1; ai:=s mod 10; s:=s div 10; end; write(s);end.403.分解质因数12=2*2*3var s,n,i,j,k:integer;a:array1.1000of integer;begin read(n); s:=n; i:=2; while s>1 do begin while s mod i =0 do begin k:=k+1; ak:=i; s:=s div 2; end; i:=i+1; end; write(n,'=',a1); if k=1 then exit else for i:=2 to k do write('*',ai);end.404.筛选出1.100内所有素数405.猴子选大王有N只猴子围成一圈,每只各一个从1到N中的编号,打算从中选出一个大王;经过协商,决定出选大王的规则:从第一个开始循环报数,数到M的猴子出圈,最后剩下来的就是大王。要求:从键盘输入N、M,编程计算哪一个编号的猴子成为大王思路1:var a:array1.1000of integer;s,n,m,i,j,k:integer;begin read(n,m); s:=n; for i:=1 to n do ai:=1; i:=0; while s>1 do begin k:=0; while k<m do begin i:=i+1; if i>n then i:=1; k:=k+ai; end; ai:=0; write(i,' '); s:=s-1; end; for i:=1 to n do if ai=1 then begin write(i); exit; end;end.思路2:建立一个循环数组,数组元素的每一个值保存的是它的下一个元素的编号,如果报到m后只要修改m前面的元素的编号为m后面的元素的编号,这样就跳过了第m个元素,如此下去直到n个人全部出列。【参考程序】Program yuesefu;Vara:array1.100 of integer;n,m,p,i,j:integer;Beginreadln(n,m);for i:=1 to n-1 doa:=i+1;an:=1;p:=n;for i:=1 to n dobeginfor j:=1 to m-1 dop:=ap;write(ap,' ');ap:=aap;end;End.406.狐狸追兔子狐狸捉兔子围绕着山顶有10个洞,一只狐狸和一只兔子住在各自的洞里。狐狸总想吃掉兔子。一天,兔子对狐狸说:“你想吃我有一个条件,先把洞从110编上号,你从10号洞出发,先到1号洞找我;第二次隔1个洞找我,第三次隔2个洞找我,以后依此类推,次数不限。若能找到我,你就可以饱餐一顿。不过在没有找到我以前不能停下来。”狐狸满口答应就开始找了,它从早到晚进了1000次洞,累得昏了过去也没找到兔子。请问,兔子躲在几号洞里?program mr;const holenumber = 10;var hole : array1 . holenumber of 0 . 1; step, i, number : longint;begin for i := 1 to holenumber do holei := 0; number := 0; for step := 1 to 1000 do begin number := number+step; 循环地数 i := number mod holenumber; 第几个洞 if i = 0 then i := 10; holei := 1; end; for i := 1 to holenumber do if holei = 0 then write(i:3); readln;end. 407.二分查找在一个包含n个数的从小到大排列的数列中查找某数k的所在该数列中的位置。 解法一:顺序查找法 var n,i,j,k:integer; a:array1.1000of integer; begin realn(n,k); for i:=1 to n do read(ai); i:=1; for i:=1 to n do if ai=k then begin write(i); break; end; if i=n then write('数列不存在k!'); end. 解法二:二分查找法 var n,i,j,k,l,r,mid:integer; f:boolean; a:array1.1000of integer; begin readln(n,k); for i:=1 to n do read(ai); f:=false; l:=1; r:=n; repeat mid:=(l+r) div 2; if amid=k then begin write(mid); f:=true; exit; end else if amid>k then r:=mid-1 else l:=mid+1; until r<l ; if f=false then write('No k!'); end.408.三种基本排序409.冒泡排序1.冒泡排序(从小到大排序): var n,i,j,k:integer; a:array1.1000of integer; begin readln(n); for i:=1 to n do read(ai); for i:=1 to n-1 do for j:=1 to n-i do if aj>aj+1 then begin k:=aj; aj:=aj+1; aj+1:=k; end; for i:=1 to n do write(ai:4); end. 思考:看程序写结果: var i,j,k:integer; a:array1.5 of integer; begin for i:=1 to 5 do read(ai); for i:=1 to 4 do for j:=5 downto i+1 do if aj<aj-1 then begin k:=aj; aj:=aj-1; aj-1:=k; end; for i:=1 to 5 do write(ai:5); end. 输入:5 3 6 4 8 输出:3 4 5 6 8410.选择排序1.选择排序(从小到大排序): var n,i,j,k:integer; a:array1.1000of integer; begin readln(n); for i:=1 to n do read(ai); for i:=1 to n-1 do for j:=i+1 to n do if aj<ai then begin k:=aj; aj:=ai; ai