4完善程序答案_第1页
4完善程序答案_第2页
4完善程序答案_第3页
已阅读5页,还剩34页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、1L数字之和问题描述求岀1000以内的整数,使其数字之和为13,每行输岀8个数。例如:数85,其数字之和为 8+5=13数373,其数字之和为 3+7+3=13变量说明I :百位数字 J:十位数字K:个位数字S:计数器,用于存放符合条件的数字个数,以决定是否换行算法分析本题采用构造法来造岀所有的二位数和三位数,穷举每位数字,判断是否满足条件。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.分数计算问题描述输入两个真分子的分子与分母(分子、分母的合法性不作检查),对这两个分数进行加法计算。若符合条件,则应将计算的结果化为带分数。例如:输入25231+1/15 (带分数的表达形式)38184/8 (不作约分)121 2 1变量说明A、B、C、D :分别代表两个分子和分母 I :循环变量S:两个分子通分后的和算法分析1、求岀分母的最小公倍数,可从两个分母任选一个 整数则是最小公倍数。B开始到B*D之间的数,看能否被整除,若一旦被子2、再对分子进行通分求和,并判断是否大于当前的分母,若比分母大,则将其

3、化为带分数的表达形式,若比分母小,则不作约分,直接输出,若相等,则输出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)以内素数的程序分析:由希腊著名数学家埃拉托色尼提岀的所谓“筛选法”,步骤如下: 设所有待求素数的范围为2 N ; 找岀其中最小数(必为素数),设为K,输岀; 在2至N的所有数中,将K的所有倍数从中去掉; 重复?直到所有的数都被去掉,即找到了所有的素数。编程时,用数组作标记的方法,a(k)=l表示保留某个数,a(k)=O表示去掉某个数。源程序如下: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. 找数问题问题描述读入若干个不相同且不为0的数X,以0作为输入结果标记,不用排序求岀其中第R个大的 数(R从键盘输入),即有R-1个数比它大,其余的数都比它小。例如:输入3, 14, 22, 15, 17, 6,

7、 0, 3,则输岀其中第3个大的数为15算法说明以数组AL. 200记录读入的数,并以 0为结束(0本身不是数列中的数)。然后从第一个数 开始,将它 与其余的数进行比较并记录岀比它大的数的个数(存于变量Y中),若丫 =&1时,得到所求结 果,否则对下一个数进行同样的处理。程序清单 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个数,用折半插入法进行从小到大排序。请完善程序。分析:所谓的折半插入法是在确定插入位置时先将插入数据与已有序数据的中间数据比较,以确定插入位置是在中间位置前还是后,然后再与前或后半部数据的中间数据比较,直到这部分数据只有一个即找到应插入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个整数K, N

12、,将K分成N个全不相同的整数的和,并使此 N个整数的乘积为最大。请完善程序。例如:输入16,3,则输岀4, 5, 7,因为4+5 + 7 = 16,而且4*5*7 = 140乘积最大。注意,当这N个数越平均(即越接近),它们的乘积越大。如:5与7的和为12,乘积为35, 4与8的和也 为12,它们的乘积为32。下面程序中数组 A存入N个数且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、问题描述:给岀一个正整数 N (1WNW100),即可得到一个由 N个正整数组成的从1开

14、始的如下数列:1,2, 3,4, N-2,N-1,N,且可求岀从1开始的这N个数的全部各个数位上的数字之和。例如:当N=12时,这 12 个数是:1,2, 3, 4, 5, 6, 7, 8, 9,10,11,12。则这 12 个数的数字之和为: 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、 下面程序能找岀所有不含数字为0的六位完全平方数,且这个六位数中至少有三个同样的数字。所谓完全平方数,是指它是某个数的平方,如111556是334的平方,其中数字 1岀现了三次,是一个符合条件的数。请完善程序。例如: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、随机数问题本程序对随机产生的100个0到50之间的随机整数用一个数组存放后进行排序,然后再将其中重复岀现的数进行删除,只保留一个

17、,使得剩下的数中任何两个都不相同且连续存储在原数组中。【程序清单】program 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个评委打分,去掉一个最高分和一个最低分,求余下的总分。请将程序补充完善。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、 有一数列,第一、第二两个数分别为1、2,其后每个数都为前两个数之和。下面程序的功能是打印岀数列中所有小于3000的数,请将程序补充完善。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、个数,使用冒泡法将它们从小到大的次序排列后输岀。请将程序完善。 算法分析:(1) 从A (1) -A (9)为止,把相邻的两个数两两进行比较。(2) 在每两个数的比较过程中,若前一个数比后一个数大,则对调这两个数。(3) 比较完一轮后,若曾进行对调操作,则从(1)开始重复,否则排序结束。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、下列程序是将两个已排好次序 (从小到大)的数列A、B合并为一个数列C,使合并后的C也按从 小到大的次序排列。A、 B数列的元素个数由键盘输入。如:A 数列:3, 6, 9,18B 数列:2, 7, 11则 C 数列为:2, 3, 6, 7, 9, 11, 18试将程序完善。算法分析:(1) 若A

23、(I)仁B (I),则将A (I)的数放入C数列中,否则 B (I)的数入C数列中。(2) 重复步骤(1),直到A或B数列中有一列完全传到C列中。(3) 将A或B中余下的元素直接传到 C数列中。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.【问题描述】给岀一个正整数 N (NV32768),要求将其分解成质因子的连乘积。例如:当N=24时,结果为:24=2*2*2*3(A)又如:当N=13时,结果为:13=13(B)输入:N输岀:如(A)或(B)的格式结果。【变量说明】n:表示输入的正整数。k:表示质因子。【算法分析】从最小的质因子数 2开始,判断n能否被2整数,若能,输岀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的不同数字,求岀由这 4个数字组成的所有 4位数。 例如:输入:3 2 16输出:

27、123612631326136216231632213621632316236126132631312631623216326136123621612361326213623163126321程序要求:从键盘输入4个非0的不同数字(输入的正确性不作检查 ),列岀由这 4个数字组成的所 有4位数,每行输出6个。【变量说明】a:数组,存放从键盘读入的 4个数。 s:计算所得的组合总数,以便控制换行。 P:表示构造岀的 4位数。【算法分析】采用穷举法列举所有可能的组合,由于穷举法的效率较低,我们应尽量多的排除循环变量值相同的情况(比如:i=l,j=l,k=l,l=l),逐次找岀所有的组合,然后输岀即

28、可。由于程序中的输入数据是任意排列的,所以增加了将四个数排序的程序段。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,个苹果,现要从中取岀 N个苹果岀来,取果的规则是要么一个也不取,要么取完这一箱苹果。从键盘输入N和K,编程序求岀该取哪几箱苹果?如果无法按要求取岀苹果,则输岀 “NO”注:2't表示KT个2相乘。分析:因为任何一个十进制数都可以转化为二进制数,如:(13) 10

30、= (1101) 2=1*2 3+1*2 2+0*2 1 + 1*2。即要取 13 个苹果可以取1,4, 8 箱的苹果,当 S>l+2+4+8+2时无法按要求取得。因此,问题转化为将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、的查找算法,算法过程是:在一列已按从小到大排好序的数列中(用找值为X的元素,若找到则输岀它的位置(下标),若找不到,则输岀 “NO。分析:首先将要查找的值与中间位置的数据值比较,若相等,则输出结果,不等,若小于中间位置值 左边子表中按同样的方法找,若大于中间位置值则在右边子表找,直到找到满足条件的数据或者确定 这样的数据。程序如下: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、比赛安排设有N (N<=64)个球队进行单循环比赛,计划在 N-1天内完成,每个队每天进行一场比赛。设计一个比赛的安排,使在N-1天内每个队都与不同的对手比赛。例如n=4时的比赛安排:队1 23 4比赛1=23=4一天1=32=4二天1=42=3三天数据输入输出:从键盘输入N的值,将比赛安排结果输出到屏幕数据输入输岀

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为奇数,将1至N*N填入方阵的每个格,使每行每列及两对角线数字的和都相等,这样的数据阵称为奇幻方。下面是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、数字三角形数学老师在黑板中写了一个有趣的数字三角形,如下图。你能编程序由计算机自动生成如下形式的数字三角形吗?请观察下列规律,根据从键盘输入的N值(NV20=,在屏幕上输岀相应的数字三角形。输入样式: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的三种规格,要求每种规格长度至少要截一根,问如何截取才能够使剩余的钢筋最少。输岀:21cm:根数19cm:根数16cm:根数参考程序如下: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. 排序问题排序的方法很多,下面也是一种排序的方法,现在要你输入n个0到100之间的整数,将输入的N个整数由小到大排序输出,每行输出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方阵填数问题在一个NXN的方阵中,填入 1,2.NXN个数,并要求构成如下的格式:如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约瑟夫问题“六一”儿童节到了,小李班上举行了一个围圈圈的游戏活动。活动是这样的,共有N位同学围成一 个圆圈,然后顺时针依次编号为1、2、N,然后从编号为1的人开始报数,每报到M的同学岀列,然后继续从下一位同学开始报数,每报到 M的同学又岀列,如此下去,直到所有的同学岀列为止。现在要你编一个程序,输入N、M的值,依次输岀岀列的同学的编号。程序如下: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、素数乘法算式问题。在下面的

47、乘法算式中,”*”号只能用素数2, 3, 5, 7代替,因此称为素数乘法算式。编程找岀素数乘法算式所有可能的方案,并按算式的格式打印岀来。算式格式如下:程序如下: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、真分数问题。从3, 5, 7, 9, 11, 13, 15这

49、7个数中任取两个数组成真分数,共有多少个?找岀组成的所有真分数程序如下: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题:连续自然数之和对?个给定的自然数M,求岀所有的连续的自然数段,这些连续的自然数段中的全部数之和为M。例子:1 998+ 1 999+ 2000+ 2001 + 2002 = 10000,所以从 1998 至U 2002 的一个自然数段为M= 10000 的一个解。下而的程序是求给定的 M的所有连续自然数段,输岀所有连续数段的起始位置和终止位置(小数在前,大数在后)。算法提示:S表示连续自段之和,L表示当前自然数段左边的数,r表示当前自然数段右边的

51、数,如果当前S小于M,则在把右边的数加入,如果S的值大于M,则把左边的数减去,如果 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怪的题目,随机产生10000个1亿以内的数,统计素数岀现的概率(素数的个数除以10000的 值),小明是一个十分聪明的小孩,他掌握了筛选法、试除法(将该数N用小于等于妨的所有数去试除,若均无法

52、整除,则N为素数。)来判断素数。小明平时训练总是严格要求自己,做题精益求精(这是成为大牛的必备条件)。他发现试除算法是可以改进的:首先用筛选法来建立素数表,求岀2-10000以内的所 有素数。然后再用试除法,即将该数N用小于等于妨的所有素数去试除,若均无法整除,则N为素数。当然,如果随机产生的数小于10000,则可以直接判断。下面是小明写的部份程序,请你完善它。数据结构设计:数组P用来存贮1 -10000以内的素数。数组F用来标记1 -10000以内的素数,如果是素数,则pi=1,否则pi = 0;ans表示产生素数的总数,s表示随机生的数,flag用来标记是否是素数。varan s,s,kJJ: longint;f: array0 . 1 0000 of i

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论