您的位置:首页 > 其它

1105. Spiral Matrix (25)

2017-06-21 16:04 387 查看


1105. Spiral Matrix (25)

时间限制

150 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasing order. Aspiral matrix is filled in from the first element at the upper-left corner, then move in a clockwise spiral. The
matrix has m rows and n columns, where m and n satisfy the following: m*n must be equal to N;m>=n; and m-n is
the minimum of all the possible values.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N. Then the next line contains N positive integers to be filled into the spiral matrix. All the numbers are no more than 104. The
numbers in a line are separated by spaces.

Output Specification:

For each test case, output the resulting matrix in m lines, each contains n numbers. There must be exactly 1 space between two adjacent numbers, and no extra space at the end of each line.
Sample Input:
12
37 76 20 98 76 42 53 95 60 81 58 93

Sample Output:
98 95 93
42 37 81
53 20 76
58 60 76


模拟,上下左右顺时针循环。

刷乙级的时候参考别人的,现在还记着。。。。

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int comp(const void *a,const void *b){
return *(int *)b-*(int *)a;
}
int main(){
int N,m,n,i,j;
scanf("%d",&N);
int min=9999999;
double temp=sqrt(N);
for(i=N;i>=temp;i--){//...
if(N%i==0){
if(i-N%i<min){
min=i-N%i;
m=i;
n=N/i;//。。。。
}
}
}
int a[m]
;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
a[i][j]=0;
}
}
int num
;
for(i=0;i<N;i++){
scanf("%d",&num[i]);
}
qsort(num,N,sizeof(int),comp);
int cou=0;
int row=0,col=-1;
while(cou<N){
for(i=col+1;i<n&&a[row][i]==0;i++){
a[row][i]=num[cou++];
}
col=i-1;
for(i=row+1;i<m&&a[i][col]==0;i++){
a[i][col]=num[cou++];
}
row=i-1;
for(i=col-1;i>=0&&a[row][i]==0;i--){
a[row][i]=num[cou++];
}
col=i+1;
for(i=row-1;i>=0&&a[i][col]==0;i--){
a[i][col]=num[cou++];
}
row=i+1;
}
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("%d",a[i][j]);
if(j==n-1){
printf("\n");
}
else{
printf(" ");
}
}
}
}


以前参考别人的:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int compare(const void *p, const void *q);

int main()
{
int N;
scanf("%d", &N);
int a
;
for (int i=0; i<N; i++) {
scanf("%d", &a[i]);
}
qsort(a, N, sizeof(int), compare);
int row ;
int col = sqrt(N);
while (N/col*col!=N) {
col--;
}
row=N/col;
// 计算行数与列数

int b[row][col];
for (int i=0; i<row; i++) {
for (int j=0; j<col; j++) {
b[i][j] = 0;
}
} // 二维数组初始化

int i = 0, j = 0;
int count = 0;
while (count != N) {
for ( ; j<col; j++) {
if (!b[i][j]) {
b[i][j] = a[count++];
} else {
break;
}
} // 向右走
j--; // 退出不满足的列
i++; // 进入下一行
for ( ; i<row; i++) {
if (!b[i][j]) {
b[i][j] = a[count++];
} else {
break;
}
} // 向下走
i--; // 退出不满足的行
j--; // 进入下一列 (向左)
for ( ; j>=0; j--) {
if (!b[i][j]) {
b[i][j] = a[count++];
} else {
break;
}
} // 向左走
i--; // 进入下一行 (向上)
j++; // 退出不满足的列
for ( ; i>=0; i--) {
if (!b[i][j]) {
b[i][j] = a[count++];
} else {
break;
}
} // 向上走
i++; // 退出不满足的行(下一步向右)
j++; // 进入下一列(向右)
}

for (int i=0; i<row; i++) {
for (int j=0; j<col; j++) {
printf ("%d", b[i][j]);
if (j < col-1) {
printf (" ");
}
}
printf ("\n");
}

return 0;
}

int compare(const void *p, const void *q) {
int x = *(int *)p;
int y = *(int *)q;
if (x > y) {
return -1;
} else if (x < y) {
return 1;
}
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: