题1. [问题描述] 输入一长度不超过80个字符的字符串(称为源串),该字符串由小写英文字母、空格组成,并以'.'结束。单词是由连续字母组成,两个单词之间至少有一个空格。 本程序的功能为:首先找出字符串中所有单词并保留一个空格作为单词分隔,存入数组ch中。然后用键盘输入一个待查找的单词,以字符'$',结束。采用顺序查找的方法在ch中进行查找,若找到,则输出该单词在ch中出现的序号(若有多个位置出现该单词,则只输出第一个序号位置)。若不存在, 则输出‘NOT FOUND’。 [程序清单] program ex981; var a,b,ch:array[1..80] of char; i,j,k,n,m:integer; begin n:=0; repeat (1) ; read(a[n]); until a[n]='.'; readln; k:=0; for i:=1 to n do if (a[i]>='a') and (a[i]<='z') then begin k:=k+1; (2) ; end else if k<>0 then if ch[k]<>' ' then begin k:=k+1;ch[k]:=' ' end; m:=0; (3) repeat m:=m+1;read(b[m]) until (4) ; i:=1;j:=1;k:=1 ;b[m]:=' '; while (i<=n) and (j<=m) do begin if (5) then begin i:=i+1;j:=j+1 end else begin while ch[i]<>' ' do (6) i:=i+1;j:=1;k:=k+1; end end; if j>m then writeln(k:4) else writeln('NOT FOUND') end.
题2. [问题描述] FBZ串问题。已知一个由0,1字符串组成的长度为2n的字符串。 请按以下规则将已给出的字符串分解为FBZ串: 若其中字符全为‘1’,,则称其为‘B’串; 若其中字符全为‘0’,则称其为‘Z’串; 若不全为‘0’,同时也不全为‘1’,则称‘F’串。 若此串为F串,则应将此串分解为2个长为 2n-1的子串。 对分解后的子串,仍按以上规则继续分解,直到全部为B串或为Z串为止。 例如n=3时,给出01串为:'10111001'最后输出为FFFBZBFFBZFZB
[程序清单] program ex982; const n=8; var i,j,stl1,stl2,st2,s,t:integer; str1:array[1..2*n,1..n] of char; str2:array[1..40] of char; begin for i:=1 to n*2 do for j:=1 to n do str1[i,j]:=' '; stl1:=1;stl2:=1;st2:=0; for i:=1 to n do read(str1[1,i]);readln; while (1) do begin s:=0;t:=0; for i:=1 to n do begin if str1[stl2,i]='1' then s:=s+1; if str1[stl2,i]='0' then t:=t+1; end; if (2) then begin st2:=st2+1;str2[st2]:='B' end else if (3) then begin st2:=st2+1;str2[st2]:='Z'; end else begin st2:=st2+1;str2[st2]:='F';j:=(s+t) div 2; for s:=n*2-2 downto (4) do for t:=1 to n do str1[s+2,t]:=str1[s,t]; stl1:=stl1+2; for i:=1 to j do begin str1[stl2+1,i]:=str1[stl2,i]; str1[stl2+2,i]:= (5) end; for i:= (6) do begin str1[stl2+1,i]:=' ';str1[stl2+2,i]:=' '; end end; stl2:=stl2+1; end; for i:=1 to st2 do write(str2[i]);writeln end.
|