文章

98

粉丝

66

获赞

102

访问

16.4k

头像
旋转方阵(模拟) 题解:
P1216 福州大学2018年机试题
发布于2024年5月18日 23:37
阅读数 178

#include<bits/stdc++.h>
using namespace std;

const int N = 110;
int n, cnt = 1;
int res[N][N];	


int main()
{
	cin >> n;

	int top = 1,bottom = n,left = 1,right = n;
    int cnt = 1;
    while(1)
	{
        for(int i = top; i <= bottom; i ++) res[i][left] = cnt ++;
        left ++;
        if(left > right) break;
        
        for(int i = left; i <= right; i ++) res[bottom][i] = cnt ++;
        bottom --;
        if(top > bottom) break;
        
        for(int i = bottom; i >= top; i --) res[i][right] = cnt ++;
        right --;
        if(right < left) break;
        
        for(int i = right; i >= left; i --) res[top][i] = cnt ++;
        top ++;
       	if(bottom < top) break;
    }
		
	for(int i = 1; i <= n; i ++)
	{
		for(int j = 1; j <= n; j ++)
			printf("%-4d", res[i][j]);
		cout << endl;
	}
	
	return 0; 
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发