pascal语言高手进来!!!
小学题目:螺旋方阵求两条对角线上的数字和的乘机。如果n=5那么如图12345161718196152425207142322218131211109不要光给我答案,我要解...
小学题目:螺旋方阵
求 两条对角线上的数字和的乘机。
如果n=5 那么如图
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
不要光给我答案,我要解释,这一步干什么,每一个定义的变量都要告诉我用来干什么的,如果有规律,或有简便方法就告诉我下,或者是什么算法,教我下。咱不缺悬赏分!!!另注:不要忽悠我,本人问题很多,问题面很广,忽悠的人回答我的问题别想拿分!!!!答案要试过哦,正确的再给我!!!好的加很多分,最高300分。
另外帮我看一下,我的程序哪里错了?谢谢了!!!!解答的,我再开一个新问题给你300分
var
a1,a2,a3,a4,b1,b2,b3,b4,i,j,k,s,n,c,d:longint;
a:array[1..20,1..20]of longint;
q,w,e,r:integer;
h1,h2:integer;
begin
read(n);
a1:=1;a2:=2;a3:=1;a4:=2;
b1:=n;b2:=n;b3:=n-1;b4:=n-1;
q:=1;w:=n;e:=n;r:=1;
s:=1; j:=1; c:=n mod 2;d:=n div 2;
if c=0 then d:=d-1;
while j<=d do
begin
for i:=a1 to b1 do
begin
k:=k+1;
a[q,i]:=k;
end;
for i:=a2 to b2 do
begin
k:=k+1;
a[i,w]:=k;
end;
for i:=b3 downto a3 do
begin
k:=k+1;
a[e,i]:=k;
end;
for i:=b4 downto b4 do
begin
k:=k+1;
a[r,i]:=k;
end;
j:=J+1;
a1:=a1+1;b1:=b1-1;
a2:=a2+1;b2:=b2-1;
a3:=a3+1;b3:=b3-1;
a4:=a4+1;b4:=b4-1;
q:=q+1;
w:=w-1;
e:=e-1;
r:=r+1;
end;
if odd(n)and(n>2) then a[n div 2+1,n div 2+1]:=n*n;
if n<3 then if 3-n=2 then s:=1
else s:=24;
if not(odd(n))and(n>2) then begin
a[n div 2,n div 2]:=n*n-3;
a[n div 2,n div 2+1]:=n*n-2;
a[n div 2+1,n div 2]:=n*n;
a[n div 2+1,n div 2+1]:=n*n-1;
end;
if n>2 then begin
for i:=1 to n do
for j:=1 to n do
begin
if i=j then h1:=a[i,j]+h1;
if i+j-1=n then h2:=a[i,j]+h2;
end;
s:=h2*h1;
end;
write(s);
end.
n最低是1,最高是20,题目是求边长为n个数的方阵(排放顺序如图),两条对角边的和的乘积,即(1+17+25+21+9)*(5+19+25+23+13) 展开
求 两条对角线上的数字和的乘机。
如果n=5 那么如图
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
不要光给我答案,我要解释,这一步干什么,每一个定义的变量都要告诉我用来干什么的,如果有规律,或有简便方法就告诉我下,或者是什么算法,教我下。咱不缺悬赏分!!!另注:不要忽悠我,本人问题很多,问题面很广,忽悠的人回答我的问题别想拿分!!!!答案要试过哦,正确的再给我!!!好的加很多分,最高300分。
另外帮我看一下,我的程序哪里错了?谢谢了!!!!解答的,我再开一个新问题给你300分
var
a1,a2,a3,a4,b1,b2,b3,b4,i,j,k,s,n,c,d:longint;
a:array[1..20,1..20]of longint;
q,w,e,r:integer;
h1,h2:integer;
begin
read(n);
a1:=1;a2:=2;a3:=1;a4:=2;
b1:=n;b2:=n;b3:=n-1;b4:=n-1;
q:=1;w:=n;e:=n;r:=1;
s:=1; j:=1; c:=n mod 2;d:=n div 2;
if c=0 then d:=d-1;
while j<=d do
begin
for i:=a1 to b1 do
begin
k:=k+1;
a[q,i]:=k;
end;
for i:=a2 to b2 do
begin
k:=k+1;
a[i,w]:=k;
end;
for i:=b3 downto a3 do
begin
k:=k+1;
a[e,i]:=k;
end;
for i:=b4 downto b4 do
begin
k:=k+1;
a[r,i]:=k;
end;
j:=J+1;
a1:=a1+1;b1:=b1-1;
a2:=a2+1;b2:=b2-1;
a3:=a3+1;b3:=b3-1;
a4:=a4+1;b4:=b4-1;
q:=q+1;
w:=w-1;
e:=e-1;
r:=r+1;
end;
if odd(n)and(n>2) then a[n div 2+1,n div 2+1]:=n*n;
if n<3 then if 3-n=2 then s:=1
else s:=24;
if not(odd(n))and(n>2) then begin
a[n div 2,n div 2]:=n*n-3;
a[n div 2,n div 2+1]:=n*n-2;
a[n div 2+1,n div 2]:=n*n;
a[n div 2+1,n div 2+1]:=n*n-1;
end;
if n>2 then begin
for i:=1 to n do
for j:=1 to n do
begin
if i=j then h1:=a[i,j]+h1;
if i+j-1=n then h2:=a[i,j]+h2;
end;
s:=h2*h1;
end;
write(s);
end.
n最低是1,最高是20,题目是求边长为n个数的方阵(排放顺序如图),两条对角边的和的乘积,即(1+17+25+21+9)*(5+19+25+23+13) 展开
4个回答
展开全部
program test;
var
a1, a2, a3, a4, b1, b2, b3, b4, i, j, k, s, n, c, d: longint;
a: array[1..20, 1..20] of longint;
q, w, e, r: integer;
h1, h2: integer;
begin
Read(n);
a1 := 1;
a2 := 2;
a3 := 1;
a4 := 2;
b1 := n;
b2 := n;
b3 := n - 1;
b4 := n - 1;
q := 1;
w := n;
e := n;
r := 1;
s := 1;
j := 1;
{c := n mod 2;
d := n div 2;
if c = 0 then
d := d - 1; 直接换成(n+1) div 2}
d:=(n+1) div 2;
k := 0; // 记得初始化
while j <= d do
begin
for i := a1 to b1 do // 上方
begin
k := k + 1;
a[q, i] := k;
end;
for i := a2 to b2 do // 右方
begin
k := k + 1;
a[i, w] := k;
end;
for i := b3 downto a3 do //下方
begin
k := k + 1;
a[e, i] := k;
end;
for i := b4 downto b4 do //左边
begin
k := k + 1;
a[r, i] := k;
end;
j := J + 1; // 中间的下一个小正方形
a1 := a1 + 1;
b1 := b1 - 1;
a2 := a2 + 1;
b2 := b2 - 1;
a3 := a3 + 1;
b3 := b3 - 1;
a4 := a4 + 1;
b4 := b4 - 1;
q := q + 1;
w := w - 1;
e := e - 1;
r := r + 1;
end;
//if odd(n) and (n > 2) then
// a[n div 2 + 1, n div 2 + 1] := n * n; 省掉
if n < 3 then
if 3 - n = 2 then
s := 1
else
s := 24;
{if not (odd(n)) and (n > 2) then
begin
a[n div 2, n div 2] := n * n - 3;
a[n div 2, n div 2 + 1] := n * n - 2;
a[n div 2 + 1, n div 2] := n * n;
a[n div 2 + 1, n div 2 + 1] := n * n - 1;
end; 省掉}
if n > 2 then
begin
for i := 1 to n do
{for j := 1 to n do
begin
if i = j then
h1 := a[i, j] + h1;
if i + j - 1 = n then
h2 := a[i, j] + h2;
end;直接写成}
begin
h1:=h1+a[i,i];
h2:=h2+a[i,n+1-i];
end;
s := h2 * h1;
end;
Write(s);
end.
觉得你的思路是对的,就是代码有些乱呵呵
11111
12221
12321
12221
11111
思路就是先填1,再把矩形缩小,填2...
其实此题还可以找两条对角线和的规律,
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
1+8=9,9+8=17,17+4=21,21+4=25
5+8=13,13+6=19,19+4=23,23+2=25
然后就变成了找
8,8,4,4
8,6,4,2的规律了~
var
a1, a2, a3, a4, b1, b2, b3, b4, i, j, k, s, n, c, d: longint;
a: array[1..20, 1..20] of longint;
q, w, e, r: integer;
h1, h2: integer;
begin
Read(n);
a1 := 1;
a2 := 2;
a3 := 1;
a4 := 2;
b1 := n;
b2 := n;
b3 := n - 1;
b4 := n - 1;
q := 1;
w := n;
e := n;
r := 1;
s := 1;
j := 1;
{c := n mod 2;
d := n div 2;
if c = 0 then
d := d - 1; 直接换成(n+1) div 2}
d:=(n+1) div 2;
k := 0; // 记得初始化
while j <= d do
begin
for i := a1 to b1 do // 上方
begin
k := k + 1;
a[q, i] := k;
end;
for i := a2 to b2 do // 右方
begin
k := k + 1;
a[i, w] := k;
end;
for i := b3 downto a3 do //下方
begin
k := k + 1;
a[e, i] := k;
end;
for i := b4 downto b4 do //左边
begin
k := k + 1;
a[r, i] := k;
end;
j := J + 1; // 中间的下一个小正方形
a1 := a1 + 1;
b1 := b1 - 1;
a2 := a2 + 1;
b2 := b2 - 1;
a3 := a3 + 1;
b3 := b3 - 1;
a4 := a4 + 1;
b4 := b4 - 1;
q := q + 1;
w := w - 1;
e := e - 1;
r := r + 1;
end;
//if odd(n) and (n > 2) then
// a[n div 2 + 1, n div 2 + 1] := n * n; 省掉
if n < 3 then
if 3 - n = 2 then
s := 1
else
s := 24;
{if not (odd(n)) and (n > 2) then
begin
a[n div 2, n div 2] := n * n - 3;
a[n div 2, n div 2 + 1] := n * n - 2;
a[n div 2 + 1, n div 2] := n * n;
a[n div 2 + 1, n div 2 + 1] := n * n - 1;
end; 省掉}
if n > 2 then
begin
for i := 1 to n do
{for j := 1 to n do
begin
if i = j then
h1 := a[i, j] + h1;
if i + j - 1 = n then
h2 := a[i, j] + h2;
end;直接写成}
begin
h1:=h1+a[i,i];
h2:=h2+a[i,n+1-i];
end;
s := h2 * h1;
end;
Write(s);
end.
觉得你的思路是对的,就是代码有些乱呵呵
11111
12221
12321
12221
11111
思路就是先填1,再把矩形缩小,填2...
其实此题还可以找两条对角线和的规律,
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
1+8=9,9+8=17,17+4=21,21+4=25
5+8=13,13+6=19,19+4=23,23+2=25
然后就变成了找
8,8,4,4
8,6,4,2的规律了~
追问
我按照你的改了,也理解了,可是输出来的不一样啊!n=7,输出的是负数,n=5,输出来的是5293,应该是5205啊。另外我想问下,这个8,8,4,4,8,6,4,2这个规律,要是n=别的,还是一样不该吗?还是要按照什么顺序改
追答
噢噢噢,在矩形左边界的循环处写错了一点。。。修改后:
program test;
var
a1, a2, a3, a4, b1, b2, b3, b4, i, j, k, s, n, c, d: longint;
a: array[1..20, 1..20] of longint;
q, w, e, r: integer;
h1, h2: integer;
begin
Read(n);
a1 := 1;
a2 := 2;
a3 := 1;
a4 := 2;
b1 := n;
b2 := n;
b3 := n - 1;
b4 := n - 1;
q := 1;
w := n;
e := n;
r := 1;
s := 1;
j := 1;
{c := n mod 2;
d := n div 2;
if c = 0 then
d := d - 1; 直接换成(n+1) div 2}
d := (n + 1) div 2;
k := 0; // 记得初始??
while j 2) then
// a[n div 2 + 1, n div 2 + 1] := n * n; 省掉
if n 2) then
begin
a[n div 2, n div 2] := n * n - 3;
a[n div 2, n div 2 + 1] := n * n - 2;
a[n div 2 + 1, n div 2] := n * n;
a[n div 2 + 1, n div 2 + 1] := n * n - 1;
end; 省掉}
if n > 2 then
begin
for i := 1 to n do
{for j := 1 to n do
begin
if i = j then
h1 := a[i, j] + h1;
if i + j - 1 = n then
h2 := a[i, j] + h2;
end;直接写成}
begin
h1 := h1 + a[i, i];
h2 := h2 + a[i, n + 1 - i];
end;
s := h2 * h1;
end;
Write(s);
end.
展开全部
纯模拟,你人是怎么填的,就让程序怎么做
program ysh(input,output);
var
s,p,i,x,y,j,n:longint;
a:array[0..200,0..200]of longint;
begin
readln(n);
n:=2*n;
s:=0;
a[1,1]:=1;
x:=1;y:=0;
p:=n;j:=1;
while s<n*n do
begin
for i:=s+1 to s+p do
begin
y:=y+j;
a[x,y]:=i;
end;
s:=s+p;
p:=p-1;
for i:=s+1 to s+p do
begin
x:=x+j;
a[x,y]:=i;
end;
s:=s+p;
j:=-j;
end;
for i:=1 to n do
begin
for j:=1 to n do
write(a[i,j]:4);
writeln;
end;
end.
program ysh(input,output);
var
n,i,j,s:longint;
a:array[0..100,0..100] of longint;
begin
readln(n);
for i:=0 to n*2+1 do a[0,i]:=-1;
for i:=0 to n*2+1 do a[i,0]:=-1;
for i:=0 to n*2+1 do a[i,n*2+1]:=-1;
for i:=0 to n*2+1 do a[n*2+1,i]:=-1;
i:=1; j:=1; s:=0;
repeat
while (a[i,j]=0) do
begin
inc(s);
a[i,j]:=s;
inc(j);
end;
dec(j);
inc(i);
while (a[i,j]=0) do
begin
inc(s);
a[i,j]:=s;
inc(i);
end;
dec(i);
dec(j);
while (a[i,j]=0) do
begin
inc(s);
a[i,j]:=s;
dec(j);
end;
inc(j);
dec(i);
while (a[i,j]=0) do
begin
inc(s);
a[i,j]:=s;
dec(i);
end;
inc(i);
inc(j);
until (i=n+1)and(j-1=n);
for i:=1 to n*2 do
begin
for j:=1 to n*2 do write(a[i,j]:4);
writeln;
end;
end.
两个都是标程
program ysh(input,output);
var
s,p,i,x,y,j,n:longint;
a:array[0..200,0..200]of longint;
begin
readln(n);
n:=2*n;
s:=0;
a[1,1]:=1;
x:=1;y:=0;
p:=n;j:=1;
while s<n*n do
begin
for i:=s+1 to s+p do
begin
y:=y+j;
a[x,y]:=i;
end;
s:=s+p;
p:=p-1;
for i:=s+1 to s+p do
begin
x:=x+j;
a[x,y]:=i;
end;
s:=s+p;
j:=-j;
end;
for i:=1 to n do
begin
for j:=1 to n do
write(a[i,j]:4);
writeln;
end;
end.
program ysh(input,output);
var
n,i,j,s:longint;
a:array[0..100,0..100] of longint;
begin
readln(n);
for i:=0 to n*2+1 do a[0,i]:=-1;
for i:=0 to n*2+1 do a[i,0]:=-1;
for i:=0 to n*2+1 do a[i,n*2+1]:=-1;
for i:=0 to n*2+1 do a[n*2+1,i]:=-1;
i:=1; j:=1; s:=0;
repeat
while (a[i,j]=0) do
begin
inc(s);
a[i,j]:=s;
inc(j);
end;
dec(j);
inc(i);
while (a[i,j]=0) do
begin
inc(s);
a[i,j]:=s;
inc(i);
end;
dec(i);
dec(j);
while (a[i,j]=0) do
begin
inc(s);
a[i,j]:=s;
dec(j);
end;
inc(j);
dec(i);
while (a[i,j]=0) do
begin
inc(s);
a[i,j]:=s;
dec(i);
end;
inc(i);
inc(j);
until (i=n+1)and(j-1=n);
for i:=1 to n*2 do
begin
for j:=1 to n*2 do write(a[i,j]:4);
writeln;
end;
end.
两个都是标程
追问
看不懂,能加些解释吗
追答
这个比较好懂,而且很短,好看
program ysh(input,output);
var
n,i,j,s:longint;
a:array[0..100,0..100] of longint;
begin
readln(n);
for i:=0 to n*2+1 do a[0,i]:=-1; //设置边界,不让数字填出去
for i:=0 to n*2+1 do a[i,0]:=-1; //设置边界,不让数字填出去
for i:=0 to n*2+1 do a[i,n*2+1]:=-1; //设置边界,不让数字填出去
for i:=0 to n*2+1 do a[n*2+1,i]:=-1; //设置边界,不让数字填出去
i:=1; j:=1; s:=0; //i,j表示当前填的是a[i,j],s表示当前填到几了
repeat
while (a[i,j]=0) do //往右填,当这个格子没被填过且没越界时
begin
inc(s); //要填的数字+1
a[i,j]:=s; //填数字
inc(j); //纵坐标+1,比如现在填到a[1,1],下个就填a[1,2]
end;
dec(j); //因为跳出循环说明已经填过头了,所以要退回来 以下雷同
inc(i);
while (a[i,j]=0) do //往下填
begin
inc(s);
a[i,j]:=s;
inc(i);
end;
dec(i);
dec(j);
while (a[i,j]=0) do //往左填
begin
inc(s);
a[i,j]:=s;
dec(j);
end;
inc(j);
dec(i);
while (a[i,j]=0) do //往上填
begin
inc(s);
a[i,j]:=s;
dec(i);
end;
inc(i);
inc(j);
until (i=n+1)and(j-1=n);
for i:=1 to n*2 do
begin
for j:=1 to n*2 do write(a[i,j]:4);
writeln;
end;
end.
你不懂的话手动模拟一遍,就很清楚了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
楼主你好,我是来提建议的,你可以把这个问题发到算法吧,那里的大神很多。相信会有人为你解答。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
能把题意和数据范围说得再清楚点吗
追问
行
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询