回 帖 发 新 帖 刷新版面

主题:这种图阵请你做,<- - -加分了

编程输出数字矩形.由键盘输入矩形的长A(每行摆放A个数)和宽(摆放B行),摆放规则如下所示,
如:输入:A=3,B=5
输入:    1   2   6
         3   5   7
         4   8   12
         9   11  13
         10  14  15
注A<20,B<20
输出格式要求每个数字占4个字符位置,不足4位时前面以空格补足.

回复列表 (共3个回复)

沙发

以前做过N*N的方阵。当时我是不开数组。现在换成A*B,如果不开数组确实难度很大。但是经过仔细地计算,终于也试成功了。呵呵~~~下面程序纯属吓唬人的,计算了一大堆在计算什么啊?

其实在计算一个小学生都知道的公式:
梯形的面积= (上底+下底)*高/2
[em1][em1][em1]



var
   x,y,A,B:longint;

function f(x,y:longint):longint;
var
   s,d1,d2,h,k:longint;

begin
   s:=0;  k:=x+y-1;
   if k-1 > A then
      begin
          h:= A;  d1:=k-A;
      end
   else
      begin
          h:=k-1;  d1:=1;
      end;
   d2:=k-1;
   if d2>B then
      begin
          s:=(d2-B)*B; h:=h-d2+B;  d2:=B;
       end;
   s:=s+(d1+d2)*h div 2;
   if k mod 2 =0 then
      begin
         if k>A then  s:=s-d1;
         f:= s+y
      end
      else
        begin
             k:=x+y;
             if k-1>A then  h:=A+1 else h:=k-1;
             if k-1>B then h:=h-(k-1-B);
             y:=y-d1;  s:=s+h-y;   f:=s;
        end;
end;

begin
    readln(A,B);
    for y:=1 to B do
      begin
         for x:=1 to A do  write(f(x,y):4);
         writeln;
      end;
end.

板凳

var
   x,y,A,B:longint;

function f(x,y:longint):longint;
var
   s,d1,d2,h,k:longint;

begin
   s:=0;  k:=x+y-1;
   if k-1 > A then
      begin
          h:= A;  d1:=k-A;
      end
   else
      begin
          h:=k-1;  d1:=1;
      end;
   d2:=k-1;
   if d2>B then
      begin
          s:=(d2-B)*B; h:=h-d2+B;  d2:=B;
       end;
   s:=s+(d1+d2)*h div 2;
   if k mod 2 =0 then
      begin
         if k>A then  s:=s-d1;
         f:= s+y
      end
      else
        begin
             k:=x+y;
             if k-1>A then  h:=A+1 else h:=k-1;
             if k-1>B then h:=h-(k-1-B);
             y:=y-d1;  s:=s+h-y;   f:=s;
        end;
end;

begin
    readln(A,B);
    for y:=1 to B do
      begin
         for x:=1 to A do  write(f(x,y):4);
         writeln;
      end;
end.
[em70][em70][em70][em70][em70][em70][em70][em70][em70][em70]

3 楼

......难以置信!

我来回复

您尚未登录,请登录后再回复。点此登录或注册