螺旋矩阵
2017-03-11 23:28
218 查看
1050. 螺旋矩阵(25)
时间限制
150 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
本题要求将给定的N个正整数按非递增的顺序,填入“螺旋矩阵”。所谓“螺旋矩阵”,是指从左上角第1个格子开始,按顺时针螺旋方向填充。要求矩阵的规模为m行n列,满足条件:m*n等于N;m>=n;且m-n取所有可能值中的最小值。
输入格式:
输入在第1行中给出一个正整数N,第2行给出N个待填充的正整数。所有数字不超过104,相邻数字以空格分隔。
输出格式:
输出螺旋矩阵。每行n个数字,共m行。相邻数字以1个空格分隔,行末不得有多余空格。
输入样例:
12
37 76 20 98 76 42 53 95 60 81 58 93
输出样例:
98 95 93
42 37 81
53 20 76
58 60 76
java代码:
package mypackage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class SpiralData {
public static void main(String[] args) {
SpiralData data = new SpiralData();
List<Integer> list = new ArrayList<Integer>();
@SuppressWarnings("resource")
Scanner cin = new Scanner(System.in);
int nextInt = cin.nextInt();
// 用nextInt确定行列
int colum = data.colum(nextInt);
int row = nextInt / colum;
int[][] intData = new int[row][colum];
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
String str = in.nextLine();
String[] split = str.split(" ");
if (str != null) {
for (int i = 0; i < split.length; i++) {
list.add(Integer.parseInt(split[i]));
}
// 升序排列
Collections.sort(list);// Sorts the specified list into ascending
// order
int k = 0;
int[] save = new int[nextInt];
int[][] state = new int[row][colum];// 逆序排列
for (int i = list.size() - 1; i > -1; i--) {
save[k++] = list.get(i);
}
// 将排好序的数填充入二位数组;填充边
int flag = 1;
int x1 = 0, y1 = 0;
for (int t = 0; t < save.length; t++) {
intData[x1][y1] = save[t];
state[x1][y1] = 1;
switch (flag) {
case 1:
if (y1 + 1 >= colum || state[x1][y1 + 1] != 0) {
x1++;
flag = 2;
} else {
y1++;
}
break;
case 2:
if (x1 + 1 >= row || state[x1 + 1][y1] != 0) {
y1--;
flag = 3;
} else {
x1++;
}
break;
case 3:
if (y1 - 1 < 0 || state[x1][y1 - 1] != 0) {
x1--;
flag = 4;
} else {
y1--;
}
break;
case 4:
if (x1 - 1 < 0 || state[x1 - 1][y1] != 0) {
y1++;
flag = 1;
} else {
x1--;
}
break;
default:
break;
}
}
for (int i = 0; i < intData.length; i++) {
for (int j = 0; j < intData[i].length; j++) {
System.out.print(intData[i][j]);
if (j + 1 < intData[i].length + 1)// 小于边界值就输出空格,大于就换行
System.out.print(" ");
}
System.out.println();
}
}
}
public int colum(int inner) {// r*c=n,c>=r
int i;
for (i = 2; i <= inner;)// 注意是小于等于
{
if (inner % i == 0) {
// System.out.print(i + " ");
inner /= i;// 记得每次进行除i
} else
i++;// 因为求的是最小因子,所以只有在i小的时候不能除尽,才进行i加一的操作,
}
System.out.println("colum:" + i);
return i;
}
}
时间限制
150 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
本题要求将给定的N个正整数按非递增的顺序,填入“螺旋矩阵”。所谓“螺旋矩阵”,是指从左上角第1个格子开始,按顺时针螺旋方向填充。要求矩阵的规模为m行n列,满足条件:m*n等于N;m>=n;且m-n取所有可能值中的最小值。
输入格式:
输入在第1行中给出一个正整数N,第2行给出N个待填充的正整数。所有数字不超过104,相邻数字以空格分隔。
输出格式:
输出螺旋矩阵。每行n个数字,共m行。相邻数字以1个空格分隔,行末不得有多余空格。
输入样例:
12
37 76 20 98 76 42 53 95 60 81 58 93
输出样例:
98 95 93
42 37 81
53 20 76
58 60 76
java代码:
package mypackage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class SpiralData {
public static void main(String[] args) {
SpiralData data = new SpiralData();
List<Integer> list = new ArrayList<Integer>();
@SuppressWarnings("resource")
Scanner cin = new Scanner(System.in);
int nextInt = cin.nextInt();
// 用nextInt确定行列
int colum = data.colum(nextInt);
int row = nextInt / colum;
int[][] intData = new int[row][colum];
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
String str = in.nextLine();
String[] split = str.split(" ");
if (str != null) {
for (int i = 0; i < split.length; i++) {
list.add(Integer.parseInt(split[i]));
}
// 升序排列
Collections.sort(list);// Sorts the specified list into ascending
// order
int k = 0;
int[] save = new int[nextInt];
int[][] state = new int[row][colum];// 逆序排列
for (int i = list.size() - 1; i > -1; i--) {
save[k++] = list.get(i);
}
// 将排好序的数填充入二位数组;填充边
int flag = 1;
int x1 = 0, y1 = 0;
for (int t = 0; t < save.length; t++) {
intData[x1][y1] = save[t];
state[x1][y1] = 1;
switch (flag) {
case 1:
if (y1 + 1 >= colum || state[x1][y1 + 1] != 0) {
x1++;
flag = 2;
} else {
y1++;
}
break;
case 2:
if (x1 + 1 >= row || state[x1 + 1][y1] != 0) {
y1--;
flag = 3;
} else {
x1++;
}
break;
case 3:
if (y1 - 1 < 0 || state[x1][y1 - 1] != 0) {
x1--;
flag = 4;
} else {
y1--;
}
break;
case 4:
if (x1 - 1 < 0 || state[x1 - 1][y1] != 0) {
y1++;
flag = 1;
} else {
x1--;
}
break;
default:
break;
}
}
for (int i = 0; i < intData.length; i++) {
for (int j = 0; j < intData[i].length; j++) {
System.out.print(intData[i][j]);
if (j + 1 < intData[i].length + 1)// 小于边界值就输出空格,大于就换行
System.out.print(" ");
}
System.out.println();
}
}
}
public int colum(int inner) {// r*c=n,c>=r
int i;
for (i = 2; i <= inner;)// 注意是小于等于
{
if (inner % i == 0) {
// System.out.print(i + " ");
inner /= i;// 记得每次进行除i
} else
i++;// 因为求的是最小因子,所以只有在i小的时候不能除尽,才进行i加一的操作,
}
System.out.println("colum:" + i);
return i;
}
}
相关文章推荐
- C语言 经典题目螺旋矩阵 实例详解
- 各种螺旋矩阵
- 矩阵螺旋输出打印原代码
- 1050. 螺旋矩阵(25)
- [C++]LeetCode: 111 Spiral Matrix II (螺旋写入矩阵)
- 螺旋矩阵
- PAT乙级1050 螺旋矩阵
- 矩阵填数-倒填、螺旋、蛇形
- 螺旋队列(螺旋矩阵)——创新工场面试总结2
- 螺旋矩阵----在博客园上看见别人的算法没有看明白自己搞了一个。
- 由内向外的螺旋矩阵
- 螺旋矩阵
- 螺旋矩阵
- 1050. 螺旋矩阵(25)
- 打印螺旋举矩阵-java
- 螺旋矩阵
- PAT (Basic Level) Practise (中文)1050. 螺旋矩阵(25)
- 螺旋矩阵问题
- 1050. 螺旋矩阵(25).
- 螺旋矩阵 之三