数组问题(好象和数学有关)(100分)

  • 主题发起人 主题发起人 bxdwx21
  • 开始时间 开始时间
B

bxdwx21

Unregistered / Unconfirmed
GUEST, unregistred user!
有N个数,例如N=20
编程实现:
1 2  3  4  5
14 15 16 17 6 
13 20 19 18 7
12 11 10 9  8
把陀螺状的N个数存到2维数组中
 
如果是N是任意数
最后还必须是 矩形 怎么办?
如N=25

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
 
逐角设置红绿灯,已成功了,有图片“金卡方阵”
 
楼主把问题再描述的清楚点吧
 
就是,把任意数N,做成圈形,存到数组里
如N=25
存到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
编程怎么编,谢谢了,我急哦,谢谢,
 
var
Datas:array of array of Integer;
procedure Fill2DArray(N, RowCount, ColCount: Integer);
type
TDir=(dirRight,dirDown,dirLeft,dirUp);
const
Dirs:array[TDir,0..1]of Integer=((0,1),(1,0),(0,-1),(-1,0));
var
i,x,y,x1,y1,c:Integer;
MyDir:TDir;
b:Boolean;
begin
if RowCount or ColCount=0 then
exit;
MyDir:=dirRight;
for y:=0 to Pred(RowCount)do
for x:=0 to Pred(ColCount)do
Datas[y,x]:=0;
x:=0;
y:=0;
i:=1;
while (i<=N) and (Datas[y,x]=0)do
begin
Datas[y,x]:=i;
c:=0;
while c<4do
begin
x1:=x+Dirs[MyDir][1];
y1:=y+Dirs[MyDir][0];
if (x1>=0) and (y1>=0) and (x1<ColCount) and (y1<RowCount) then
begin
if Datas[y1,x1]=0 then
begin
x:=x1;
y:=y1;
break;
end
else
if MyDir=dirUp then
MyDir:=dirRight
else
MyDir:=Succ(MyDir);
end
else
if MyDir=dirUp then
MyDir:=dirRight
else
MyDir:=Succ(MyDir);
Inc(c);
end;
Inc(i);
end;
end;

使用例子:
SetLength(Datas,SpinEdit3.Value,SpinEdit2.Value);
Fill2DArray(SpinEdit1.Value,SpinEdit3.Value,SpinEdit2.Value);
 
是不是规定了就是5行?
 
行和列 是根据N定的,如N=25 则 行=5 列=5
 
别人用c++给我写了一个,他写的对不对哦??
------------------------------
int h,w, rw[2], rh[2], sh, sw , c, ch, cw;
int a[w][h]={0};
c=h*w;
rw[0] = rh[0] = 0;
rw[1] = w-1;
rh[1] = h-1;
cw=ch=sw=-1;sh=1;
for (int k=0;
k<c;
k++) {
if (cw==rw[0]) {sh=-sh;
rc[0]++;
}
if (cw==rw[1]) {sh=-sh;
rc[1]--;
}
if (ch==rh[0]) {sw=-sw;
rw[0]++;
}
if (ch==rh[1]) {sw=-sw;
rw[1]--;
}

cw+=sw;
ch+=sh;
cw=max(cw,rw[0]);
cw=min(cw,rw[1]);
ch=max(ch,rh[0]);
ch=min(ch,rw[1]);
a[ch*w+cw]=k;
}
 
uses
Math;
type
TData = array of array of Integer;
procedure FillData(var a: TData;
aWidth, aDepth: Integer);
var
LeftX, RightX, TopY, BottomY, Value: Integer;
procedure SetBounds(aLeft, aTop, aRight, aBottom: Integer);
begin
LeftX := aLeft;
TopY := aTop;
RightX := aRight;
BottomY := aBottom;
end;
function NextValue: Integer;
begin
Inc(Value);
Result := Value;
end;
var
X, Y, i, MinSide: Integer;
begin
if (aWidth < 0) or (aDepth < 0) then
Exit;
Value := 0;
SetLength(a, aDepth, aWidth);
SetBounds(0, 0, aWidth - 1, aDepth - 1);
MinSide := Min(aWidth, aDepth);
for i := 1 to MinSide div 2do
begin
for X := LeftX to RightXdo
a[TopY, X] := NextValue;
for Y := TopY + 1 to BottomYdo
a[Y, RightX] := NextValue;
for X := RightX - 1do
wnto LeftXdo
a[BottomY, X] := NextValue;
for Y := BottomY - 1do
wnto TopY + 1do
a[Y, LeftX] := NextValue;
SetBounds(LeftX + 1, TopY + 1, RightX - 1, BottomY - 1);
end;
if Odd(MinSide) then
begin
for X := LeftX to RightXdo
a[TopY, X] := NextValue;
for Y := TopY + 1 to BottomYdo
a[Y, RightX] := NextValue;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
const
cWidth: Integer = 5;
cDepth: Integer = 6;
var
B: TData;
i, j: Integer;
TempStr: String;
begin
FillData(B, cWidth, cDepth);
for j := 0 to cDepth - 1do
begin
TempStr := '';
for i := 0 to cWidth - 1do
TempStr := TempStr + Format('%3d',[B[j, i]]);
Memo1.Lines.Add(TempStr);
end;
end;
 
那个高手能说说代码思路,我不太明白,谢谢了哦,
 
大一用vfp编的,转的方向不一样,没时间修改,基本符合题意
可读性谈不上有时间并且很不幸也学了vfp的可以看一下,两个程序:
NO。1
clear
input &quot;请输入一个正整数n:&quot;
to n
dime a(n+2,n+2)
for i=1 to n+2 &&得到一个n+2 阶的矩阵
for j=1 to n+2
if i=1.or.i=n+2.or.j=1.or.j=n+2
a(i,j)=1
else

a(i,j)=0
endif
endfor
endfor
k=1
m=1
i=2
j=2
a(i,j)=m
for i0=1 to n*n-1
if a(i+1,j)=0.and.a(i,j-1)=0
k=0
else

k=1
endif
if k=1.and.a(i+1,j)=0&&1.下
m=m+1
i=i+1
a(i,j)=m
else
if k=1.and.a(i,j+1)=0&&2.右
m=m+1
j=j+1
a(i,j)=m
else

if k=1.and.a(i-1,j)=0&&3.上
m=m+1
i=i-1
a(i,j)=m
else
&&4.左
m=m+1
j=j-1
a(i,j)=m
endif
endif
endif
endfor
for i=2 to n+1
for j=2 to n+1
??str(a(i,j),3)
endfor
?
endfor


NO.2
clear
input &quot;请输入一个正整数n:&quot;
to n
dime a(n,n)
m=1
a=0
for i=1 to round(n/2,0)
for j=i to n-i+1
a(j,i)=m
m=m+1
endfor
j=j-1
for k=i+1 to n-i+1
a(j,k)=m
m=m+1
endfor
k=k-1
for j1=j-1 to i step -1
a(j1,k)=m
m=m+1
endfor
j1=j1+1
for k1=k-1 to i+1 step -1
a(j1,k1)=m
m=m+1
endfor
k1=k1+1
endfor
for j2=1 to n
for k2=1 to n
??str(a(j2,k2),3)
endfor
?
endfor
 
谢谢大家:)
 
我较个真,n要是个质数怎么办?
 
后退
顶部