螺旋矩阵:)
2010-05-27 22:57
211 查看
/*
一个矩阵如果满足如下关系:
7 8 9 10
6 1 2 11
5 4 3 12
16 15 14 13
俺给定义螺旋矩阵,嘿嘿。设1的坐标为“(0,0)”
7的坐标为(-1,-1),编写一个小程序,使程序做到
输入坐标(x,y)之后显示出相应的数字
*/
#include<stdio.h>
void main()
{
int tempx, tempy, temp, x, y, num, numx,numy;
int index = 0;//用来辅助求(x,y)所对应的数值
printf("请依次输入您想要查询的数的横坐标和纵坐标:(x,y)/n");
scanf("(%d,%d)",&x,&y);
//计算(x,y)在由内向外算起的第几圈上
if (x >= 1)
tempx = x;
if(x <= 0)
tempx = 1-x;
if (y >= 1)
tempy = y;
if ( y<= 0)
tempy = 1 - y;
temp = ( tempx > tempy ? tempx : tempy);
//计算该圈的第一个元素是多少,以及对应的坐标
num = 4 * ( temp - 1) * ( temp - 1) +1;
numx = temp -1;
numy = 1 - temp;
//计算所对应的数值
if (numy != y)
{
for (index=0; index < 2*temp-2; index++)
{
--numx;
++num;
}
}
else
{
for (index=0; index < 2*temp-2; index++)
{
if (numx == x)
{
printf("(%d, %d)所对应的值为:%d/n", x, y ,num);
return;//注意一旦匹配,程序将返回。
}
--numx;
++num;
}
}
if (numx != x)
{
for (index=0; index < 2*temp-1; index++)
{
++numy;
++num;
}
}
else
{
for (index=0; index < 2*temp-1; index++)
{
if (numy == y)
{
printf("(%d, %d)所对应的值为:%d/n", x, y ,num);
return;
}
++numy;
++num;
}
}
if (numy != y)
{
for (index=0; index < 2*temp-1; index++)
{
++numx;
++num;
}
}
else
{
for (index=0; index < 2*temp-1; index++)
{
if (numx == x )
{
printf("(%d, %d)所对应的值为:%d/n", x, y ,num);
return;
}
++numx;
++num;
}
}
if(numx != x)
{
for (index=0; index < 2*temp; index++)
{
--numy;
++num;
}
}
else
{
for (index=0; index < 2*temp; index++)
{
if ( numy == y)
{
printf("(%d, %d)所对应的值为:%d/n", x, y ,num);
return;
}
--numy;
++num;
}
}
// 下面这种循环方式也可以,但是可能增加了多次判断,不过代码相对简洁。
/*
for (index=0; index < 2*temp-2; index++)
{
if (numx == x && numy == y)
{
printf("(%d, %d)所对应的值为:%d/n", x, y ,num);
return;
}
--numx;
++num;
}
for (index=0; index < 2*temp-1; index++)
{
if (numx == x && numy == y)
{
printf("(%d, %d)所对应的值为:%d/n", x, y ,num);
return;
}
++numy;
++num;
}
for (index=0; index < 2*temp-1; index++)
{
if (numx == x && numy == y)
{
printf("(%d, %d)所对应的值为:%d/n", x, y ,num);
return;
}
++numx;
++num;
}
for (index=0; index < 2*temp; index++)
{
if (numx == x && numy == y)
{
printf("(%d, %d)所对应的值为:%d/n", x, y ,num);
return;
}
--numy;
++num;
}
*/
}
一个矩阵如果满足如下关系:
7 8 9 10
6 1 2 11
5 4 3 12
16 15 14 13
俺给定义螺旋矩阵,嘿嘿。设1的坐标为“(0,0)”
7的坐标为(-1,-1),编写一个小程序,使程序做到
输入坐标(x,y)之后显示出相应的数字
*/
#include<stdio.h>
void main()
{
int tempx, tempy, temp, x, y, num, numx,numy;
int index = 0;//用来辅助求(x,y)所对应的数值
printf("请依次输入您想要查询的数的横坐标和纵坐标:(x,y)/n");
scanf("(%d,%d)",&x,&y);
//计算(x,y)在由内向外算起的第几圈上
if (x >= 1)
tempx = x;
if(x <= 0)
tempx = 1-x;
if (y >= 1)
tempy = y;
if ( y<= 0)
tempy = 1 - y;
temp = ( tempx > tempy ? tempx : tempy);
//计算该圈的第一个元素是多少,以及对应的坐标
num = 4 * ( temp - 1) * ( temp - 1) +1;
numx = temp -1;
numy = 1 - temp;
//计算所对应的数值
if (numy != y)
{
for (index=0; index < 2*temp-2; index++)
{
--numx;
++num;
}
}
else
{
for (index=0; index < 2*temp-2; index++)
{
if (numx == x)
{
printf("(%d, %d)所对应的值为:%d/n", x, y ,num);
return;//注意一旦匹配,程序将返回。
}
--numx;
++num;
}
}
if (numx != x)
{
for (index=0; index < 2*temp-1; index++)
{
++numy;
++num;
}
}
else
{
for (index=0; index < 2*temp-1; index++)
{
if (numy == y)
{
printf("(%d, %d)所对应的值为:%d/n", x, y ,num);
return;
}
++numy;
++num;
}
}
if (numy != y)
{
for (index=0; index < 2*temp-1; index++)
{
++numx;
++num;
}
}
else
{
for (index=0; index < 2*temp-1; index++)
{
if (numx == x )
{
printf("(%d, %d)所对应的值为:%d/n", x, y ,num);
return;
}
++numx;
++num;
}
}
if(numx != x)
{
for (index=0; index < 2*temp; index++)
{
--numy;
++num;
}
}
else
{
for (index=0; index < 2*temp; index++)
{
if ( numy == y)
{
printf("(%d, %d)所对应的值为:%d/n", x, y ,num);
return;
}
--numy;
++num;
}
}
// 下面这种循环方式也可以,但是可能增加了多次判断,不过代码相对简洁。
/*
for (index=0; index < 2*temp-2; index++)
{
if (numx == x && numy == y)
{
printf("(%d, %d)所对应的值为:%d/n", x, y ,num);
return;
}
--numx;
++num;
}
for (index=0; index < 2*temp-1; index++)
{
if (numx == x && numy == y)
{
printf("(%d, %d)所对应的值为:%d/n", x, y ,num);
return;
}
++numy;
++num;
}
for (index=0; index < 2*temp-1; index++)
{
if (numx == x && numy == y)
{
printf("(%d, %d)所对应的值为:%d/n", x, y ,num);
return;
}
++numx;
++num;
}
for (index=0; index < 2*temp; index++)
{
if (numx == x && numy == y)
{
printf("(%d, %d)所对应的值为:%d/n", x, y ,num);
return;
}
--numy;
++num;
}
*/
}