|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | /*Name:HANOITOWER *Description:solvethehanoitowerproblembyrecursion */ #include<stdio.h> #include<stdlib.h> /*movenplates:from-->to, *thebuffercanbeusedifneeded*/ inthanoi(intn,charfrom,charbuffer,charto) { if(n==1) { /*movetheNO.1platedirectly:from-->to*/ printf("Moveplate#%dfrom%cto%c\n",n,from,to); /*theNO.1plateismovedsoreturn*/ return0; } else { /*nplatestobemoved:from-->to, *movethen-1platesabove:from-->buffer, *givethistasktothenextrecursion*/ hanoi(n-1,from,to,buffer); /*then-1platesaboveweremovedtobuffer, *sotheNO.nplatecanbemoveddirectly*/ printf("Moveplate#%dfrom%cto%c\n",n,from,to); /*howeverthen-1platesarestillinbuffer, *movethemtotheterminalposition, *(the"from"positionhasnoplate,&canbeoneso-calledbuffer)*/ hanoi(n-1,buffer,from,to); /*thetaskgivenisdonesoreturn*/ return0; } } intmain() { #defineN4 /*NplatesinA,let'smovethemtoC*/ hanoi(N,'A','B','C'); return0; } |
1 2 3 4 5 | //pascal procedurea; begin a; end; |
1 2 3 4 5 6 7 8 9 10 | //pascal procedureb; begin c; end; procedurec; begin b; end; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | //pascal programfac2; var n:integer; functionfac(n:integer):real; begin ifn=0thenfac:=1elsefac:=n*fac(n-1); end; begin write('n=');readln(n); writeln('fac(',n,')=',fac(n):6:0); end. |
1 2 3 4 5 6 7 8 9 10 11 12 | //pascal programlouti; varn:integer; functionf(x:integer):integer; begin ifx=1thenf:=1else ifx=2thenf:=2elsef:=f(x-1)+f(x-2); end; begin write('n=');read(n); writeln('f(',n,')=',f(n)) end. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | programkspv; var a:array[0..10000]oflongint; i,n:integer; procedurequicksort(l,r:longint); vari,j,mid:longint; begin i:=l;j:=r;mid:=a[(l+r)div2]; repeat whilea[i]<middoinc(i); whilea[j]>middodec(j); ifi<=jthen begin a[0]:=a[i];a[i]:=a[j];a[j]:=a[0]; inc(i);dec(j); end; untili>j; ifi<rthenquicksort(i,r); ifl<jthenquicksort(l,j); end; begin write('inputdata:'); readln(n); fori:=1tondoread(a[i]); writeln; quicksort(1,n); write('outputdata:'); fori:=1tondowrite(a[i],''); writeln; end. |
小黑屋|手机版|Archiver|鱼C工作室
( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)
GMT+8, 2025-7-3 15:56
Powered by Discuz! X3.4
© 2001-2023 Discuz! Team.