1. (求字符的逆序)下面的程序的功能是输入若干行字符串,每输入一行,就按逆序输出该行,最后键入-1终止程序。
请将程序补充完整。
Program j401;
type str1=string[100];
var line:str1; kz:integer;
procedure reverse(var s:str1);
var I,j:integer; t:char;
begin
i:=1; j:=length(s);
while (i<j) do begin
t:=s[i]; s[i]:=s[j]; s[j]:=t;
____inc(i)____;____dec(j)_____ ;
end;
end;
begin
writeln(‘continue? -1 for end.’);
readln(kz);
while ( ___kz<>-1_____)do
begin
readln(line);
___reverse(line)_____;
writeln(line);
writeln(‘continue? -1 for end.’);
readln(kz);
end;
end.
2 2 3 3
2 -1 1 3
4 1 1 5
4 4 5 5
2. (棋盘覆盖问题)在一个2k×2 k个方格组成的棋盘中恰有一个方格与其它方格不同(图中标记为-1的方格),称之为特殊方格。现用L型(占3个小方格)纸片覆盖棋盘上除特殊方格的所有部分,各纸片不得重叠,于是,用到的纸片数恰好是(4 k-1)/3。在下表给出的一个覆盖方案中,k=2,相同的3各数字构成一个纸片。
下面给出的程序使用分治法设计的,将棋盘一分为四,依次处理左上角、右上角、左下角、右下角,递归进行。请将程序补充完整。
Program j402;
type arr1=array[1..65] of integer;
arr2=array[1..65] of arr1;
var board:arr2; tile:integer; size,dr,dc:integer;
procedure chessboard(tr,tc:integer; dr,dc:integer; var size:integer);
var t,s:integer;
begin
if (size=1) then ___exit______;
t:=tile; inc(tile);
s:=size div 2;
if then chessboard(tr,tc,dr,dc,s) else begin
board[tr+s-1]:=t;
_____(dr<tr+s)and(dc<tc+s)______;
end;
if (dr<tr+s) and (dc>=tc+s) then chessboard(tr,tc+s,dr,dc,s)
else begin board[tr+s-1][tc+s]:=t;
______chessboard(tr,tc,tr+s-1,tc+s-1,s)______; end;
if (dr>=tr+s) and (dc<tc+s) then chessboard(tr+s,tc+s,dr,dc,s) else begin
board[tr+s][tc+s]:=t;
______chessboard(tr,tc+s,tr+s-1,tc+s,s)______; end;
if (dr>=tr+s) and (dc>=tc+s) then chessboard(tr+s,tc+s,dr,dc,s)
else begin board[tr+s][tc+s]:=t;
_____chessboard(tr+s,tc,tr+s,tc+s-1,s)______; end;
end;
procedure prt1(n:integer);
var I,j:integer;
begin
for I:=1 to n do begin
for j:=1 to n do write(board[i][j]:3);
writeln;
end;
end;
begin
writeln(‘input size(4/8/16/64):’);
readln(size); writeln(‘input the position of special block(x,y):’);
readln(dr,dc); board[dr][dc]:=-1;
tile:=1; chessboard(1,1,dr,dc,size); prt1(size);
end.