第1题
[问题描述]
求出所有满足列条件的二位数:将此二位数的个位数字与十位数字进行交换,可得到一个新的数,要求新数与原数之和小于100。
程序要求:每行输出6个满足要求的数。
[算法说明]
分解每一个二位数,然后重新组成一个新数,当满足条件时,用计数器来统计个数。
[程序清单]
begin
k:=0;
for i:=_____①____ to 99 do
begin
x:=____②____; y:=____③____;
j:=x*10+y;
if ____④____ then
begin
k:=k+1;
write(1:4);
____⑤____ then writeln;
end
end
end.
第2题
[问题描述]
找出小于33的6个正整数,用这些整数进行加法运算,使得包括原来的整数在内能组成尽可能多的不同整数。
例如:用2,3,5这3个数能组成下面的数
2,3,5
2+3=5, 5已经存在
2+5=7, 3+5=8, 2+3+5=10
所以用2,3,5能组成6个不同的数。
程序要求:输出所选的这6个数,以及能组成不同整数的个数。
[算法说明]
选择的这6个数,用来组成数时应该尽可能不重复,引入数组a保存找出的这6个数。
[程序清单]
begin
a[1]:=1;t:=0;
for i:=2 to 6 do
begin
____①____;
for j:=1 to i-1 do
s:=____②____;
a[i]:=____③____;
end;
for i:=1 tO 6 dO
begin
t:=____④____;
write(a[i],'')
end;
writeln('能组成不同整数的个数:',t)
end.
第3题
[问题描述]
求出2—1000之间长度最长的、成等差数列的素数(质数)。
例如:在2-50之间的全部素数有
2,3,5,7,11,13,17,19,23,29,3l,37,4l,43,47
其中公差为1的素数数列为2,3,其长度为2
公差为2的素数数列为3,5,7,其长度为3
程序要求:输出满足条件的素数数列。
[算法说明]
首先用筛选法求出此范围内的全部素数,存放在数组b中,然后用2个变量i,j,
逐步求出满足条件的素数数列。
[程序清单]
begin
max:=0;num:=1000;
for i:=2 to num do b[i]:=i;
for i:=2 to ____①____ do
if ____②____ then
begin
k:=i+i;
while k <= num do
begin
b[k]:=0;
k:=k+i
end
end;
for i:=2 to num-1 do
if ____③____ then
begin
j:=1;
d[j]:=b[i];
for i1:=____④____ do
if b[i1]<>0 then
begin
delta:=____⑤____;
k:=delta;
while (i+k <= num) and ____⑥____ do
begin
j:=j+1;
d[j]:=i+k;
k:=k+delta
end;
if j > max then begin
max:=j;
c:=d {数组d的值赋给数组c}
end;
j:=1
end
end;
writeln('The max length is:',max);
write('The string is:');
for i:=1 to max do write(c[i],'');
writeln
end.
第4题
[问题描述]
求出二个整形数组错位相加的最大面积
1.数组面积的定义:(限定数组头尾不为0)
设有一个数组C二(4,8,12,0,6)
则C的面积为Sc=(4+8)、2+(8+12)/2+12/2+6/2
也就是说,Sc=各梯形面积之和(其中梯形的高约定为1,三角形作为梯形的特殊情况处理)。
又如D=(12,24,6)是,其面积的定义为
Sd=(12+24)/2+(24+6)/2
2.数组错位相加的定义
设有2个正整数的数组a,b,长度为n,当n=5时:
a=(34,26, 15,44, 12) b=(23,46,4,0, 18)
对a、b进行错位相加,可能有下列情况
34 26 15 44 12
+) 23 46 4 0 18
34 26 15 44 12 23 46 4 0 18
或:
34 26 15 44 12
+) 23 46 4 0 18
34 26 15 44 35 46 4 0 18
或:
34 26 15 44 12
+) 23 46 4 0 18
34 26 15 67 58 4 0 18
或 ……最后有:
34 26 15 44 12
+) 23 46 4 0 18
23 46 4 0 18 34 26 15 44 12
可以看到:由于错位不同,相加的结果也不同。
程序要求:找出一个错位相加的方案,使得输出的数组面积为最大。
[算法说明]
设a,b的长度为10,用a,b:array[1..10] of integer表示,其结果用数组c,d:array[1.. 30] of integer表示。
错位相加的过程可以从开始不重叠,然后逐步重叠,再到最后的不重叠。
梯形面积的计算公式为:(上底+下底)*高/2,其中约定高为1,故可写为(上底十下底)/2。
[程序清单]
const n=10;
function sea:=real;{计算数组C面积}
begin
j1:=1;
while ____①____ do j1: = j1 + 1;
if j1 =3*n then sea: =0
else begin
j2:=3 *n;
while ___②___ do j2: = j2 - 1;
if j1 =j2 then sea: =0
else begin
j3: =c[jl] +c[j2];
for j4:=jl+l to j2-1 do
j3:=j3 +c[j4] *2;
sea: = j3/2
end
end
end;
begin {主程序}
for i: =1 to n do read(a[i]);
for j: =1 to n do read(b[j]);
___③___ ;
for i: =1 to 2 * n+1 do
begin
for j:=1 to 3*n do ___④___ ;
for j:=1 to n do c[j+n]:=a[j];
for j::1 to n do ___⑤___ ;
p:=sea;
if p > s then begin
d: =c;
s: =p
end;
end;
for i: = 1 to 3 * n do write(d[i],' ');
writeln;
writeln ( 's=', s)
end.
第5题
[问题描述]
表的操作:设有一个表,记为L=(a1,a2,…,an),其中:
L:表名。 a1,a2,…,an为表中的元素。
当ai为0-9数字时,表示元素,为大写字母时表示是另一个表,但不能循环定义。
例如下列表的定义是合法的(约定L是第一个表的表名)。
L=(1,3,K,8,0,4)
K=(3,P,4,H,7)
P=(2,3)
H=(4,0,5,3)
程序要求:当全部表给出之后,求出表中所有元素的最大元素,以及表中所有元素之和。
【算法说明】
表用记录类型定义:设lmax为表中元素最大个数
tabtype:record
length:0..lmax; {长度}
element:array[1..lmax] Of char; {表体}
end;
再定义队列: qtype=record
base:array[0..lmax] Of char;
front,rear:0..lmax;
end;
为此,设计一个字符入队的过程inqueue,出队函数outqueue,表中最大元素及元素求和均采用递归计算。
[程序清单]
const lmax = 38;
var t: array[ 'A'.. 'Z' ] of tabtype;
s: string[lmax];
procedure inqueue(var q:qtype; c : char);
begin
q. rear:= ___(1)___ ;
q. base[q.rear ]:= c
end;
function outqueue(var q:qtype):char;
begin
q. front:= ___(2)___ ;
outqueue:= q.base[q.front]
end.
function maxnumber(c:char):char;
var max: char;
begin
max:= chr(0);
for i:= 1 to t[c].length do
begin
ch:= t[c].element[i];
if ___(3)___ then m:= maxnumber(ch)
else m:= ch
if max < m then max:=m
end;
___(4)___
end;
function total(c:char):integer
var k, i: integer;
begin
k:=0;
for i:= 1 to t[c].length do
begin
ch:= t[e].lelment[i];
if ___(5)___ then m:= total(ch);
else m:=ord(ch)-ord('0');
k:=k+m
end;
total:= k
end;
begin
max:=36;
for tabno:= 'A' to 'Z' do t[tabno].length:=0;
q.front:=O;q.rear:=0;
inqueue(q,'L');
while q.front <>.rear do
begin
tabno:= outqueue(q);
write(tabno,'=');
readln(s);
i:=1;
while s[i] <>'(' do i:=i+1;
while s[i] <>')' do
begin
if(s[i] >='a')and(s[i] <='z')
then s[i]:= chr(ord(s[i])+ord('A')-ord('a'));
if(s[i] >='A')and(s[i]<='Z')
then begin
inc(t[tabno].length);
t[tabno].element[t[tabno].length] := s[i];
inqueue(q,s[i])
end
else if(s[i] >='0')and(s[i] <= '9')
then begin
inc(t[tabno].length);
t[tabno].element[t[tabno].length ] := s[i]
end;
inc(i)
end;
end;
writeln('the max number in table L is:', maxnumber('L'));
writeln('Total is:', total('L'))
end.
第6题
[问题描述]
设有一个实数,以字符串形式存放于数组x中,用x:array[1..n] of char表示。其中x[1]若为'-',表示负数;若为'+','.',' ',则表示正数。若为数字,也认为是正数。
例如 x=(' ','2',,'0',' ','3','.','5','%') 则表示203.5
x=('-','1',,'.',' ','2','0','%') 则表示-1.2
约定:在字符串x中,除x[1]外,其后可以包含有若干个'.'与' ',但仅以第一次出现的为准,空格不起任何作用,并以字符'%',作为结束标志。
程序要求:将输入的字符串还原成实数(小数点后无用的0应除去),还原的结果以下列形式存放(不需要输出)
f:数符。正数放0,负数放1。
a:array[1..n] of integer;存放数字,不放小数点。
k:表示A中有效数字的个数。
j:表示小数点后的位数。
例如:数203.24,还原后结果的存放是:
f=0
a=(2,0,3,2,4)
k=5
j=2
又如:数-33.0740,还原后结果的存放是:
f=1
a=(3,3,0,7,4)
k=5
j=3
[算法说明]
x:array[1..10] of char;可放长度定为10;首先读入字符串,然后处理数的符号,在还原的过程中,需要判定正数部分与小数部分,同时去除多余的空格和小数点,并约定输入是正确的,
不用作出错检查。
[程序清单]
begin ’
for i:=1 to 10 do a[i]:=0;
for i:=1 to 10 do read(x[i]);
j:=0;f:=0;k:=0;b:=0;
if x[1]='-' then begin
___(1)___
___(2)___
end
else if x[1]:=' ' then I:=2
else I: = 1;
while ___(3)___ do i:= i + 1;
while ___(4)___ do
begin
if (x[i] >='0')and(x[i] <='9')
then begin
k: =k+l;
___(5)___ ;
if b=1 then ___(6)___
end
else if(x[I]='.')and(b=0) then b:= 1;
i: =i+l
end;
if j >0 then while a[k] =0 do begin
___(7)___
___(8)___
end
end.
|