如何实现一个顺时针的螺旋算法

int i 值可以随意

  
int i=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

int i=6
1 2 3 4 5 6
20 21 22 23 24 7
19 32 33 34 25 8
18 31 36 35 26 9
17 30 29 28 27 10
16 15 14 13 12 11
刘锡涛
刘锡涛
281
编辑于2012-08-13
评论 (0)链接2012-08-09 

给你个php版算法:

  
<?php
function getSpiralArray($n)
{
$pos = 1;
$count = $n;
$value = -$n;
$sum = -1;
do
{
$value = -1 * $value / $n;
for ($i = 0; $i < $count; $i++)
{
$sum += $value;
$result[$sum / $n][$sum % $n] = $pos++;
}
$value *= $n;
$count--;
for ($i = 0; $i < $count; $i++)
{
$sum += $value;
$result[$sum / $n][$sum % $n] = $pos++;
}
} while ($count > 0);
return $result;
}

function PrintArray($array)
{
for ($i = 0; $i < count($array); $i++) {
for ($j = 0; $j < count($array); $j++) {
echo str_pad($array[$i][$j],3,' ');
}
echo "\n";
}
}

$arr5 = getSpiralArray(5);
PrintArray($arr5);
$arr6 = getSpiralArray(6);
PrintArray($arr6);

?>

截图:

请输入图片描述

PanYue
编辑于 2012-08-09
该答案已被锁定,无法对其进行评论,编辑及投票。
()
评论 (0)链接 • 2012-08-09
  
static void function(int param){
int locationX = 0;
int locationY = 0;
int forward = 2; // 1 north, 2 east, 3 south , 4 west
int[][] arrayList = new int[param][param];
for(int i =1; i<= param*param; i++){
arrayList[locationX][locationY] = i;
switch(forward){
case 1:// north
if(locationX>0){
if(arrayList[locationX-1][locationY] == 0){
locationX--;
}else{
forward = 2;
locationY++;
}
} else{
forward = 2;
locationY++;
}
break;
case 2: //EAST
if(locationY<(param-1)){
if(arrayList[locationX][locationY+1] == 0){
locationY++;
} else {
forward = 3;
locationX++;
}
} else {
forward = 3;
locationX++;
}
break;
case 3: //SOUTH
if(locationX<(param-1)){
if(arrayList[locationX+1][locationY] == 0){
locationX++;
} else{
forward = 4;
locationY--;
}
} else {
forward = 4;
locationY--;
}
break;
case 4: // west
if(locationY>0){
if(arrayList[locationX][locationY-1] == 0){
locationY--;
}else{
forward = 1;
locationX--;
}
} else {
forward = 1;
locationX--;
}
break;
default:
break;
}
}
int i =0;
int j =0;
for(;i<param;i++){
for(;j<param;j++){
System.out.print(arrayList[i][j]+" ");
}
j=0;
System.out.println();
}
}
小白
小白
1776
编辑于 2012-08-10
该答案已被锁定,无法对其进行评论,编辑及投票。
()
评论 (0)链接 • 2012-08-09
  
#include <iostream>
#include<iomanip>

using namespace std;

void inputGrid(int n,int *m){ //n表示规模,m为二维数组指针
int row=0,line=0,flag=0;//row,line表示第几次行处理或列处理;
int i=0,j=-1;
for(int num=1;num<=n*n;){
if(!flag){//行处理
int addnum;
if(row%2==0)
addnum=1;//往前走
else
addnum=-1;//往回走
int num_row=n-row;
while(num_row){//将本行剩余空白列全部填满
num_row--;
j+=addnum;
*(m+n*i+j)=num;
num++;
}
row++;
flag=1;
}else{//列处理
int addnum;
if(line%2==0)
addnum=1;//往前走
else
addnum=-1;//往回走
int num_line=n-line-1;
while(num_line){//将本列剩余空白项全部填满
num_line--;
i+=addnum;
*(m+n*i+j)=num;
num++;
}
line++;
flag=0;
}
}
/*for(int i=0;i<n*n;i++){
*(m+i)=i;
}*/
}

int main(){
int n=1;
int *m,*tm;
while(n!=-1){
cout<<"请输入回形数规模:(输入-1退出)";
cin>>n;
if(n==-1)
return 0;
m=(int *)malloc(n*n*sizeof(int));
inputGrid(n,m);
for(int i=0;i<n;i++){
tm=m+n*i;
for(int j=0;j<n;j++){
cout<<setw(3)<<*(tm+j)<<" ";
}
cout<<"\n";
}
}
//return 0;
}
该答案已被锁定,无法对其进行评论,编辑及投票。
()
评论 (0)链接 • 2012-08-09

c++代码

  
void p(int n) {

int step[4] = {1, n+2, -1, -n-2};

int *arr = new int( (n+2) * (n+2) );
memset(arr, 1, sizeof(int) * (n+2) * (n+2));
for (int i = 1; i <= n; ++i) memset(arr + i*(n+2) + 1, 0, sizeof(int) * n);

arr[n+3] = 1;
for (int i = 2, dir = 0, pos = n+3; i <= n*n; ++i) {
if ( (arr[ pos + step[dir] ]) != 0 ) dir = (dir + 1) % 4;

pos += step[dir];
arr[pos] = i;
}

for(int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) printf("%2d ", arr[i*(n+2)+j]);
printf("\n");
}

delete []arr;
}
该答案已被锁定,无法对其进行评论,编辑及投票。
()
评论 (0)链接 • 2012-08-13
  
#include <stdio.h>
#define NUM 5
int Array[NUM][NUM] = {0};
void func(int start, int ceng)
{
int j = 0;
for (j = 0; j < NUM - ceng * 2 + 1; j++)
Array[ceng - 1][ceng - 1 + j] = start++;
for (j = 0; j < NUM - ceng * 2 + 1; j++)
Array[ceng - 1 + j][NUM - ceng] = start++;
for (j = 0; j < NUM - ceng * 2 + 1; j++)
Array[NUM - ceng][NUM - ceng - j] = start++;
for (j = 0; j < NUM - ceng * 2 + 1; j++)
Array[NUM - ceng - j][ceng - 1] = start++;

if (ceng++ < (NUM + 1) / 2)
{
func(start, ceng);
}
else
{
if (NUM % 2 != 0)
{
Array[NUM / 2][NUM / 2] = start;
}
}
}
int main(int argc, char* argv[])
{
int j = 0, k = 0;
func(1, 1);
for (j = 0; j < NUM; j++)
{
for (k = 0; k < NUM; k++)
{
printf("%2d ", Array[j][k]);
}
printf("\n");
}
getchar();
return 0;
}
该答案已被锁定,无法对其进行评论,编辑及投票。
()
评论 (0)链接 • 2012-08-13
德问是一个专业的编程问答社区,请 登录注册 后再提交答案