


下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、1L數(shù)字之和問題描述求岀1000以內(nèi)的整數(shù),使其數(shù)字之和為13,每行輸岀8個數(shù)。例如:數(shù)85,其數(shù)字之和為 8+5=13數(shù)373,其數(shù)字之和為 3+7+3=13變量說明I :百位數(shù)字 J:十位數(shù)字K:個位數(shù)字S:計數(shù)器,用于存放符合條件的數(shù)字個數(shù),以決定是否換行算法分析本題采用構(gòu)造法來造岀所有的二位數(shù)和三位數(shù),窮舉每位數(shù)字,判斷是否滿足條件。vari, j,k,s:integer;begins:=0;for i:=0 to 9 dofor j: =0 to 9 dobegink:=13-i-j;if ( k>=0 ) and ( k =9) thenbeginwrite (:5);s:=
2、s+l;if s mod 8=0 the n write inendendend.2.分?jǐn)?shù)計算問題描述輸入兩個真分子的分子與分母(分子、分母的合法性不作檢查),對這兩個分?jǐn)?shù)進(jìn)行加法計算。若符合條件,則應(yīng)將計算的結(jié)果化為帶分?jǐn)?shù)。例如:輸入25231+1/15 (帶分?jǐn)?shù)的表達(dá)形式)38184/8 (不作約分)121 2 1變量說明A、B、C、D :分別代表兩個分子和分母 I :循環(huán)變量S:兩個分子通分后的和算法分析1、求岀分母的最小公倍數(shù),可從兩個分母任選一個 整數(shù)則是最小公倍數(shù)。B開始到B*D之間的數(shù),看能否被整除,若一旦被子2、再對分子進(jìn)行通分求和,并判斷是否大于當(dāng)前的分母,若比分母大,則將其
3、化為帶分?jǐn)?shù)的表達(dá)形式,若比分母小,則不作約分,直接輸出,若相等,則輸出1。vara,b,c,d,i,s:i nteger;beginreadl n( a,b,c,d);for i:=b to b*d doif (i mod b=0) and (i mod d=0)the n break;a:=(i div b)*a;c:=(I div d)*c;s:=a+c;if s<ithen writeln(s,7',i)else if (s=i)then writeln(l)else write ln( 1s-i ,7i)end.1、上樓梯問題問題描述有一個N級的樓梯,N從鍵盤輸入,上樓梯
4、時一次可以上1級,也可以上 2級,也可以上 3級,問N級的樓梯共有幾種不同的上樓方案。程序說明用遞推方法求解。程序清單program fl_401;var n, a,b,c,d,i:i nteger;beginwrite(' n= '); readl n(n);if n=l the n c:=l;if n=2 the n c:=2;if n=3 the n c:=4;if n>3 thenbegina: =l;b:=2; c:=4;for i:=4 to n dobegind:= a+b+c :a:=b;b=c: c:=d end;en d;writel n( c);en
5、d.2、編寫用篩選法求l-n(n<200)以內(nèi)素數(shù)的程序分析:由希臘著名數(shù)學(xué)家埃拉托色尼提岀的所謂“篩選法”,步驟如下: 設(shè)所有待求素數(shù)的范圍為2 N ; 找岀其中最小數(shù)(必為素數(shù)),設(shè)為K,輸岀; 在2至N的所有數(shù)中,將K的所有倍數(shù)從中去掉; 重復(fù)?直到所有的數(shù)都被去掉,即找到了所有的素數(shù)。編程時,用數(shù)組作標(biāo)記的方法,a(k)=l表示保留某個數(shù),a(k)=O表示去掉某個數(shù)。源程序如下:program fl_402;varn ,i,j ,k,t,ok: in teger;a:array1.200 of i nteger;beginwrite(' n= '); readl
6、n(n);for i:=2 to n doa|T|:=l;t:=l;repeatok:=0;過;repeatj:=j+l;if aj=l the nbeginwrite? *);ok:=l;for k:=j to n doif kmodj=0 the n ak:=0;en d;un til (j>n) or ( ok=l );t=t+l ;un til t>n;end.3. 找數(shù)問題問題描述讀入若干個不相同且不為0的數(shù)X,以0作為輸入結(jié)果標(biāo)記,不用排序求岀其中第R個大的 數(shù)(R從鍵盤輸入),即有R-1個數(shù)比它大,其余的數(shù)都比它小。例如:輸入3, 14, 22, 15, 17, 6,
7、 0, 3,則輸岀其中第3個大的數(shù)為15算法說明以數(shù)組AL. 200記錄讀入的數(shù),并以 0為結(jié)束(0本身不是數(shù)列中的數(shù))。然后從第一個數(shù) 開始,將它 與其余的數(shù)進(jìn)行比較并記錄岀比它大的數(shù)的個數(shù)(存于變量Y中),若丫 =&1時,得到所求結(jié) 果,否則對下一個數(shù)進(jìn)行同樣的處理。程序清單 program fl_403; vari,j,k,p,x,y, in teger; a:arrayl .20 of in teger; beg in j: =0 ; readln( x); while xv>0do begi n j : =j+l ; aj : =x; read(x) ; end; re
8、adl n( r); P : =l ; i:=l; while p=l do beg inx:=a|T|y: =o: for k:=l to j do if x<ak the n y:=y+l; if y=r-1 the n beg in writel n( x); P end; end.:=0 : end else beg in i:=i+l; end;31、從鍵盤輸入兩個字符串入$和1?$,下面程序能找岀它們最長的公共子串,請完善程序。分析:所謂公共子串,就是兩個字符串中含有相同的子字符岀。program f2_401;var a, b, temp, d:stri ng;la, lb
9、, le n, j, k: in teger;beginwrite (J in put the str ing 1:');readl n(a);write (J in put the stri ng 2:');readl n (b);if le ngth(a)>=le ngth(b) the n begintemp:=a;a:=b;b:二 temp;end;la:=le ngth(a);lb:二 length(b);for len:=la dow nto 1 dofor j:=1 to la+l-le n dobegind:二 copy (a, j, len):for k
10、:=l to lb+1-le n doif d=copy(b, k, le n) the nbeginwritein ( halt;wend2、en d;end;/ritein ('no fin t!');面程序的功能是:讀入N個數(shù),用折半插入法進(jìn)行從小到大排序。請完善程序。分析:所謂的折半插入法是在確定插入位置時先將插入數(shù)據(jù)與已有序數(shù)據(jù)的中間數(shù)據(jù)比較,以確定插入位置是在中間位置前還是后,然后再與前或后半部數(shù)據(jù)的中間數(shù)據(jù)比較,直到這部分?jǐn)?shù)據(jù)只有一個即找到應(yīng)插入program f2_402;varn, m, j, k, g, h, temp: in teger;a:array1.
11、 100 of in teger;beginwrite C n = ); readl n(n);for j:=1 to n doreadl n(aj);for j:=2 to n dobegintemp:=aj; g:=1; h:=j-l;repeatm:=(g+h+l) div 2;if temp =amlthen h:=m-1 else g:=m+1;un til g>=h;for k:=j-l dow nto g doak+ll:二 akl ;Mg:二 temp ;en d;for j:=l to n dowritel n(aj);write in;end.3、輸入2個整數(shù)K, N
12、,將K分成N個全不相同的整數(shù)的和,并使此 N個整數(shù)的乘積為最大。請完善程序。例如:輸入16,3,則輸岀4, 5, 7,因為4+5 + 7 = 16,而且4*5*7 = 140乘積最大。注意,當(dāng)這N個數(shù)越平均(即越接近),它們的乘積越大。如:5與7的和為12,乘積為35, 4與8的和也 為12,它們的乘積為32。下面程序中數(shù)組 A存入N個數(shù)且N小于200, K大于N* (N+1) /2。program f2403;varn, k, s, j, h, hl: i nteger;a:array1. . 200 of i nteger;beginwrite (' k=');readl
13、n (k);write C n = ); readl n( n);s:二 n*(n+1)div 2 -;for j:=1 to n do一 aj :=j ;k:=k-s;h:二 k div n ;hl:二 k-h*n;for j:=1 to n doaj:=aj+h;j: =n;while hl<>0 dobeginaj :=aj+l; hl:=hl-l;_ j :二 j- _; en d;for j:=1 to n do write(aj,'');write in;end.1、問題描述:給岀一個正整數(shù) N (1WNW100),即可得到一個由 N個正整數(shù)組成的從1開
14、始的如下數(shù)列:1,2, 3,4, N-2,N-1,N,且可求岀從1開始的這N個數(shù)的全部各個數(shù)位上的數(shù)字之和。例如:當(dāng)N=12時,這 12 個數(shù)是:1,2, 3, 4, 5, 6, 7, 8, 9,10,11,12。則這 12 個數(shù)的數(shù)字之和為: S=l+2+3+4+5+6+7+8+9+l+0+l+l+l+2=51程序如下:program dgcl_401;varn,s,x,y,j integer;beginreadl n(n);s:=0;for j:=l to n dobeginX : =j;while xv>0 dobeginy:=x mod 10;s:=s+y;x: = x:=x d
15、iv 10;en d;en d;writel n('s=',s);end.2、 下面程序能找岀所有不含數(shù)字為0的六位完全平方數(shù),且這個六位數(shù)中至少有三個同樣的數(shù)字。所謂完全平方數(shù),是指它是某個數(shù)的平方,如111556是334的平方,其中數(shù)字 1岀現(xiàn)了三次,是一個符合條件的數(shù)。請完善程序。例如:334*334=111556。program dgcl_402;varj,m,bj,bj2: in teger;n, t,i:l ongint;C:array1. . 9 of in teger;beginfor i:=327 to 999 dobeginn:二i*i ;for j:=1
16、to 9 do Cj:=0 ;t :=n;bj:=l;for j:=l to 6 dobeginm:=n mod 10;if m>0 then cm: 二 cm+l else bj:=0;n:=n div 10;en d;if bj=l the nbeginbj2:=0;for j:=1 to 9 doif cj>=3 the n bj2:=l;if bj2=l then writeln(i,' ', t);en d;en d;end.3、隨機(jī)數(shù)問題本程序?qū)﹄S機(jī)產(chǎn)生的100個0到50之間的隨機(jī)整數(shù)用一個數(shù)組存放后進(jìn)行排序,然后再將其中重復(fù)岀現(xiàn)的數(shù)進(jìn)行刪除,只保留一個
17、,使得剩下的數(shù)中任何兩個都不相同且連續(xù)存儲在原數(shù)組中?!境绦蚯鍐巍縫rogram dgc 1 _403;varmaxn,current,tail,i,j ,temp: integer;a:array 1. 100 of in teger;beginmax n:=100;ran domize;for i:=l to max n doai:=tru nc(ra ndom(51);for i:=l to maxn-1 dofor j: =i±± to maxn doif ai<aj the nbegintemp:=ai;ai:=aj;aj:=temp;en d;for i:
18、=2 to maxn doif ai=abs(ai_l) then ai:=-aT| :tail:=0;curre nts 1;while(curre ntv=max n) and (alcurre ntv>0) dobeginwhile acurre nt<0 docurre nt: =curre nt+1;tail:=tail+l;a tail: =alcurre nt;curre nt: =curre nt+1;en d;if (atail<>0) and (amaxn=0) thenbegintail:=tail+l;atail:=O;en d;for i:=l
19、 to tail dowrite(ai;,);write in;end.51、下列程序的功能是:有N個評委打分,去掉一個最高分和一個最低分,求余下的總分。請將程序補(bǔ)充完善。program c2_401;var s, n, f, max, min, i, jg: in teger;begins:=0;write (J n=') ; readln(n);write (J fen = ) ; readln(f);max:=f; min :=f; s:=f;for i :=2 to n dobeginwrite C fen='); readln(f);if f>max thenm
20、ax,二 f ;if fvmin the nmin:=f ;s:=s+f;en d;j g: =s - max - min;write in (jg);en d.2、 有一數(shù)列,第一、第二兩個數(shù)分別為1、2,其后每個數(shù)都為前兩個數(shù)之和。下面程序的功能是打印岀數(shù)列中所有小于3000的數(shù),請將程序補(bǔ)充完善。program c2_402;vara, b,c:i nteger;begina:=l; b:=2;write (a,'');while b<3000 dobeginwrite (b,");c:二 a+b ;a:=b ;b:=c ;en d;en d.3、給定10
21、個數(shù),使用冒泡法將它們從小到大的次序排列后輸岀。請將程序完善。 算法分析:(1) 從A (1) -A (9)為止,把相鄰的兩個數(shù)兩兩進(jìn)行比較。(2) 在每兩個數(shù)的比較過程中,若前一個數(shù)比后一個數(shù)大,則對調(diào)這兩個數(shù)。(3) 比較完一輪后,若曾進(jìn)行對調(diào)操作,則從(1)開始重復(fù),否則排序結(jié)束。program c2_403;vari, t, f:integer;a:array1. . 10 of in teger;beginfor i:=1 to 10 doread(ai);for i:=1 to 10 dowrite(ai,'');writei n;repeatf:二 0;for i
22、:=1 to 9 dobeginif aj < Mj+l the nbegint:=ai;ai :=ai+l; ai+l :=t;f: =1 ;en d;en d;un tilf二-0 ;for i:=1 to 10 dowrite(ai,'');write in;en d.4、下列程序是將兩個已排好次序 (從小到大)的數(shù)列A、B合并為一個數(shù)列C,使合并后的C也按從 小到大的次序排列。A、 B數(shù)列的元素個數(shù)由鍵盤輸入。如:A 數(shù)列:3, 6, 9,18B 數(shù)列:2, 7, 11則 C 數(shù)列為:2, 3, 6, 7, 9, 11, 18試將程序完善。算法分析:(1) 若A
23、(I)仁B (I),則將A (I)的數(shù)放入C數(shù)列中,否則 B (I)的數(shù)入C數(shù)列中。(2) 重復(fù)步驟(1),直到A或B數(shù)列中有一列完全傳到C列中。(3) 將A或B中余下的元素直接傳到 C數(shù)列中。program c2_404;vara, b:array1. . 100 of in teger;c:array1. . 200 of integer;i, j, k, t, m, n, x: in teger;beginwrite (J shu ru A, B shu lie de ge shu );readl n(m, n);write C shu ru A shu lie:');for i
24、:=1 to m do readl n( ai);write C shu ru B shu lie:');for i:=1 to n doreadl n(bi);i:二 1; j:=l; k:=l;repeatif ai<=bj the nbeginck:二 aiL;i:=i+l; k:=k+l;endelsebeginck :=bj;j: 二 j+1 ; k:二 k+1;en d;un til (i>m) or j>n;if i>m the nfor x:=j to n dobeginck :=bx; k:=k+l;endelsefor x:二 i to m
25、dobeginck:=ax;k:=k+l;en d;for i:=1 to m+n dowrite(ci,'');write in;end.1.【問題描述】給岀一個正整數(shù) N (NV32768),要求將其分解成質(zhì)因子的連乘積。例如:當(dāng)N=24時,結(jié)果為:24=2*2*2*3(A)又如:當(dāng)N=13時,結(jié)果為:13=13(B)輸入:N輸岀:如(A)或(B)的格式結(jié)果?!咀兞空f明】n:表示輸入的正整數(shù)。k:表示質(zhì)因子?!舅惴ǚ治觥繌淖钚〉馁|(zhì)因子數(shù) 2開始,判斷n能否被2整數(shù),若能,輸岀2, n縮小2倍,仍舊用2去整除,看n能否被2整除,若不能被 2整除,則取3,判斷能否被3整除,若能
26、,輸岀3, n縮小3倍,仍舊用3整除,直到不能。然后再取4, 5,直到n的值為1為止。varn ,k: in teger;beginreadl n(n);write( n/=');while Inoll dobegink: =l 2 I ;while I (n mod k<>0) and(k<n)I dok:=k+l;n:= I n div k|;if nolthen write(k,'*')else write ;k|);en d;en d.2、【問題描述】任給4個非0的不同數(shù)字,求岀由這 4個數(shù)字組成的所有 4位數(shù)。 例如:輸入:3 2 16輸出:
27、123612631326136216231632213621632316236126132631312631623216326136123621612361326213623163126321程序要求:從鍵盤輸入4個非0的不同數(shù)字(輸入的正確性不作檢查 ),列岀由這 4個數(shù)字組成的所 有4位數(shù),每行輸出6個?!咀兞空f明】a:數(shù)組,存放從鍵盤讀入的 4個數(shù)。 s:計算所得的組合總數(shù),以便控制換行。 P:表示構(gòu)造岀的 4位數(shù)?!舅惴ǚ治觥坎捎酶F舉法列舉所有可能的組合,由于窮舉法的效率較低,我們應(yīng)盡量多的排除循環(huán)變量值相同的情況(比如:i=l,j=l,k=l,l=l),逐次找岀所有的組合,然后輸岀即
28、可。由于程序中的輸入數(shù)據(jù)是任意排列的,所以增加了將四個數(shù)排序的程序段。vari,j,k,l,t,s,p:i nteger;a:array1.4 of in teger;beginfor i:=l to 4 doread(ai);for i:=l to 3 dofor j:=i+l to 4 do if I_the n beg int: =ai;ai:=aj; aj : =t;en d;s:=0;for i:=l to 4 dofor j:=l to 4 do if | the nfor k:=l to 4 doif (iok) and (jok) the nfor 1:=1 to 4 doif
29、 I (iv>l)an d(iv>l)a nd(kv>l)Ithe nbegins:=s+l;p:= I aniFOOO+aHiroo+alkiro+aH;write(p:10);if | s mod 6|the n writei n;en d;en d.71、取蘋果問題有K箱蘋果,分別裝有1,2, 4, 8,個蘋果,現(xiàn)要從中取岀 N個蘋果岀來,取果的規(guī)則是要么一個也不取,要么取完這一箱蘋果。從鍵盤輸入N和K,編程序求岀該取哪幾箱蘋果?如果無法按要求取岀蘋果,則輸岀 “NO”注:2't表示KT個2相乘。分析:因為任何一個十進(jìn)制數(shù)都可以轉(zhuǎn)化為二進(jìn)制數(shù),如:(13) 10
30、= (1101) 2=1*2 3+1*2 2+0*2 1 + 1*2。即要取 13 個蘋果可以取1,4, 8 箱的蘋果,當(dāng) S>l+2+4+8+2時無法按要求取得。因此,問題轉(zhuǎn)化為將N轉(zhuǎn)化為二進(jìn)制,即可找到取蘋果方案。程序如下:program dgc401;var k,n ,ru: in teger;i,j,bj in teger;t,s:l ongint;a,b:arrayO. 100 of in teger;Beg inWrite('k=*); readl n(k);Write(' n= '); readl n(n);S:=0; bj:=l;For i:=l
31、to k doBeg inT:=l ;For j:=l to 1-1 doT:=t*2Al-l:=t;S:=s+t;End;If svn the n bj:=O; if bj=l the n Beg inT:=0;RepeatRu:=n mod 2;n:= n div 2;B t := ru :T:=t+1;Until n=0;End;If bj=O then writel n('n o')Elsefor i:=0 to t-1 doif bi=l then write(ai/');writei n;End.Al表示),,則在表中沒有2、二分查找二分查找是一種常用的高效
32、的查找算法,算法過程是:在一列已按從小到大排好序的數(shù)列中(用找值為X的元素,若找到則輸岀它的位置(下標(biāo)),若找不到,則輸岀 “NO。分析:首先將要查找的值與中間位置的數(shù)據(jù)值比較,若相等,則輸出結(jié)果,不等,若小于中間位置值 左邊子表中按同樣的方法找,若大于中間位置值則在右邊子表找,直到找到滿足條件的數(shù)據(jù)或者確定 這樣的數(shù)據(jù)。程序如下:program dgc402; var a: array1. 100 of in teger;mid,low,hig: integer; find: boolean;temp, n,x,h,j: i nteger;beginwrite(' n=');
33、 readl n(n);for j:=l to n do read(aj);for j:=l to n-1 dofor h:=j+l to n doifthe nbegintemp:=aj; aj:=ah; ah:=temp;end;write('x= '); read ln( x); fin d:=false;Iow:=l; hig:= nj_while (low<=hig) and no t(fi nd) dobeginmid:=(low+hig) div 2;if x=amid then find:=true else beginif x<amid the n
34、 hig:=mid-1;if x>amid the n low:= mid+1 ;en d;en d;if find the n write ln( x,*is the no: 1,mid) else write ln (' no fin d!*);end.3、比賽安排設(shè)有N (N<=64)個球隊進(jìn)行單循環(huán)比賽,計劃在 N-1天內(nèi)完成,每個隊每天進(jìn)行一場比賽。設(shè)計一個比賽的安排,使在N-1天內(nèi)每個隊都與不同的對手比賽。例如n=4時的比賽安排:隊1 23 4比賽1=23=4一天1=32=4二天1=42=3三天數(shù)據(jù)輸入輸出:從鍵盤輸入N的值,將比賽安排結(jié)果輸出到屏幕數(shù)據(jù)輸入輸岀
35、樣例:輸入:N=4輸出:1-2.3-41-3,2-41-4,2-3算法分析:枚舉每天的比賽情況,如果某天的隊I沒比過賽,隊j也沒比過賽,而且隊 I與隊j以前沒比過賽,則安排隊I與隊j比賽。program dgc403;vari,j,k, m,n:i nteger;a: array 1.64,1.64of i nteger; b:array1.64o f in teger;beginwrite('N='); readl n(n);for i:=l to n dofor j:=l to n doai,j : =0;for i:=l to n do ai,i:=l;for m:=l
36、ton-1 dobeginfor i:=l to n dobi:=0;for i:=l to n doifbi=Othe nfor j:=l to n doif ( blil=O ) and (ai,il=O) then beginbi:=1;bj:=l;a i,j:=l;if iol then write(*,');write。, Uj);break;en d;writei n;en d;end.81、奇幻方問題在一個N*N的方陣中,N為奇數(shù),將1至N*N填入方陣的每個格,使每行每列及兩對角線數(shù)字的和都相等,這樣的數(shù)據(jù)陣稱為奇幻方。下面是N-3和N-5時的一種填法。N從鍵盤輸入。17
37、241815816235714163574613202249210121921311182529程序如下:program dgf401;vara:array1.99,1.99 of i nteger; n ,l,j,k:i nteger;beginwrite('n=J ;readln(n);for i:=l to n dofor j:=l to n doai,j : =O ;i:=l;j := (n+l)div2 ;ai,j : =l;for k:=2 to n*n dobeginif (k-1) mod n=0 then i:=i+lelse beg ini:=i-l; if i=0
38、 the n i:=n ;j:=j+1; if j=n+l the n j:=l;en d;ai,j : =k;en d;for i:=l to n dobeginfor j:=l to n dowrite(ai,j,' *);write in;en d;end.2、數(shù)字三角形數(shù)學(xué)老師在黑板中寫了一個有趣的數(shù)字三角形,如下圖。你能編程序由計算機(jī)自動生成如下形式的數(shù)字三角形嗎?請觀察下列規(guī)律,根據(jù)從鍵盤輸入的N值(NV20=,在屏幕上輸岀相應(yīng)的數(shù)字三角形。輸入樣式:N=5輸出樣式:112 21113310 1514 498765參考程序如下:program dgf402;varx, n,
39、 i, j, s, 1, p: i nteger;a:arrayl. . 20, 1. 20of integer;beginreadl n(n);s:=n;x:=0;i: =0;J: =0;repeat for 1:=1 to s do begi nmaxa=1000;vari, j, s, t, m, k: in teger;a:array1. . maxa of in teger;n :l ongint;beginreadl n(k);readl n(n);for I:=1 to n do aI:=0;al : =1 ;i:=0;repeati:=i+l;j:二 maxa;while aj
40、=0 do j:=jl;t :=0;for s:=1 to j+2 dobegin as *k+t;t:=m div 10;as : =m mod 10en d;un til i=n;j:=maxa;while aj=0 do j :=j-l writei n;for s:=j dow nto 1 dowrite(as:2);write in;en d.91. 余料最少問題:有一根長514cm的鋼筋,先要將它截為長為21cm, 19cm, 16cm的三種規(guī)格,要求每種規(guī)格長度至少要截一根,問如何截取才能夠使剩余的鋼筋最少。輸岀:21cm:根數(shù)19cm:根數(shù)16cm:根數(shù)參考程序如下:progr
41、am dgf401;var i,j,k:i nteger;x, y, z, left, a, b: in teger;begina:二 514;left:二 a ;for i:=l to (514-19-16)div 21 dofor j:=1 to (514-21*1-16)div 19 dofor k:=l to (514-21*iT9*j)div 16 dobeginb:二 514-21*i-19*j-16*k;if b<left fhe n begi nleft:=b; x:=i;y:二 j; z:二 k;en d;en d;writei n(' 21cm:',
42、x);5writei n( 19cm:', y);write in ('16cm:', z);end.2. 排序問題排序的方法很多,下面也是一種排序的方法,現(xiàn)在要你輸入n個0到100之間的整數(shù),將輸入的N個整數(shù)由小到大排序輸出,每行輸出8個。程序清單:program dgf402;varI, j, k, n, x: in teger;b: array0. 100of i nteger;beginreadl n(n);for i:=0 to 100 do bi:=0;for i:=l to n dobeginread ln( x);b x: = b|x+l ;end;k:
43、=0;for i:=0 to 100 dowhile bi>0 dobeginwrite(i:4);k:=k+l;b i := bi1-l :if k mod 8=0 the n write in; en d;readl n;end.3方陣填數(shù)問題在一個NXN的方陣中,填入 1,2.NXN個數(shù),并要求構(gòu)成如下的格式:如N = 4N= 510 11 12 113 14 15 1619 16 13212 23 2422 25 183765410 21 20 19498765【參考程序】program dgf403;Varj: I nteger;n ,i,k,x,y,
44、Number:Byte;Result: Array1.25,1.25 of Byte;Beg inWrite('N='); Readl n(n);For x:=l to n dofor y:=l to n do Resultx,y :=0:X:=0; Y:=N; i:=0;k: = n_ ; j: = LWhile ivn*n doBeg inFor i:=i+l to i+k doBeg inX:=X+j; Resultx,y:=i;En d;k:= k-1 :If j=l thenj:= -1_else j:=l;For i:=i+l to i+k doBeg iny: =
45、y+j ;Resultx,y:=i;En d;En d;For x:=l to n doBegi nFor y:=l to n do Write(Resultx,y :3); Writei n; En d;End.101約瑟夫問題“六一”兒童節(jié)到了,小李班上舉行了一個圍圈圈的游戲活動。活動是這樣的,共有N位同學(xué)圍成一 個圓圈,然后順時針依次編號為1、2、N,然后從編號為1的人開始報數(shù),每報到M的同學(xué)岀列,然后繼續(xù)從下一位同學(xué)開始報數(shù),每報到 M的同學(xué)又岀列,如此下去,直到所有的同學(xué)岀列為止。現(xiàn)在要你編一個程序,輸入N、M的值,依次輸岀岀列的同學(xué)的編號。程序如下:program dgc401;v
46、arn, s,m,i,t:i nteger; a:array1.100 of in teger;beginwrite(' n= '); readl n(n);write( 'm='); readln(m);for i:=l to n do a i:=l :i:=0; s:=0; t:=0;repeati:=i+l;if i>n the n i:=l;s:=s+ai;if s=m the nbegint:=t+l;ai:=O;s:=0;write (i ;'):en d;un til t=n :writei n;end.2、素數(shù)乘法算式問題。在下面的
47、乘法算式中,”*”號只能用素數(shù)2, 3, 5, 7代替,因此稱為素數(shù)乘法算式。編程找岀素數(shù)乘法算式所有可能的方案,并按算式的格式打印岀來。算式格式如下:程序如下:program dgc402;vard:array 1.4 of in teger;t,i 1 ,i2,i3,i4,i5,k,j dongint;a,b,c:lo ngi nt;check:boolea n;begindl:=2; d2:=3; d3:=5; d4:=7;t:=0;for il:=l to 4 dofor i2:=l to 4 dofor i3:=l to 4 dofor i4:=l to 4 dofor i5:=l
48、to 4 dobegink:=di 1 * 100+di2 * 10+di3; j:=di4*10+di5;check:-true ;a:=k*j;遷 a>77777 then check:=falseelsefor c:= dobeginb:=a mod 10;if (b<>2)a nd(b<>3)a nd(bo5)a nd(b<>7) the n check: =false; a:=a div 10;end;en d;writel n( 'Total=',t);end.3、真分?jǐn)?shù)問題。從3, 5, 7, 9, 11, 13, 15這
49、7個數(shù)中任取兩個數(shù)組成真分?jǐn)?shù),共有多少個?找岀組成的所有真分?jǐn)?shù)程序如下:program dgc403;varx,y,a,b,c,d:i nteger;z:boolea n;beginx:=3;y : =5 ;z:=true; a:=0;write in;write in;while x<=13 dobeginwhile yv=15 dobeginfor d:=2 to x doif (x mod d=0) and (y mod d=0) the n z:=false;if z=true the nbegina:=a+l ;writel n(x,7',y);en d;y: =y+2
50、 ;en d;z:=true;x:=x+2y:=人 ±en d;writel n( 'a=',a);en d.1 1第1題:連續(xù)自然數(shù)之和對?個給定的自然數(shù)M,求岀所有的連續(xù)的自然數(shù)段,這些連續(xù)的自然數(shù)段中的全部數(shù)之和為M。例子:1 998+ 1 999+ 2000+ 2001 + 2002 = 10000,所以從 1998 至U 2002 的一個自然數(shù)段為M= 10000 的一個解。下而的程序是求給定的 M的所有連續(xù)自然數(shù)段,輸岀所有連續(xù)數(shù)段的起始位置和終止位置(小數(shù)在前,大數(shù)在后)。算法提示:S表示連續(xù)自段之和,L表示當(dāng)前自然數(shù)段左邊的數(shù),r表示當(dāng)前自然數(shù)段右邊的
51、數(shù),如果當(dāng)前S小于M,則在把右邊的數(shù)加入,如果S的值大于M,則把左邊的數(shù)減去,如果 S和M相等,則輸 岀。varnsm L,r5S:lo ngint;beginreadl n( m);L:= 1 ;r:= 1 :s:=1:while r< = m doifonthe n begi nwriteln(L,' ',r);s: = s-l;L:二 L+lendelse if s< m怪的題目,隨機(jī)產(chǎn)生10000個1億以內(nèi)的數(shù),統(tǒng)計素數(shù)岀現(xiàn)的概率(素數(shù)的個數(shù)除以10000的 值),小明是一個十分聰明的小孩,他掌握了篩選法、試除法(將該數(shù)N用小于等于妨的所有數(shù)去試除,若均無法
52、整除,則N為素數(shù)。)來判斷素數(shù)。小明平時訓(xùn)練總是嚴(yán)格要求自己,做題精益求精(這是成為大牛的必備條件)。他發(fā)現(xiàn)試除算法是可以改進(jìn)的:首先用篩選法來建立素數(shù)表,求岀2-10000以內(nèi)的所 有素數(shù)。然后再用試除法,即將該數(shù)N用小于等于妨的所有素數(shù)去試除,若均無法整除,則N為素數(shù)。當(dāng)然,如果隨機(jī)產(chǎn)生的數(shù)小于10000,則可以直接判斷。下面是小明寫的部份程序,請你完善它。數(shù)據(jù)結(jié)構(gòu)設(shè)計:數(shù)組P用來存貯1 -10000以內(nèi)的素數(shù)。數(shù)組F用來標(biāo)記1 -10000以內(nèi)的素數(shù),如果是素數(shù),則pi=1,否則pi = 0;ans表示產(chǎn)生素數(shù)的總數(shù),s表示隨機(jī)生的數(shù),flag用來標(biāo)記是否是素數(shù)。varan s,s,kJJ: longint;f: array0 . 1 0000 of i
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度餐廳服務(wù)員職業(yè)發(fā)展規(guī)劃與晉升合同
- 二零二五年度汽車美容店市場營銷人員用工合同規(guī)范
- 二零二五年度工傷賠償協(xié)議范本(服裝行業(yè))
- 2025年陽江貨運(yùn)從業(yè)資格證考試技巧
- 2025年武漢貨運(yùn)從業(yè)資格證模擬考試試題答案解析
- 2025年萊蕪貨運(yùn)從業(yè)資格證考試內(nèi)容
- 2025年延邊貨運(yùn)從業(yè)資格證模擬考試下載
- 年度產(chǎn)品研發(fā)進(jìn)展報告表
- 五年級六一發(fā)言稿
- 本季度營銷活動詳細(xì)規(guī)劃
- 2024年環(huán)北部灣廣西水資源配置有限公司招聘考試真題
- 2023-2024年演出經(jīng)紀(jì)人之演出經(jīng)紀(jì)實務(wù)考前沖刺模擬試卷附答案(研優(yōu)卷)
- 2025年黑龍江建筑職業(yè)技術(shù)學(xué)院單招職業(yè)技能測試題庫完美版
- 第16課《有為有不為 》課件-2024-2025學(xué)年統(tǒng)編版語文七年級下冊
- 第一單元時、分、秒(說課稿)-2024-2025學(xué)年三年級上冊數(shù)學(xué)人教版
- 地理-浙江省杭州八縣市2024學(xué)年高二第一學(xué)期期末學(xué)業(yè)水平測試試題和答案
- 《康復(fù)工程學(xué)》課件-第一講 康復(fù)工程概論
- DeepSeek:從入門到精通
- 天津2025年天津中德應(yīng)用技術(shù)大學(xué)輔導(dǎo)員崗位招聘7人筆試歷年參考題庫附帶答案詳解
- 2025年無錫職業(yè)技術(shù)學(xué)院高職單招職業(yè)適應(yīng)性測試近5年??及鎱⒖碱}庫含答案解析
- 2025年銅材拉絲項目可行性研究報告
評論
0/150
提交評論