最新PASCAL程序基础
- 格式:ppt
- 大小:543.00 KB
- 文档页数:78
Pascal语言编程基础程序(常州市)十进制转二进制var i,n,j:longint;a:array[1..100] of longint;beginreadln(n);i:=1;while n<>0 dobegina[i]:=n mod 2;i:=i+1;n:=n div 2;end;write('Bin:');for j:= i-1 downto 1 dowrite(a[j])end.数组元素删除var a:array[1..10]of longint;i,t,x:longint;beginread(x);for i:=1 to 10 doa[i]:=2*i-1;t:=a[x];for i:=x+1 to 10 doa[i-1]:=a[i];for i:=1 to 9 dowrite(a[i]:4);end.数组元素删除2var a:array[1..11]of longint;i:longint;beginfor i:=1 to 10 doa[i]:=i;a[11]:=a[1];for i:= 1 to 10 doa[i]:=a[i+1];for i:= 1 to 10 dowrite(a[i]:4); end.数组元素的移动var a:array[1..10] of longint;s,n,i,x,t:longint;beginreadln(n);for i:=1 to n doread(a[i]);readln(x);s:=a[x];for i:=x+1 to n doa[i-1]:=a[i];for i:=1 to n-1 dowrite(a[i],' ');write(s);end.排除所有异形基因var a:array[1..100] of longint;n,g,j,i,wz:longint;beginreadln(n);for i:=1 to n do read(a[i]);g:=0;for i:=1 to n doif sqr(a[i]) mod 7=1then beginwz:=i;for j:=wz+1 to n doa[j-1]:=a[j];g:=g+1end;write(a[1]);for i:=2 to n-g dowrite(' ',a[i]);writeln;end.排除第一个异形基因var a:array[1..100] of longint;n,i,wz:longint;beginreadln(n);for i:=1 to n do read(a[i]);for i:=1 to n doif sqr(a[i]) mod 7=1then begin wz:=i; break; end;for i:=wz+1 to n doa[i-1]:=a[i];write(a[1]);for i:=2 to n-1 dowrite(' ',a[i]);writeln;end.排除所有var i,n,j,s:longint;a:array[0..100] of longint;beginreadln(n);i:=0;while n<>0 dobegina[i]:=n mod 2;i:=i+1;n:=n div 2;end;s:=0;for j:=i-1 downto 0 dobeginif (s=0)and(a[j]=1) then begin write(j);s:=1; endelse if (a[j]=1) then write(' ',j);end;writeln;end.排名var a:array[1..100] of real;xh:array[1..100]of longint;n,j,i,k:longint;t:real;beginread(n);for i:=1 to n do read(a[i]);for i:=1 to n do beginfor j:=1 to n-1 doif a[j]<a[j+1] then begin t:=a[j];a[j]:=a[j+1];a[j+1]:=t;xh[j]:=xh[j+1];xh[j+1]:=k;end;end;for i:=1 to n dowriteln('NO',i,':',xh[i ]);end.排队迟到var n,x,y,wz,xwz,i :longint;a:array[1..100]of longint;beginreadln(n,x,y);for i:=1 to n do read(a[i]);for i:=1 to n doif x=a[i] then xwz:=i;wz:=xwz+1;for i:=n downto wz doa[i+1]:=a[i];a[wz]:=y ;write(a[1]);for i:=2 to n+1 dowrite(' ',a[i]);writeln;end.元素插入有序数组var n,i,x,y,wz,m:longint;a:array[1..100]of longint;beginreadln(n);readln(m);for i:=1 to m doread(a[i]);wz:=m+1;for i:= 1 to m doif n<=a[i] then begin wz:=i; break;end;if wz<>0 thenfor i:=m downto wz doa[i+1]:=a[i];a[wz]:=n ;write(a[1]);for i:=2 to m+1 dowrite(' ',a[i]);writeln;end.数组平移var a:array[1..11]of longint;i:longint;beginfor i:=1 to 10 doa[i]:=i;a[11]:=a[1];for i:= 1 to 10 doa[i]:=a[i+1];for i:= 1 to 10 dowrite(a[i]:4);end.排除第一个异形基因var a:array[1..100] of longint;n,i,wz:longint;beginreadln(n);for i:=1 to n do read(a[i]);for i:=1 to n doif sqr(a[i]) mod 7=1then begin wz:=i; break; end;for i:=wz+1 to n doa[i-1]:=a[i];write(a[1]);for i:=2 to n-1 dowrite(' ',a[i]);writeln;end.各位数之和2var a:string;s,i:longint;beginreadln(a);s:=0;for i:=1 to length (a) dos:=s+ord(a[i])-48;writeln(s);end.八进制回文数-提高var a:array[1..100] of longint;n,i,j,t,l,r,f:longint;beginread(n);i:=1;while n<>0 do begina[i]:=n mod 8;i:=i+1;n:=n div 8;end;write('Oct:' );for j:=i-1 downto 1 dowrite(a[j]);f:=0;l:=1;r:=i-1;while l<=r doif a[l]=a[r] then begin l:=l+1;r:=r-1; endelse begin f:=1;break; end; writeln;if f=0 then writeln('YES')else writeln('NO');end.最大公约数2var ans,n,r,i,t:longint;function work(a,b:longint):longint;beginif a mod b=0 then work:=belse work:=work(b,a mod b); end;beginreadln(n);read(ans);for i:=2 to n dobeginread(r);if r<ans then begin t:=r;r:=ans; ans:=t;end;ans:=work(r,ans);end;writeln(ans);end.走台阶var n:longint;function ztj(a:longint):longint;beginif a=1 thenztj:=1elseif a=2 thenztj:=2elseztj:=ztj(a-1)+ztj(a-2);end;beginreadln(n);writeln(ztj(n));end.自然数拆分var n:longint;a:array[1..100]of longint; procedure try(dep,lasti,r:longint); vari:longint;beginif r<=0then if r=0 thenbeginfor i:=1 to dep-1 do write(a[i]:3); writelnendelseelse for i:=lasti downto 1 do begina[dep]:=i;try(dep+1,i,r-i);end;end;beginread(n);try(1,n,n);end.自然数拆分2var n:longint;a:array[1..100]of longint; procedure cfs(dep,n,x:longint); var i:longint;beginif n=0then beginfor i:=1 to dep-2 dowrite(a[i],' ');writeln(a[dep-1]);endelse beginfor i:=x to n dobegina[dep]:=i;cfs(dep+1,n-i,i);end;end;end;beginreadln(n);cfs(1,n,1);end.自然对数的底vari,n,m,cc,p,k : longint;a,b,c : array[0..10010] of longint;Procedure addb;varj: longint;beginif m>cc then cc:=m;for j:=1 to cc doinc(c[j],b[j]);for j:=cc downto 1 dobegininc(c[j-1], c[j] div 10);c[j]:= c[j] mod 10;end;end;Procedure divi(k:longint);varj,t: longint;begint:=0;j:=1;while (j<=n+10) dobeginb[j]:=(a[j]+t*10) div k;t:=(a[j]+t*10) mod k;if j>m then m:=j;inc(j);if (t=0) and (j>m) then break;end;a:=b;end;beginreadln(n);fillchar(b,sizeof(b),0);fillchar(c,sizeof(c),0);fillchar(a,sizeof(a),0);a[1]:=10;m:=1;cc:=1;i:=2;k:=0;while k=0 dobegindivi(i);addb;inc(i);k:=1;for p:=1 to cc doif b[p]<>0 thenbegink:=0;break;end;end;write('2.');for i:=1 to n dowrite(c[i]);writeln;end.字符串逆序Var n:string; procedure w(s:longint);beginif s<1 thenwritelnelsebeginwrite(n[s]);w(s-1);end;end;beginreadln(n);w(length(n));end.猪猪的反击Var n,m,i,j,s:longint;a:array [1..100,1..100] of longint; beginreadln(n,m);fillchar(a,sizeof(a),0);for i:=1 to n dofor j:=1 to m doread(a[i,j]);i:=1;j:=1;s:=a[1,1];while i<n dobeginif j=m theninc(i)elseif a[i+1,j]<a[i,j+1] theninc(j)elseinc(i);inc(s,a[i,j]);end;writeln(s);end.植树Var m,n,i,j,t:longint;a:array[1..100,1..3] of longint;beginreadln(m,n);t:=0;for i:=1 to n doreadln(a[i,1],a[i,2],a[i,3]);for i:=0 to m doif i mod 5=0 thenbeginfor j:=1 to n+1 doif j=n+1 theninc(t,16)elseif (i>=a[j,1]) and (i<=a[j,2]) thenbegininc(t,a[j,3]);break;endelsecontinue;endelsecontinue;t:=t*2;writeln(t);end.找孪生数var x,cx,s,i,bx,n:longint;beginreadln(n);s:=0;for x:=1 to n dobegincx:=0;for i:=2 to trunc(sqrt(x)) doif x mod i=0 then cx:=cx+i+x div i;if i*i=x then cx:=cx-i;cx:=cx+1;bx:=0;for i:=2 to trunc(sqrt(cx)) doif cx mod i =0 then bx:=bx+i+cx div i;if i*i=cx then bx:=bx-i;bx:=bx+1;if (bx=x) and (x<cx)then begin s:=1;writeln(x:10,cx:10);end;end;if s=0then writeln('nothing');end.辗转相除var a,b,x:longint;function f(a,b:longint):longint;beginif a mod b=0 then begin f:=b; endelse begin f:=f(b,a mod b); end;end;beginread(a,b);x:=a*b div f(a,b);writeln(f(a,b),' ',x);end.约瑟夫1var a:array[1..100] of 0..1;n,m,left,count,wei,i:integer; beginreadln(n,m);for i:=1 to 100 do a[i]:=1; left:=n;count:=0;wei:=0;while left>1 dobeginwei:=wei+1;if wei>n then wei:=1;count:=count+a[wei];if count=mthen begincount:=0;a[wei]:=0;left:=left-1;end;end;for i:=1 to n doif a[i]=1then writeln(i);end.小鸟的阵地var n:longint;function f(a:longint):longint;beginif a=1 thenf:=1elseif a=2 thenf:=2elsef:=f(a-1)+f(a-2);end;beginreadln(n);writeln(f(n));end.小黄的短信var a:array [1..1000] of string;b:array [1..10000] of string;m,n,i,j,t:longint;beginreadln(m,n);t:=0;for i:=1 to m doreadln(a[i]);for j:=1 to n doreadln(b[j]);for i:=1 to n dofor j:=1 to m doif pos(b[i],a[j])>0 thenbegininc(t);break;end;writeln(t);end.仙人吃牛肉varn,i,j,k,m,s:longint;a,b,c,d:int64;t,r :string;beginreadln(n);s:=0;for i:=3 to n-2 dobegina:=i;for j:=i+1 to n-1 dobeginb:=j;c:=a*a+b*b;d:=round(sqrt(c));if (d*d=c) and (d<=n) theninc(s);end;end;writeln(s);end.矩阵杨辉三角形vara:array [0..100,0..100] of longint;n,i,j:longint;beginreadln(n);fillchar(a,sizeof(a),0);a[1,1]:=1;for i:=2 to n dofor j:=1 to i doa[i,j]:=a[i-1,j]+a[i-1,j-1];for i:=1 to n dobeginfor j:=1 to i dowrite(a[i,j]:4);writeln;end;end.哥德巴赫猜想2varn,i,k,s:longint;a:array [1..3,1..100] of longint;function ss(a:longint):boolean;varj:longint;beginss:=true;if a<2 thenbeginss:=false;exit;end;for j:=2 to round(sqrt(a)) doif a mod j=0 thenbeginss:=false;exit;end;end;beginreadln(n);s:=0;for i:=1 to n div 2 dofor k:=i to n div 2 doif (ss(i)) and (ss(k)) and (ss(n-i-k)) thenif (i<=k) and (k<=n-i-k) thenbegininc(s);a[1,s]:=i;a[2,s]:=k;a[3,s]:=n-i-k;end;writeln(s);for i:=1 to s dowriteln(n,'=',a[1,i],'+',a[2,i],'+',a[3,i]); end.汉诺塔varn:longint;procedure hanoi(n:longint;a,b,c:char);beginif n=1 thenwriteln(a,' To ',c)elsebeginhanoi(n-1,a,c,b);writeln(a,' To ',c);hanoi(n-1,b,a,c);end;end;beginreadln(n);hanoi(n,'A','B','C');end.看电影var a:array[1..100]of longint;n,t,i:longint;beginreadln(n);for i:=1 to n do read(a[i]);for i:=1 to n div 2 dobegint:=a[i];a[i]:=a[n+1-i];a[n+1-i]:=t;end;for i:=1 to n-1 dowrite(a[i],' ');write(a[i+1]);end.打印成绩单var a:array[1..100]of longint;n,s,i:longint; beginreadln(n);for i:=1 to n do read(a[i]);for i:=1 to n dos:=s+a[i];writeln('total:',s);for i:=1 to n dowriteln(a[i]);end.美丽的黄山vara,b,t,n,i:longint;beginreadln(n);read(a);t:=1;for i:=2 to n dobeginread(b);if b>a thenbegininc(t);a:=b;end;end;writeln(t);end.奶牛的相似性vara,b:array [1..1000,1..1000] of shortint; m,n,i,j,t:longint;beginreadln(m,n);for i:=1 to m dofor j:=1 to n doread(a[i,j]);for i:=1 to m dofor j:=1 to n doread(b[i,j]);t:=0;for i:=1 to m dofor j:=1 to n doif a[i,j]=b[i,j] theninc(t);writeln(t);end.牛的速记var a:array ['a'..'z'] of longint;s:string; r,k:char; i,j,max:longint;beginreadln(s); max:=0;while length(s)<>0 dobeginfor i:=1 to length(s) do inc(a[s[i]]);for r:='a' to 'z' do if a[r]>max then begin max:=a[r]; k:=r; end;while pos(k,s)<>0 do delete(s,pos(k,s),1);if length(s)>0 then writeln(s);fillchar(a,sizeof(a),0); max:=0;end;end.烤面包varn,k,t:longint;beginreadln(n,k);n:=n*2;if n mod k=0 thent:=n div kelset:=n div k+1;if t=1 thent:=2;writeln(t);end.猪猪的反击varn,m,i,j,s:longint;a:array [1..100,1..100] of longint;beginreadln(n,m);fillchar(a,sizeof(a),0);for i:=1 to n dofor j:=1 to m doread(a[i,j]);i:=1;j:=1;s:=a[1,1];while i<n dobeginif j=m theninc(i)elseif a[i+1,j]<a[i,j+1] theninc(j)elseinc(i);inc(s,a[i,j]);end;writeln(s);end.计算N的阶乘varn:longint;function jc(a:longint):longint;beginif a<=1 thenjc:=1elsejc:=jc(a-1)*a;end;beginreadln(n);writeln(jc(n));end.北郊初级中学七(5)班戚博程。
PASCAL语言程序设计第一部分语言基础知识一、程序结构:例1.1计算半径为R的圆面积S{程序首部} program Area;{说明部分——数据描述}const pi=3.14159;{常量定义}var s,r:real;{变量定义}{执行部分——程序体}Beginreadln(r);{读入数据给r}s:=pi*sqr(r);{求圆的面积}writeln('s=',s);{在屏幕上输出面积}end.上述程序第一行称为程序首部。
其中用花括号(注释可以用{ }或(* *)来表示)括起来的内容是注释,注释除了给人看,增加程序的可读性外,对程序编译和运行不起作用。
一个程序可以包含多个出现在不同处注释,亦可无注释。
程序第二行是常量说明,程序第三行是变量说明。
程序从begin到end 都是执行(语句)部分(1)程序首部例1.1的第一行称为程序首部。
program是保留字,接着是程序名(由你依据“标示符”规则自行定义),最后以分号表示程序首部结束,下面是程序主体的开始。
程序首部在一个Turbo Pascal(仅在Turbo Pascal中有效)程序中并非必须出现,它是可选的。
写上它仅起了文档作用。
因此,在时间有限的情况下,如果用Turbo Pascal编程完全可以省略程序首部。
(2)程序体a.说明部分说明部分用于定义和说明程序中用到的数据,由单元说明、标号说明、常量说明、类型说明、变量说明、函数或过程说明组成,并且这些数据的说明次序必须按照以上次序。
但是一个简单的Turbo Pascal程序也可以不包含说明部分,也就是说说明部分是可选的。
b.执行部分执行部分描述了程序要执行的操作。
它必须以一个Turbo Pascal保留字begin开始,以保留字end后跟句点结束,其间是一些执行具体操作的语句,并且以分号作为语句之间的分隔符。
begin 和end必须成对出现,这是一个Turbo Pascal 程序所必须有的。
PASCAL 语言基础教程BY IcEBlast第一章 PASCAL程序结构一个典型的PASCAL程序是这样的:Program <程序名>Uses <调用的单元>Type <数据类型>Const <常量>Procedure <过程>Function <函数>BeginEnd.还有一个Lable,用来规定行号,不过我是恨死它了,因为它使你的程序变得没条理(记得GOTO吗),一般是禁止使用的。
如果你没有程序设计的基础,可能看不懂上面的结构,那么可以参照Pascal的帮助,或读下面的程序,我们还是从“Hello World”开始。
Program Hello_world;Uses crt;Const str=’Hello World’;BeginClrscr;Writeln(str);End.我用的是Word,发现拼写错误就不要追究了。
我们可以看到,Pascal程序是比较严格的,当然对初学者培养变成习惯大有好处。
有上机条件的话可以试一下。
差点忘了,注释的标志是 {,}, (*, *)注意括号配对。
下面介绍Pascal的表达式。
和其他编程语言类似,Pascal中使用+,-,*,/,()表示表达式,例如:1+2*4=9a-3+b/(a*5)Pascal中还提供一些数学函数,包括SIN,ARCCOS,LN,EXP等等。
请参见帮助中的SYSTEM单元。
请注意,这里没有以10为底的对数,所以应这样写LG(X): LG(X)=LN(X)/LN(10)这是换底公式。
请尝试读以下程序:Program aver;Var a,b,c,aver:integer;BeginReadln(a,b,c);Aver:=(a+b+c) DIV 3;Writeln(aver);End.该程序求三个数的平均数。
DIV表示整除。
作业:(1)求三个数的最大值(2)在屏幕上输出以下字符:***Welcome to Pascal world!***答案我就不提供了。
Pascal基础教程目录第一课初识PASCAL语言 (1)第二课赋值语句与简单的输出语句 (5)第三课带格式的输出语句输入语句 (12)第四课简单的分支结构程序设计 (19)第五课if嵌套与case语句 (23)第六课for循环 (29)第七课while循环与repeat-until循环 (30)第八课一维数组 (35)第九课多维数组 (39)第十课字符数组与字符串 (45)第十一课枚举、子界、集合及记录类型 (51)第十二课过程与函数 (66)第十三课动态数据类型(指针类型) (76)第十四课文件 (89)附录一Pascal中的字符串函数和数学函数 (111)附录二关于fillchar的使用和讨论 (116)附录三程序的调试技巧 (117)附录四Pascal的多种退出语句用法 (123)第一课初识Pascal语言信息学奥林匹克竞赛是一项益智性的竞赛活动,核心是考查选手的智力和使用计算机解题的能力。
选手首先应针对竞赛中题目的要求构建数学模型,进而构造出计算机可以接受的算法,之后要写出高级语言程序,上机调试通过。
程序设计是信息学奥林匹克竞赛的基本功,在青少年朋友参与竞赛活动的第一步必须掌握一门高级语言及其程序设计方法。
一、Pascal语言概述PASCAL语言也是一种算法语言,它是瑞士苏黎世联邦工业大学的N.沃思(Niklaus Wirth)教授于1968年设计完成的,1971年正式发表。
1975年,对PASCAL语言进行了修改,作为"标准PASCAL语言"。
PASCAL语言是在ALGOL60的基础上发展而成的。
它是一种结构化的程序设计语言,可以用来编写应用程序。
它又是一种系统程序设计语言,可以用来编写顺序型的系统软件(如编译程序)。
它的功能强、编译程序简单,是70年代影响最大一种算法语言。
二、Pascal语言的特点从使用者的角度来看,PASCAL语言有以下几个主要的特点:⒈它是结构化的语言。
一.简单题目1.高精度加法(1)数组法procedure add;var a,b,s:array[1..10000] of longint;i,j:longint;beginfor i:=1 to 10000 dobegins[i]:=s[i]+a[i]+b[i];if s[i]>9 thenbegins[i+1]:=s[i+1]+s[i] div 10;s[i]:=s[i] div 10;炸成林end;end;j:=10000;while (s[j]=0) and(j>1) do dec(j);for i:=j downto 1 do write(s[i]);end;(2)字符串法procedure add(s1,s2:string);var l1,l2:longint;i,j:longint;a:array[0..10000] of longint;s:string;x:longint;begins:='';l1:=length(s1); l2:=length(s2);if l1>l2 then for i:=1 to l1-l2 do s2:='0'+s2 else if l2>l1 then for i:=1 to l2-l1 do s1:='0'+s1;l1:=length(s1);for i:=l1 downto 1 dobeginx:=ord(s1[i])-ord('0')+ord(s2[i])-ord('0');a[l1-i]:=a[l1-i]+x;if a[l1-i]>9 thenbegina[l1-i+1]:=a[l1-i+1]+a[l1-i] div 10;a[l1-i]:=a[l1-i] mod 10;end;end;j:=l1;if a[j]=0 then dec(j);for i:=0 to j do s:=chr(a[i]+ord('0'))+s;writeln(s);end;2.高精度乘法(1)数组法procedure multiply;var i,j,k:longint;a,b,s:array[1..1001] of longint; beginfor j:=1 to 100 dobegink:=j;for i:=1 to 100 dobegins[k]:=s[k]+a[j]*b[i];s[k+1]:=s[k+1]+s[k] div 10;s[k]:=s[k] mod 10;inc(k);end;end;j:=1000;while (s[j]=0) and(j>1) do dec(j);for i:=j downto 1 do write(s[i]); end;(2)字符串法procedure multiply(s1,s2:string);var i,j,k:longint;l1,l2:longint;t,x:longint;s:string;a:array[0..1000] of longint;begins:='';l1:=length(s1); l2:=length(s2);if l1 >l2 thenbegint:=l1; l1:=l2; l2:=t;end;for i:=l1 downto 1 dobegink:=l1-i;for j:=l2 downto 1 dobeginx:=(ord(s1[i])-ord('0'))*(ord(s2[j])-ord('0'));a[k]:=a[k]+x;a[k+1]:=a[k+1]+a[k] div 10;a[k]:=a[k] mod 10;inc(k);end;end;if a[l1+l2-1]=0 then k:=l1+l2-2else k:=l1+l2-1;for i:=0 to k do s:=chr(a[i]+ord('0'))+s;writeln(s);end;3.混读字符串var n:longint;i:longint;name,mark:string;procedure inp;var s,s1:string;beginreadln(n);for i:=1 to n dobeginreadln(s);p:=pos(' ',s);s1:=copy(s,1,p-1);name:=s1;s1:=copy(s,p+1,length(s));mark:=s1;end;end;4. 进制转换(N进制-M进制)const st:string[16]=('0123456789ABCDEF'); var s:string;n:longint;a:array[1..100] of longint;procedure change; var i,j:longint;l:longint;beginreadln(n,m);readln(s);l:=length(s);for i:=1 to l dobeginfor j:=1 to 100 do a[j]:=a[j]*n;a[1]:=a[1]+pos(s[i],st)-1;for j:=2 to 100 dobegina[j]:=a[j]+a[j-1] div m;a[j-1]:=a[j-1] mod m;end;end;j:=100;while (a[j]=0) and(j>1) do dec(j); for i:=j downto 1 do write(a[i]); end;5.求两数的最大公约数function gcd(a,b:integer):integer; beginif b=0 then gcd:=aelse gcd:=gcd (b,a mod b);end;6.求两数的最小公倍数function lcm(a,b:integer):integer; beginif a< b then swap(a,B);lcm:=a;while lcm mod b >0 do inc(lcm,a); end;7. 素数(1).小规模判断function prime (n: integer): Boolean; var I: integer;beginfor I:=2 to trunc(sqrt(n)) doif n mod I=0 then beginprime:=false; exit;end;prime:=true;end;(2).判断longint范围内的数是否为素数(包含求50000以内的素数表):procedure getprime;vari,j:longint;p:array[1..50000] of boolean;beginfillchar(p,sizeof(p),true);p[1]:=false;i:=2;while i< 50000 do beginif p[i] then beginj:=i*2;while j< 50000 do beginp[j]:=false;inc(j,i);end;end;inc(i);end;l:=0;for i:=1 to 50000 doif p[i] then begininc(l);pr[l]:=i;end;end;{getprime(质数表)}function prime(x:longint):integer;var i:integer;beginprime:=false;for i:=1 to l doif pr[i] >=x then breakelse if x mod pr[i]=0 then exit;prime:=true;end;{prime(判断)}8.m的n次方var n,m:longint;procedure mn(i,s:longint);var t:longint; begint:=1;while i<>0 dobeginif i mod 2=1 then t:=t*s;s:=s*s;i:=i div 2;end;writeln(t);end;beginread(m,n);mn(n,m);end.9.简单排序(1).盲目排序procedure sort;beginfor i:=1 to n-1 dofor j:=i+1 to n doif a[j]<a[i] thenswap(a[i],a[j]);end;.(2). 选择排序:procedure sort;var i,j,k:integer;beginfor i:=1 to n-1 dobegink:=i;for j:=i+1 to n doif a[j]< a[k] then k:=j; {找出a[I]..a[n]中最小的数与a[I]作交换}if k< >i then begina[0]:=a[k];a[k]:=a[i];a[i]:=a[0];end;end;end;(3). 冒泡排序procedure sort;var i,j,k:integer;beginfor i:=n downto 1 dofor j:=1 to i-1 doif a[j] >a[i] then begina[0]:=a[i];a[i]:=a[j];a[j]:=a[0];end;end;(4).插入排序procedure sort;var x:longint;i,j:longint;beginfor i:=1 to n dobeginread(x);j:=1;while (a[j]<x) and (j>i) do inc(j);move(a[j],a[j+1],(i-j)*sizeof(a[j]));a[j]:=x;end;end;(5).下标排序法Procedure sort;BeginFor i:=1 to n do d[i]:=i;For i:=1 to n-1 doFor j:=i+1 to n doIf a[d[j]]<a[d[i] then swap(d[i],d[j]); End;10.排列与组合的生成(1).排列的生成:(1..n)procedure solve(dep:integer);vari:integer;beginif dep=n+1 then begin writeln(s);exit; end; for i:=1 to n doif not used[i] then begins:=s+chr(i+ord('0'));used[i]:=true;solve(dep+1);s:=copy(s,1,length(s)-1); used[i]:=false; end;end;(2).组合的生成(1..n中选取k个数的所有方案) procedure solve(dep,pre:integer);vari:integer;beginif dep=k+1 then begin writeln(s);exit; end;for i:=1 to n doif (not used[i]) and (i >pre) then begins:=s+chr(i+ord('0'));used[i]:=true;solve(dep+1,i);s:=copy(s,1,length(s)-1); used[i]:=false; end;end;11.折半查找function binsearch(k:keytype):integer;var low,hig,mid:integer;beginlow:=1;hig:=n;mid:=(low+hig) div 2;while (a[mid].key< >k) and (low< =hig) do beginif a[mid].key >k then hig:=mid-1else low:=mid+1;mid:=(low+hig) div 2;end;if low >hig then mid:=0;binsearch:=mid;end;二.Diggersun 复杂算法1.排序(1).归并排序{a为序列表,tmp为辅助数组} procedure merge(var a:listtype; p,q,r:integer); {将已排序好的子序列a[p..q]与a[q+1..r]合并为有序的tmp[p..r]}var I,j,t:integer;tmp:listtype;begint:=p;i:=p;j:=q+1;{t为tmp指针,I,j分别为左右子序列的指针}while (t< =r) do beginif (i< =q){左序列有剩余} and ((j >r) or (a[i]< =a[j])) {满足取左边序列当前元素的要求} then begintmp[t]:=a[i]; inc(i);endelse begintmp[t]:=a[j];inc(j);end;inc(t);end;for i:=p to r do a[i]:=tmp[i];end;{merge}procedure merge_sort(var a:listtype; p,r: integer); {合并排序a[p..r]}var q:integer;beginif p< >r then beginq:=(p+r-1) div 2;merge_sort (a,p,q);merge_sort (a,q+1,r);merge (a,p,q,r);end;end;{main}beginmerge_sort(a,1,n);end.(2).快速排序procedure qsort(s,t:longint);vari,j,x:longint;beginif t=s then exit;i:=s;j:=t;x:=data[s];while i<j dobeginwhile (i<j)and(data[j]>x)do dec(j);data[i]:=data[j];if i<j then inc(i);while (i<j)and(data[i]<x)do inc(i);data[j]:=data[i];if i<j then dec(j);end;data[i]:=x;if s<i-1 then qsort(s,i-1);if t>i+1 then qsort(i+1,t);end;2.树的遍历顺序转换A. 已知前序中序求后序procedure Solve(pre,mid:string);var i:integer;beginif (pre='') or (mid='') then exit;i:=pos(pre[1],mid);solve(copy(pre,2,i),copy(mid,1,i-1));solve(copy(pre,i+1,length(pre)-i),copy(mid,i+ 1,length(mid)-i));post:=post+pre[1]; {加上根,递归结束后post 即为后序遍历}end;B.已知中序后序求前序procedure Solve(mid,post:string);var i:integer;beginif (mid='') or (post='') then exit;i:=pos(post[length(post)],mid);pre:=pre+post[length(post)]; {加上根,递归结束后pre即为前序遍历}solve(copy(mid,1,I-1),copy(post,1,I-1)); solve(copy(mid,I+1,length(mid)-I),copy(post,I ,length(post)-i));end;C.已知前序后序求中序function ok(s1,s2:string):boolean;var i,l:integer; p:boolean;beginok:=true;l:=length(s1);for i:=1 to l do beginp:=false;for j:=1 to l doif s1[i]=s2[j] then p:=true;if not p then begin ok:=false;exit;end;end;end;procedure solve(pre,post:string);var i:integer;beginif (pre='') or (post='') then exit;i:=0;repeatinc(i);until ok(copy(pre,2,i),copy(post,1,i));solve(copy(pre,2,i),copy(post,1,i));midstr:=midstr+pre[1];solve(copy(pre,i+2,length(pre)-i-1),copy(post, i+1,length(post)-i-1));end;3.最短路径A.标号法求解单源点最短路径:vara:array[1..maxn,1..maxn] of integer;b:array[1..maxn] of integer; {b[i]指顶点i到源点的最短路径}mark:array[1..maxn] of boolean;procedure bhf;varbest,best_j:integer;beginfillchar(mark,sizeof(mark),false);mark[1]:=true; b[1]:=0;{1为源点}repeatbest:=0;for i:=1 to n doIf mark[i] then {对每一个已计算出最短路径的点}for j:=1 to n doif (not mark[j]) and (a[i,j] >0) thenif (best=0) or (b[i]+a[i,j]< best) then begin best:=b[i]+a[i,j]; best_j:=j;end;if best >0 then beginb[best_j]:=best;mark[best_j]:=true; end;until best=0;end;{bhf}(2).Dijkstra 算法:类似标号法,本质为贪心算法。
第一章Pascal语言和基础知识一、教学目标:1、了解Pascal言语的来源。
2、掌握Pascal的基本结构。
3、知道Pascal中的常用的标识符、常量与变量等。
二、教学重点:1、掌握Pascal的基本结构。
2、常用的标识符、常量与变量。
三、教学难点:常量与变量。
四、教学方法及设计采用讲解、演示、探究的教学方法。
五、教学手段:计算机网络机房或多媒体教室。
六、课时安排:2课时。
七、教学过程:教学过程手段与方法引入:1、编一程序,使屏幕出现如下图形:A AA A A AA A A A A AA A A A A AA A A AA A2、编写一个程序:求半径为r的圆的周长和面积。
3、注意:13+23=9;(1+2)2=9;13+23+33=36;(1+2+3)2=36成立,试编程证明对任何一自然数N(<30)都存在一个整数M使下式成立:13+23+33+···+N3=M2放映幻灯片后运行这三个程序计算机语言可分为低级语言(机器语言、汇编语言)和高级语言两种。
用汇编语言或高级语言编写的程序称为源程序。
源程序必须经过编译程序翻译成机器指令程序后才能被计算机执行。
一、Pascal程序的结构Pascal语言是沃思(N.Wirth)于60年代创立的,他提出一个著名的公式:程序=算法+数据结构,于1974年发表了Pascal用户手册报告,后来人们把这个报告确定为标准Pascal。
放映幻灯片后讲解1、Pascal程序的基本结构program<程序名称>(程序参数);{程序首部} <说明部分>;begin{语句部分}<语句1>;<语句2>;······<语句n>;end.放映幻灯片后讲解说明:①、每个程序都必须以program开头,每个语句之间以分号(;)隔开,程序名称由设计者自定,但必须以字母开头,后面可跟字母或数字,不能有空格,如3ex、e y等都是错。