请帮我打印这个模式,并解释它背后的逻辑。提前感谢你。
0
111
22222
3333333
我的Pascal代码是:
program patnum;
uses crt;
var
row, col1, col2, totalRows, totalCol: integer;
Begin
clrscr;
totalRows := 3;
totalCol := totalRows-2;
for row := 0 to totalRows do
begin
for col1 := 0 to totalCol do
begin
write(' ');
end;
totalCol := totalCol - 1;
for col2 := 0 to row do
begin
write(row);
end;
writeln('');
end;
readln;
End.
*请帮助*
答案 0 :(得分:0)
我确定有一种更简单的方法可以做到这一点,但如果你只是想通过努力完成它,这应该会有所帮助。
var
t : integer
begin
t := 6
for x := 0 to t do
begin
write(" ");
end;
write("0");
writeln;
t := 4;
for x := 0 to t do
begin
write(" ");
end;
write("111");
writeln;
t := 2;
for x := 0 to t do
begin
write(" ");
end;
write("22222");
writeln;
t := 0;
for x := 0 to t do
begin
write(" ");
end;
write("3333333");
end.
答案 1 :(得分:-1)
这似乎是一项编程练习。但是,这是:
Program patnum(output);
function digitNumber(n:integer) : integer;
var counter : Integer;
begin
counter := 0;
while n >= 1 do
begin
counter := counter + 1;
n := n div 10;
end;
digitNumber := counter;
end;
procedure prettyPrint(x1 : integer; x2 : integer; x3 : integer; x4 : integer);
var Counter, max, tmp : Integer;
Begin
max := digitNumber(x1);
tmp := digitNumber(x2);
if(tmp > max) then
begin
max := tmp;
end;
tmp := digitNumber(x3);
if(tmp > max) then
begin
max := tmp;
end;
tmp := digitNumber(x4);
if(tmp > max) then
begin
max := tmp;
end;
tmp := max - digitNumber(x1);
For Counter := 1 to tmp do
Begin
write(' ');
End;
writeln(x1);
tmp := max - digitNumber(x2);
For Counter := 1 to tmp do
Begin
write(' ');
End;
writeln(x2);
tmp := max - digitNumber(x3);
For Counter := 1 to tmp do
Begin
write(' ');
End;
writeln(x3);
tmp := max - digitNumber(x4);
For Counter := 1 to tmp do
Begin
write(' ');
End;
writeln(x4);
End;
begin
prettyPrint(22,3333,1,534);
end.
我建议使用数组来保存要打印的值,但我把它留给你......
此外,您的代码似乎几乎没用。
* edit:注意该函数在整数上失败< 1。