螺旋排列的矩阵,设1点的坐标是(0,0),x方向向右为正,y方向向下为正。如7的坐标为(-1,-1),2的坐标为(0,1)。编程实现输入任意一点坐标(x,y),输出对应的数字。#include <iostream> using namespace std; int abs(int x) { return x>0?x:(0-x); } int luoxuan(int x,int y) { int result=0; int ceng=(abs(x)>abs(y)?abs(x):abs(y)); int cengMax=(2*ceng+1)*(2*ceng+1); if(0==y+ceng) { result=cengMax-(ceng-x); } else { if(0==x+ceng) { result=(cengMax-ceng*2)-(ceng+y); } else { if(y==ceng) { result=(cengMax-ceng*4)-(ceng+x); } else { result=(cengMax-ceng*6)-(ceng-y); } } } return result; } int main() { for(int i=-4;i<=4;i++) { for(int j=-4;j<=4;j++) { cout<<luoxuan(j,i)<<"/t"; } cout<<endl; } getchar(); return 0; } 还有以一种螺旋,从外向内递增的:#include<iostream> #include<cmath> using namespace std; int vMin(int x,int y) { return x<y?x:y; } void luoxuan(int N) { for(int i=0;i!=N;i++) { for(int j=0;j!=N;j++) { int quan=vMin(vMin(vMin(i,j),N-i-1),N-j-1);//圈层,最外层quan为0 int quanMin=1+4*((N-1)*quan-((quan-1)*2*quan/2));//没圈层左上角为该圈最小数 //cout<<quan<<"/t"; int yuansu=0;对应的元素 if(i==quan) { yuansu=quanMin+j-quan; } else if(j==(N-quan-1)) { yuansu=quanMin+(N-2*quan-1)+i-quan; } else if(i==(N-quan-1)) { yuansu=quanMin+3*(N-2*quan-1)-j+quan; } else { yuansu=quanMin+4*(N-2*quan-1)-i+quan; } cout<<yuansu<<"/t"; } cout<<endl; } } int main() { luoxuan(8); getchar(); return 0; }