求助:一个蜗牛壳图形问题分100分,有题解马上给分!

snowman_pc 2004-11-16 08:14:24
求助:


打图形问题

打出这样
7 8 9
6 1 2
5 4 3
急!马上给分。谢谢!
...全文
570 33 打赏 收藏 转发到动态 举报
写回复
用AI写文章
33 条回复
切换为时间正序
请发表友善的回复…
发表回复
carylin 2004-12-03
  • 打赏
  • 举报
回复
to googlex(乱码)

to :carylin(别信我,我在说谎)switch(*pDirection)中4个"if (arr[*pR + 1][*pC] == -1)"没有嘛!可以删去吧
==================================================================
怎么沒有呢?不然我怎么控製改變方嚮啊?-1是個標誌,用以錶明還沒有處理的地方。main函數中語句塊
//initialization
for (i = 0; i < row; ++i)
{
for (int j = 0; j < row; ++j)
{
arr[i][j] = -1;
}
}
不是嗎?
snowman_pc 2004-11-18
  • 打赏
  • 举报
回复
我看一下,没问题马上给分
wenyin1 2004-11-18
  • 打赏
  • 举报
回复
#include<stdio.h>
#define value 11 //定义一个方阵大小

void main()
{
int i=0,j=0,row=0,col=0,max=0;
int num[value][value];

for (i=0;i<value;i++)
{
for(j=0;j<value;j++)
{
max++;
num[i][j]=1;
}

}

row=value;
//看作为n个空心的矩形,从最外面一个开始,一个一个赋值
while(row!=col+1)
{
//上面的
for (i=row;i>col;i--)
{
num[col][i-1]=max--;
}

//左面的
for (i=col+1;i<row;i++)
{
num[i][col]=max--;
}

//下面的
for(i=col+1;i<row;i++)
{
num[row-1][i]=max--;
}
//右面的
for(i=row-1;i>col+1;i--)
{
num[i-1][row-1]=max--;
}

col++;
row--;
}

//输出
for (i=0;i<value;i++)
{
for(j=0;j<value;j++)
{
printf("%3d ",num[i][j]);
}
printf("\n");
}

}
wenyin1 2004-11-18
  • 打赏
  • 举报
回复
#include<stdio.h>
#define value 11
void main()
{
int i=0,j=0,m=0,n=0,max=0;
int num[value][value];
for (i=0;i<value;i++)
{
for(j=0;j<value;j++)
{
max++;
num[i][j]=1;
}

}

m=value;
while(m!=n+1)
{
for (i=m;i>n;i--)
{
num[n][i-1]=max--;
}
for (i=n+1;i<m;i++)
{
num[i][n]=max--;
}
for(i=n+1;i<m;i++)
{
num[m-1][i]=max--;
}
for(i=m-1;i>n+1;i--)
{
num[i-1][m-1]=max--;
}

n++;
m--;
}
for (i=0;i<value;i++)
{
for(j=0;j<value;j++)
{
printf("%3d ",num[i][j]);
}
printf("\n");
}

}
mathe 2004-11-17
  • 打赏
  • 举报
回复
取n=1,就是上面3*3的图,n=2,就是5*5的图....
mathe 2004-11-17
  • 打赏
  • 举报
回复
使用数组那也太简单了,看我的,不用任何数组
for(j=n;j>=-n;j--){
for(i=-n;i<=n;i++){
if(i-j>0&&i+j>=0){
printf("%d\t",(2*i-1)*(2*i-1)+i-j);
}else if(i-j>=0&&i+j<=0){
printf("%d\t", (-2*j-1)*(-2*j-1)-3*j-i);
}else if(i-j<=0&&i+j>=0){
printf("%d\t", (2*j+1)*(2*j+1)-j+i);
}else{//i=j<=0&&i+j<=0
printf("%d\t", (-2*i+1)*(-2*i+1)+3*i+j);
}
}
}
haha52 2004-11-17
  • 打赏
  • 举报
回复
从外向里一圈一圈的将值填入数组,注意距阵坐标的变化,任意圈数都行.
觉得比较机械的做法,还没想到更好的。
googlex 2004-11-17
  • 打赏
  • 举报
回复
to :carylin(别信我,我在说谎)switch(*pDirection)中4个"if (arr[*pR + 1][*pC] == -1)"没有嘛!可以删去吧
rszhangwj 2004-11-17
  • 打赏
  • 举报
回复
高手太多,没有发言权
dashang 2004-11-17
  • 打赏
  • 举报
回复
.....
davidhw 2004-11-16
  • 打赏
  • 举报
回复
数值是125时图形是这样的吗?
21 22 23 24 25
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13
lamputa_lito 2004-11-16
  • 打赏
  • 举报
回复
只有奇数的平方才能打印出这样的数,因此可以建议先安要求存储到一维数组中,再按顺序打出
=====================================================
不同意,奇偶数都可以的。
lamputa_lito 2004-11-16
  • 打赏
  • 举报
回复
用我哪个,输入多少都行(不要大于100,要大于100把arr数组改大就ok了)
zxl_llx 2004-11-16
  • 打赏
  • 举报
回复
只有奇数的平方才能打印出这样的数,因此可以建议先安要求存储到一维数组中,再按顺序打出
red98 2004-11-16
  • 打赏
  • 举报
回复
#include<stdio.h>

int main()
{
int rows=0;
int max=0;
int distance=1;
int center=0;
int m=0;
int n=0;
int array[10][10];
static int num=1;
int i=0;
int j=0;
printf("how many rows do you want to output(rows<=10):\n");
scanf("%d",&rows);
max=rows*rows;
if((rows%2)==1)
{
center=m=n=(rows-1)/2;
}
else
{
center=m=n=(rows/2-1);
}
array[m][n]=num++;

n12:
if((n+1)<rows&&(n+1-center)<=distance)
{
array[m][n+1]=num++;
if(num>max)
{
goto end;
}
n+=1;
goto n12;
}
else
{
goto m12;
}

n21:
if((n-1)>=0&&(n-1-center)<=distance&&(n-1-center)>=-distance)
{
array[m][n-1]=num++;
if(num>max)
{
goto end;
}
n-=1;
goto n21;
}
else
{
goto m21;
}

m21:
if((m-1)>=0&&(m-1-center)<=distance&&(m-1-center)>=-distance)
{
array[m-1][n]=num++;
if(num>max)
{
goto end;
}
m-=1;
goto m21;
}
else
{
distance+=1;
goto n12;
}

m12:
if((m+1)<rows&&(m+1-center)<=distance)
{
array[m+1][n]=num++;
if(num>max)
{
goto end;
}
m+=1;
goto m12;
}
else
{
goto n21;
}

end:
for(i=0;i<rows;i++)
{
for(j=0;j<rows;j++)
{
printf("\t%d",array[i][j]);
}
printf("\n");
}
return(0);
}
mathe 2004-11-16
  • 打赏
  • 举报
回复
呵呵,问题问的不好,应该说明扩展问题的情况,不然就
main(){
printf("\t7\t8\t9\n"
"\t6\t1\t2\n"
"\t5\t4\t3\n");
}
lamputa_lito 2004-11-16
  • 打赏
  • 举报
回复
int main(int argc, char* argv[])
{
int n = 0;
cout<<"输入方阵列数"<<endl;
cin>>n;
int arr[100][100] = {0};
int len = n;
int xcur = n - 1;
int ycur = 0;
int num = n*n;
while(1)
{
int tempxcur = xcur;
int tempycur = ycur;
int templen = len;
if( templen <= 0 )
break;
for( ; templen >= 1 ; templen-- )
{
arr[tempycur][tempxcur] = num;
num--;
tempxcur--;
}
tempycur++;
tempxcur++;
templen = len-1;
if( templen <= 0 )
break;
for( ; templen >= 1 ; templen-- )
{
arr[tempycur][tempxcur] = num;
num--;
tempycur++;
}
tempycur--;
tempxcur++;
templen = len-1;
if( templen <= 0 )
break;
for( ; templen >= 1 ; templen-- )
{
arr[tempycur][tempxcur] = num;
num--;
tempxcur++;
}
tempxcur--;
tempycur--;
templen = len-2;
if( templen <= 0 )
break;
for( ; templen >= 1 ; templen-- )
{
arr[tempycur][tempxcur] = num;
num--;
tempycur--;
}
len = len - 2;
xcur--;
ycur++;
}
for( int i = 0 ; i < n ; i++ )
{
for( int j = 0 ; j < n ; j ++ )
{
if( arr[i][j] < 10 )
cout<<arr[i][j]<<" ";
else
if( arr[i][j] < 100)
cout<<arr[i][j]<<" ";
else
if( arr[i][j] < 1000 )
cout<<arr[i][j]<<" ";
else
if( arr[i][j] < 10000 )
cout<<arr[i][j]<<" ";
else
if( arr[i][j] < 100000 )
cout<<arr[i][j]<<"";
}
cout<<endl;
}
return 0;

}
Aylazhang 2004-11-16
  • 打赏
  • 举报
回复
printf() ^_^
carylin 2004-11-16
  • 打赏
  • 举报
回复
函数void ChangeDirection(int *pR, int *pC, int **arr, int row, int *pDirection)应写成
void ChangeDirection(int r, int c, int **arr, int row, int *pDirection)的,算了,不改了,搂住自己改改好了。呵呵……
carylin 2004-11-16
  • 打赏
  • 举报
回复
#include <iostream>
#include <cassert>
using namespace std;

const int UP = 1;
const int DOWN = 2;
const int LEFT = 3;
const int RIGHT = 4;

void ChangeDirection(int *pR, int *pC, int **arr, int row, int *pDirection)
{
switch(*pDirection)
{
case RIGHT:
if (arr[*pR + 1][*pC] == -1) {*pDirection = DOWN;}
break;
case DOWN:
if (arr[*pR][*pC - 1] == -1) {*pDirection = LEFT;}
break;
case LEFT:
if (arr[*pR - 1][*pC] == -1) {*pDirection = UP;}
break;
case UP:
if (arr[*pR][*pC + 1] == -1) {*pDirection = RIGHT;}
break;
default:
assert(false);
}
}

int main()
{
int row;
do
{
cout << "input the size of row :" << endl;
cin >> row;
} while(row % 2 == 0);

int **arr;
arr=new int* [row];
for (int i = 0; i < row; i++) arr[i]=new int[row];

//initialization
for (i = 0; i < row; ++i)
{
for (int j = 0; j < row; ++j)
{
arr[i][j] = -1;
}
}

i = 1;
int direction = RIGHT;
int r = row / 2;
int c = r;
while(i <= row * row)
{
arr[r][c] = i++;

switch(direction)
{
case RIGHT:
++c;
break;
case DOWN:
++r;
break;
case LEFT:
--c;
break;
case UP:
--r;
break;
default:
assert(false);
}

ChangeDirection(&r, &c, arr, row, &direction);
}

//Display the result
for (i = 0; i < row; ++i)
{
for (int j = 0; j < row; ++j)
{
cout << arr[i][j] << "\t";
}
cout << endl;
}

for (i = 0; i < row; ++i){delete [] arr[i]; }
delete []arr;

return (0);
}
加载更多回复(13)

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧