1105. Spiral Matrix (25)
#include <iostream>
#include <algorithm>
#include <algorithm>using namespace std;int num[10010], arr[110][110];int cmp(int a, int b)
{return a > b;
}int main()
{int d;scanf("%d", &d);int i;for(i = 1; i <= d; i++){scanf("%d", &num[i]);}int begin = sqrt(d * 1.0);while(begin * begin < d){begin++;}int m, n;for(i = begin; i <= d; i++){if(d % i == 0){m = i;n = d / i;break;}}sort(num + 1, num + d + 1, cmp);int x = 1, y = 0, count = 0;while(count < d){while(count < d && y + 1 <= n && arr[x][y + 1] == 0){arr[x][++y] = num[++count];}while(count < d && x + 1 <= m && arr[x + 1][y] == 0){arr[++x][y] = num[++count];}while(count < d && y - 1 >= 1 && arr[x][y - 1] == 0){arr[x][--y] = num[++count];}while(count < d && x - 1 >= 1 && arr[x - 1][y] == 0){arr[--x][y] = num[++count];}}int j;for(i = 1; i <= m; i++){for(j = 1; j <= n; j++){if(j > 1){printf(" ");}printf("%d", arr[i][j]);}printf("\n");}system("pause");return 0;
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/975568.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!