82111668_2012版Pascal算法高级视频教程附带例题与答案
- 格式:docx
- 大小:37.23 KB
- 文档页数:22
raptor斐波那契数列n!的迭代求解-回复Raptor是一种流程图语言,用于解决问题和编写算法。
流程图是一种可视化的工具,用于描述问题的解决步骤和流程。
本文将使用Raptor来解决斐波那契数列n!(阶乘)的求解问题。
让我们一步一步来回答这个问题。
1. 什么是斐波那契数列和阶乘?斐波那契数列是一个数列,其中每个数字都是前两个数字之和。
数列的前两个数字通常定义为0和1,所以数列的前几个数字是0、1、1、2、3、5、8、13等等。
斐波那契数列可以用递归或迭代的方式来计算。
阶乘是一个数的所有正整数乘积。
例如,5!(5的阶乘)等于5 * 4 * 3 * 2 * 1 = 120。
2. 如何使用Raptor编写斐波那契数列n!的迭代求解算法?首先,我们需要创建一个流程图,描述算法的步骤和流程。
以下是一个简化的Raptor代码示例:start输入ninput n初始化变量set n1 to 1set n2 to 1set result to 1计算阶乘for i from 1 to n doset result to result * iend for输出结果output resultend在这个流程图中,我们首先输入n的值,然后初始化n1、n2和result 三个变量。
n1和n2分别用于保存斐波那契数列中的前两个数字,而result 用于保存阶乘的结果。
然后,我们使用一个循环来计算阶乘。
循环从1到n遍历,并将每个数字乘以result。
最后,我们输出result的值作为阶乘的结果。
3. 如何测试和运行Raptor代码?要运行Raptor代码,您需要一个Raptor编译器或解释器。
您可以在Raptor的官方网站上找到一些开源的Raptor实现。
在运行Raptor代码之前,我们可以使用不同的测试用例来验证算法的正确性。
例如,我们可以将n的值设置为5,这将给出5! = 120的结果。
我们还可以尝试其他一些较小或较大的n的值,以确保算法在各种情况下都能正常工作。
第九讲 分治算法一、分治思想分治思想融入于常见算法的各个角落,简单点说,所谓的分治,实际上是将一个问题拆成若干个小问题,将它们分别解决后,就等价于解决了整个问题。
二、快速幂算法与分治思想(底下部分的^表示乘方)求2^233(mod 1,000,000,007)这个问题我们可以这么考虑2^233 = 2^116 * 2^116 * 2那么原来问题就转化为求2^116(mod 1,000,000,007)就这样下去,我们只要求得2^52即可,也就是说2^26即可换句话说,我们求得2^13即可,也就是说2^6 * 2^6 * 22^6 = 2^3 * 2^32^3 = 2^1 * 2^1 * 2 = 8所以2^6 = 8*8=642^13 = 2^6 * 2^6 * 2 = 64 * 64 * 2 = 8192就这样…我们逐步回推,可以得到2^233当然注意要取模*你可以说这个算法是递归,但是这个也可以说是”拆成若干个小问题”*三、一道NOIP2007的例题本题摘自NOIP 2007 普及组初赛(完善程序第二题) 大家可以暂时思考一下本题中,我们只要求构造一种方案所以,这个题我们可以采取分治的策略来解决首先,我们思考一下,这一题的特殊条件1,我们用的是2的k次方*2的k次方的格子2,这一题中,有一个”-1”的存在那么,我们就可以用一种特殊的策略首先,如果k=0,那么……就不用构造了,皆大欢喜所以,我们想办法将2^k改为2^(k-1)即可我们可以将原来的正方形切成四个,如右图所以,这时候,我们可以将三个”1”当作-1,带入到右上,右下,左下处理,将”-1”依旧留在左上角这样,问题就转化成了一个2^(k-1)规模的然后..(本图中为了较为清晰的显示,将13~16与18~21略去了)所以,这样就将原来问题分治出来了那么,这一个问题就得到了解决四、一道来自codeforces的例题这一题摘自Codeforces 321C题面:英文版:C. Ciel the Commandertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputNow Fox Ciel becomes a commander of Tree Land. Tree Land, like its name said, has n cities connected byn - 1 undirected roads, and for any two cities there always exists a path between them.Fox Ciel needs to assign an officer to each city. Each officer has a rank — a letter from 'A' to 'Z'. So there will be 26 different ranks, and 'A' is the topmost, so 'Z' is the bottommost.There are enough officers of each rank. But there is a special rule must obey: if x and y are two distinct cities and their officers have the same rank, then on the simple path between x and y there must be a city z that has an officer with higher rank. The rule guarantee that a communications between same rank officers will be monitored by higher rank officer.Help Ciel to make a valid plan, and if it's impossible, output "Impossible!".InputThe first line contains an integer n (2 ≤ n ≤ 105) — the number of cities in Tree Land.Each of the following n - 1 lines contains two integers a and b (1 ≤ a, b ≤ n, a ≠ b) — they mean that there will be an undirected road between a and b. Consider all the cities are numbered from 1 to n.It guaranteed that the given graph will be a tree.OutputIf there is a valid plane, output n space-separated characters in a line — i-th character is the rank of officer in the city with number i.Otherwise output "Impossible!".Examplesinput41 21 31 4outputA B B Binput101 22 33 44 55 66 77 88 99 10outputD C B A D C B D C DNoteIn the first example, for any two officers of rank 'B', an officer with rank 'A' will be on the path between them. So it is a valid solution.中文题意:给你n个点,求构造一个方案,使得每个点存在一个’A’到’Z’的权值任意两个权值为x(x从’A’到’Z’)的点之间,一定存在至少一个权值比x大的点如果无解,输出”Impossible!”注意:’A’比’B’~’Z’大,’B’比’C’~’Z’大……’Y’比’Z’大如果不懂,可以观察样例所以,这道题我们该怎么做呢?我们仔细观察‘A’是最大的,也就是说,最多只有一个’A’那么我们每次选一个点,将它当作最大的点,然后,将它的其他部分”解离”也就是说,将它的其他部分分开处理这样,就可以解决了这题红点为A,然后将绿色点当B/C*但是,怎么找这个点呢*我们可以在这个树里面,随机一个点,但是是有可能得出无解我们这时候,只要找出”树的重心”即可这个树的重心可以百度得到……。
82111668_2012版Pascal视频教程附带例题与答案82111668_2012出品例1.1.1:交换a、b两数值定义 ca→cb→ac→b例3.1.1:program p3_1_1 ( input , output );var a : integer;begina:=100;write(a:10);end.program p3_1_2 ( input , output );var a : real;begina:=100;write(a:0:2);end.例3.1.3:读入两个实型,交换后输出,精确到0.01。
program p3_1_3_1 ( input , output );var a , b , c : real;beginreadln ( a , b );c:=a;a:=b;b:=c;writeln ( a:0:2 , b:0:2 );readln;end.program p3_1_3_2 ( input , output );var a , b : real;beginreadln ( a , b );a := a + b;b := a – b;a := a – b;writeln ( a:0:2 , b:0:2 );readln;end.例3.1.4:读入一个整数,将其平方后减去原数的绝对值,最后求值的平方根,输出最后的值,保留整数。
program p3_1_4 ( input , output );var n : integer;beginreadln ( n );writeln ( sqrt ( n * n - abs ( n ) ) : 0 : 0 );readln;end.例3.3.1:将两个整数大的放入max,小的放入min,并输出。
program p3_3_1 ( input , output );var max , min , t : integer;beginreadln ( max , min );if max<min thenbegint := max;max := min;min := t;end;writeln ( max , min );readln;end.例3.3.2:计算下列函数program p3_3_2 ( input , output );var x , y : integer;beginreadln ( x );if x<0 then y = -1else if x=0 then y = 0else y = 1;writeln ( y );readln;end.例3.3.3:读入三个数排序program p3_3_3 ( input , output );var a , b , c : integer;beginreadln (a,b,c);if a>b then if b>c then writeln ( a ,’ ’, b , ’ ’,c )else if a>c then writeln( a , ’ ’, c , ’ ’, b )else writeln ( c , ’ ’, a , ’ ’, b )else if c>b then writeln ( c , ’ ’, b , ’ ’, a )else if a>c then wr iteln ( b , ’ ’, a , ’ ’, c )else writeln ( b , ’ ’, c , ’ ’, a );readln;end.例3.5.1:输出从1到100所有的整数。
赛)把Pascal语言定为唯一提倡的程序设计语言,在大学中Pascal语言也常常被用作学习数据结构与算法的教学语言。
在Pascal问世以来的三十余年间,先后产生了适合于不同机型的各种各样版本。
其中影响最大的莫过于Turbo Pascal系列软件。
它是由美国Borland公司设计、研制的一种适用于微机的Pascal编译系统。
该编译系统由1983年推出1.0版本发展到1992年推出的7.0版本,其版本不断更新,而功能更趋完善。
下面列出Turbo Pascal编年史出版年代版本名称主要特色1983Turbo Pascal 1.0Turbo Pascal 2.0Turbo-87 Pascal提高实数运算速度并扩大值域1985Turbo Pascal 3.0增加图形功能Turbo BCD Pascal特别适合应用于商业1987Turbo Pascal 4.0提供集成开发环境(IDE),引入单元概念1988Turbo Pascal 5.0增加调试功能1989Turbo Pascal 5.5支持面向对象的程序设计(OPP)1990Turbo Pascal 6.0提供面向对象的应用框架和库(Turbo Vision)1992Turbo Pascal 7.0面向对象的应用系统、更完善的IDETurbo Vision 2.01993Borland Pascal 7.0开发 Object Windows库、(For Windows)提供对OLE多媒体应用开发的支持1995DelphiVisual PascalTurbo Pascal语言是编译型程序语言,它提供了一个集成环境的工作系统,集编辑、编译、运行、调试等多功能于一体。
1.2 Turbo Pascal 或 Borland Pascal 的启动(1) Turbo Pascal的启动a.DOS下的启动(适用于MS-DOS6.22之前的版本或Win 9X & Win2000 的Command Mode)DOS下,在装有Turbo Pascal的文件目录下,键入turbo即可进入Turbo Pascal集成环境。
Pascal教程第一章好,下面我们开始。
登入就会出现如图:输入用户名:Pasacal 密码:nbjboier出现如图然后,下拉,会出现如图打开Pascal。
会出现如图单击File会出现如图在单击New会出现如图:纯正的Pascal界面出来了,大家就可以写代码了。
第二章今天我们来学一下Pascal的程序结构。
1:var(Pascal命令表示变量定义)2:begin(Pascal必不可少的命令程序开始)3:end(表示程序结束)好,如图:这样,我们程序的框架就做好了。
本课小结:学会制作Pascal程序框架。
Var begin end第三章今天我们来学习简单的write和read语句,学会writeln,readln的运用,编译和运行,变量定义。
下面就出一个例题。
输入任意数,输出他们的和。
如输入23 1,输出24好,开始。
读入任意2个数(read(变量1,变量2),我们习惯a,b。
(重点:每句结束要加分号)接下来呢?当然是运算了!Write(变量1+变量2),记住,Pascal可以这样运算,牛吧。
如图好变量定义到了!先给大家介绍5种变量:integer(整形),longint(长整形),char(字形),string(字符型)还给大家介绍一种int64。
他的范围是64位的。
我们之定义了a,b,所以:选择变量定义:integer,longint,int64都行,但char,string就不行。
然后呢?编译啊!单击Compile文件夹里的compile 会出现保存界面,保存名自己定,然后会出现两种情况:这样的话,表示编译成功。
否则:等好了后,按Run文件夹里的Run就会。
输入两个数,按回车,再去Debug里的Debug看看结果。
对了吧,牛吧。
提高部分好,下面我们在做几个例题。
例题1:不用输入,直接输出I love Pascal!看到例题,先立好框架。
看到框架,我们会问,为什么没有var?直接输出,不用变量定义。
82111668_2012版Pascal高级算法视频教程附带例题与答案82111668_2012出品例:2.1.1 高精度加法program ex(input,output);typearr=array [1..256] of integer;var st1,st2,st3:string;sz1,sz2,sz3:arr;i:integer;procedure sum(st:string;varsz:arr);vari,code:integer;beginfor i:=1 to length(st) doval(st[length(st)+1-i],sz[i],code);end;functionfac:boolean;var j:integer;beginfac:=true;for j:=256 downto i+1 doif sz3[j]<>0 thenfac:=false;exit;end;end;procedure start;beginfor i:=1 to 256 dobeginsz1[i]:=0;sz2[i]:=0;sz3[i]:=0;end;end;beginstart;readln(st1);readln(st2);sum(st1,sz1);sum(st2,sz2);{字符串→数组} st3:='';for i:=1 to 255 dosz3[i+1]:=(sz3[i]+sz1[i]+sz2[i]) div 10; sz3[i]:=(sz3[i]+sz1[i]+sz2[i]) mod 10;end;{加法}for i:=1 to 256 dobeginst3:=chr(sz3[i]+48)+st3;iffac then break;end;{数组→字符串}writeln(st1,'+',st2,'=',st3);readln;end.例2.1.2.1 高精度减法_1program ex(input,output);typearr=array [1..65536] of integer; var st1,st2,st3:ansistring;sz1,sz2,sz3:arr;i:integer;procedure sum(st:string;varsz:arr); vari,code:integer;beginfor i:=1 to length(st) doval(st[length(st)+1-i],sz[i],code); end;functionfac:boolean;var j:integer;beginfac:=true;for j:=256 downto i+1 doif sz3[j]<>0 thenbeginfac:=false;exit;end;end;procedure start;beginfor i:=1 to 256 dobeginsz1[i]:=0;sz2[i]:=0;sz3[i]:=0;end;end;beginstart;readln(st1);readln(st2);sum(st1,sz1);sum(st2,sz2);{字符串→数组} st3:='';for i:=1 to 255 dobeginsz3[i]:=sz3[i]+sz1[i]-sz2[i];if sz3[i]<0 thenbeginsz3[i]:=sz3[i]+10;sz3[i+1]:=sz3[i+1]-1;end;end;{减法}for i:=1 to 256 dobeginst3:=chr(sz3[i]+48)+st3; iffac then break;end;{数组→字符串}writeln(st1,'-',st2,'=',st3);readln;end.例2.1.2.2 高精度减法_2program ex(input,output);type arr1=array [1..65536] of shortint;arr2=array [1..65536] of char;var sz1,sz2,sz3:arr1;s1,s2:arr2;n,m,i:longint;beginfor i:=1 to 65536 do begin sz1[i]:=0; sz2[i]:=0; sz3[i]:=0; end;n:=0;while not(eoln()) dobegininc(n);read(s1[n]);end;for i:=1 to n do sz1[i]:=integer(s1[n+1-i])-48;readln();m:=0;while not(eoln()) dobegininc(m);read(s2[m]);end;readln();for i:=1 to m do sz2[i]:=integer(s2[m+1-i])-48; for i:=1 to n dobeginsz3[i]:=sz3[i]+sz1[i]-sz2[i];if sz3[i]<0 thenbeginsz3[i]:=sz3[i]+10;sz3[i+1]:=-1;end;end;i:=n;while (i>0)and(sz3[i]=0) dodec(i);ifi=0 then writeln(0)elsebeginfor i:=i downto 1 do write(sz3[i]);writeln();end;readln;end.例2.3.1:插入排序programinsert_sort(input,output);vari,j,n,k:integer;a:array[1..100] of integer; beginreadln(n);for i:=1 to n do read(a[i]);readln;for i:=2 to n dobegink:=a[i];j:=i;while (j>=2)and(a[j-1]>k) dobegina[j]:=a[j-1];dec(j);end;a[j]:=k;end;for i:=1 to n do write(a[i]); writeln;readln;end.例3.3.1:先序遍历procedure first(p:point); beginif p=nil then exit;writeln(p^.data);first(p^.left);first(p^.right);end;例3.3.2:中序遍历procedure middle (p : point); beginif p=nil then exit;middle (p^ . left);writeln (p^ . data);middle (p^ . right);end;例3.3.3:后序遍历procedure last (p : point); beginif p=nil then exit;last (p^ . left);last (p^ . right);writeln (p^ . data);end;例3.3.4:树的宽度搜索program BFS(input,output); type point=^node;node = recorddata:integer;left,right:point;end;queue=objectconstmaxn=1000;data : array [1..maxn] of point;front , rear : integer;function empty():boolean;procedure add(n:point);end;functionqueue.empty():boolean;beginexit(front=rear);end;procedurequeue.add(n:point);begininc(rear);data[rear]:=n;end;varhead,p:point;q:queue;begin{输入二叉树。
}q[1]:=head;q.front:=0;repeatinc(q.front);ifq.data[q.front]^.left<>nil then q.add(q[q.front]^.left);ifq.data[q.front]^.right<>nil then q.add(q[q.front]^.right);writeln(q.data[q.front]^.data);untilq.empty();end.例4.1.1:求100以内所有素数program ex(input,output);var i:integer;functionfac(n:integer):boolean;var i:integer;beginfor i:=2 to trunc(sqrt(n)) doif n mod I = 0 thenexit(false);exit(true);end;beginfor i:=2 to 100 doiffac(i) thenwrite(I,’ ’);writeln();readln();end.例4.2.1:不定级数穷举代码段a:array of integer ; n : integer;fillchar (a , sizeof(a) , 1);repeati:=1;while a[i] = 11 dobegina[i]:=1;inc(a[i+1]);inc(i);end;{执行的语句}inc(a[1]);until a[n+1]=2;例4.3.1:N皇后program ex(input,output);varn,i:integer;a:array [1..100] of integer; functionfac:boolean;vari,j:integer;beginfor i:=1 to n-1 dofor j:=i+1 to n doif (a[i]=a[j])or(abs(a[i]-a[j])=abs(i-j)) thenbeginfac:=false;exit;end;fac:=true;end;procedureprintf;var i:integer;j:integer;beginfor i:=1 to n dobeginfor j:=1 to a[i]-1 dowrite(' ');write('*');writeln();end;writeln();end;beginreadln(n);for i:=1 to n+1 do a[i]:=1; repeati:=1;while a[i] = n+1 dobegina[i]:=1;inc(a[i+1]);inc(i);end;iffac() then printf;inc(a[1]);until a[n+1]=2;readln;end.例4.4.1:部分背包问题program 部分背包(input,output); {定义区}type node=recordw:integer;v:integer;end;arr=array [ 1..100] of node; varsz:arr;n,m:integer;{主程序}begin读入;按v/w排序sz;i:=0;while m<>0 dobegininc(i);ifsz[i].w<m thenbeginm:=m-sz[i].v;write(sz[i].w,' ',sz[i].v);endelsebeginwrite(sz[i].w,' ',m);break;end;end;readln;end.例5.2.1:线性动规——砍价问题(步步高升programStepByStep(input,output);typearr=array [1..1000] of longint;varw,z,a,b,i:integer;sz:arr;beginassign(input,'1.in');reset(input);readln(w,z);readln(a,b);close(input);fillchar(sz,sizeof(sz),0);sz[w]:=1;for i:=w-1 downto z dosz[i]:=sz[i+a]+sz[i+b];assign(output,'1.out');rewrite(output);writeln(sz[z]);close(output);end.例5.3.1:区域动规——统计单词个数programTotalOfWords(input,output); vartotal,i:integer;ch:char;beginassign(input,'1.in');reset(input);total:=0;while not(eof) dobeginread(ch);ifch in ['a'..'z','A'..'Z'] then begininc(total);while (ch in ['a'..'z','A'..'Z']) doread(ch);end;end;close(input);assign(output,'1.out');rewrite(output);writeln(total);close(output);end.例5.4.1:树形动规——数字三角形programNumberTriangle_high(input,output); varsz:array [1..4,1..4] of integer = ((1,0,0,0),(2,3,0,0),(4,5,6,0),(7,8,9,10));i,j:integer; function max(a,b:integer):integer;beginif a>b then max:=aelse max:=b;end;beginfor i:=3 downto 1 dofor j:=i downto 1 dosz[i,j]:=max(sz[i+1,j],sz[i+1,j+1])+sz[i,j];writeln(sz[1,1]);write('The root:');whilei<>4 dobeginwrite(sz[i,j]-max(sz[i+1,j],sz[i+1,j+1]),'-->'); ifsz[i+1,j]<sz[i+1,j+1] then inc(j);inc(i);end;writeln(sz[i,j]);end.例5.5.1:背包动规——0/1背包问题(完全背包问题)program bag(input,output);constmaxm=200;maxn=30;varm,n,i,j,k:integer;w,c,x:array [0..maxn] of integer;f:array [0..maxn,0..maxm] of integer;beginreadln(m,n);for i:=1 to n do readln(w[i],c[i]);for i:=1 to m do f[0,i]:=0;for i:=1 to n do f[i,0]:=0;for i:=1 to n dofor j:=1 to m doif j>=w[i] then if f[i-1,j-w[i]]+c[i]>f[i-1,j] thenf[i,j]:=f[i-1,j-w[i]]+c[i]else f[i,j]:=f[i-1,j]else f[i,j]:=f[i-1,j];k:=m;for i:=n downto 1 doif f[i,k]>f[i-1,k] then begin x[i]:=1; k:=k-w[i]; end else x[i]:=0;writeln(f[n,m]);for i:=1 to n doif x[i]=1 thenwrite(i,' ');readln();for i:=0 to n dobeginfor j:=0 to m dowrite(f[i,j],' ');writeln();end;end.例5.6.1.1:斐波那契数列求第n项值.递归公式functionfac(n:integer):integer;beginif n<=1 then fac:=1;elsefac:=fac(n-1)+fac(n-2);end;例5.6.1.2:斐波那契数列求第n项值.递推公式sz[0]:=1;sz[1]:=1;for i:=2 to n do sz[i]:=sz[i-1]+sz[i-2];例5.7.1:过河卒问题地推公式for i:=1 to 8 dofor j:=1 to 4 doif sz[i,j]<>-1 then sz[i,j]:=sz[i-1,j]+sz[i,j-1] elsesz[i,j]:=0;备注:课件可到/share/link?shareid=1727208655& uk=304560731下载。