蓝桥杯:基础练习 回形取数
2016-03-21 13:25
429 查看
基础练习 回形取数
时间限制:1.0s 内存限制:512.0MB
问题描述
回形取数就是沿矩阵的边取数,若当前方向上无数可取或已经取过,则左转90度。一开始位于矩阵左上角,方向向下。
输入格式
输入第一行是两个不超过200的正整数m, n,表示矩阵的行和列。接下来m行每行n个整数,表示这个矩阵。
输出格式
输出只有一行,共mn个数,为输入矩阵回形取数得到的结果。数之间用一个空格分隔,行末不要有多余的空格。
样例输入
3 3
1 2 3
4 5 6
7 8 9
样例输出
1 4 7 8 9 6 3 2 5
样例输入
3 2
1 2
3 4
5 6
样例输出
1 3 5 6 4 2
方法1:
时间限制:1.0s 内存限制:512.0MB
问题描述
回形取数就是沿矩阵的边取数,若当前方向上无数可取或已经取过,则左转90度。一开始位于矩阵左上角,方向向下。
输入格式
输入第一行是两个不超过200的正整数m, n,表示矩阵的行和列。接下来m行每行n个整数,表示这个矩阵。
输出格式
输出只有一行,共mn个数,为输入矩阵回形取数得到的结果。数之间用一个空格分隔,行末不要有多余的空格。
样例输入
3 3
1 2 3
4 5 6
7 8 9
样例输出
1 4 7 8 9 6 3 2 5
样例输入
3 2
1 2
3 4
5 6
样例输出
1 3 5 6 4 2
方法1:
import java.util.Scanner; public class Main{ private static void printArray(int m, int n, int[][] array){ int mark = 0; int sum = (m > n ? n : m) / 2; while(mark <= sum){ for(int i = mark; i < m - mark; i++){ // down: col is stable and col = mark. if(array[i][mark] != -1){ System.out.print(array[i][mark] + " "); array[i][mark] = -1; } } for(int i = mark + 1; i < n - mark; i++){ // right: row is stable and row = mark + 1. if(array[m - mark - 1][i] != -1){ System.out.print(array[m - mark - 1][i] + " "); array[m - mark - 1][i] = -1; } } for(int i = m - 1 - mark - 1; i >= mark; i--){ // up: col is stable and col = m - 1 - mark - 1. if(array[i][n - mark - 1] != -1){ System.out.print(array[i][n - mark - 1] + " "); array[i][n - mark - 1] = -1; } } for(int i = n - 1 - mark - 1; i > mark; i--){ // left: row is stable and row = n - 1 - mark - 1. if(array[mark][i] != -1){ System.out.print(array[mark][i] + " "); array[mark][i] = -1; } } mark ++; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int m = sc.nextInt(); int n = sc.nextInt(); int[][] array = new int[m] ; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { array[i][j] = sc.nextInt(); } } printArray(m, n, array); } }方法2:
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int mc = sc.nextInt(); int nr = sc.nextInt(); int[][] array = new int[mc][nr]; for (int i = 0; i < mc; i++) { for (int j = 0; j < nr; j++) { array[i][j] = sc.nextInt(); } } int count = 0; int circlecount = 0; int i, j, x = 0, y = 0; int m = mc; int n = nr; int number = 1; System.out.print(array[0][0]); array[0][0] = -1; for (i = 0, j = 0; number < mc * nr;) { if (i < m - 1 && j == nr - n) { // down if (array[i][j] != -1) { System.out.print(" " + array[i][j]); array[i][j] = -1; number++; } i++; } else if (i == m - 1 && j != n - 1) { // right if (array[i][j] != -1) { System.out.print(" " + array[i][j]); array[i][j] = -1; number++; } j++; } else if (j == n - 1 && i > mc - m) { // up if (array[i][j] != -1) { System.out.print(" " + array[i][j]); array[i][j] = -1; number++; } i--; } else if (j > nr - n && i == mc - m) { // left if (array[i][j] != -1) { System.out.print(" " + array[i][j]); array[i][j] = -1; number++; } j--; } else { if (array[i][j] != -1) { System.out.print(" " + array[i][j]); array[i][j] = -1; number++; } i++; } count++; if (count == 2 * (m + n) - 4) { circlecount++; j = circlecount; i = circlecount; m--; n--; count = 0; } } } }此题总是有三组测试数据超时,欢迎大家积极评论
相关文章推荐
- iOS开发-给服务器发送JSON数据
- CCS3超长文字显示省略号的方法
- OpenCV入门:简单图像处理实例——平滑滤波
- TreeSet详解
- day08
- Make sure you have Java JDK or JRE installed and the required ports are free
- ccf 送货
- 课堂测试
- [bzoj1826] [JSOI2010]缓存交换
- 汇编int21h,DOS调用(转)
- 根文件系统/etc下 inittab
- java中static final与final的不同
- LINUX学习笔记逻辑导图
- [leetcode] 156. Binary Tree Upside Down 解题报告
- uiautomatorviewer 识别android微信元素报错
- web项目启动报java.lang.ClassNotFoundException
- 单例模式
- html5之meta charset网页字符编码简写
- [置顶] 人工智能实战——人工神经网络(C库iOS交叉编译)
- [bzoj1483] [HNOI2009]梦幻布丁