题目描述:

输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵:

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.

输入:

输入可能包含多个测试样例,对于每个测试案例,

输入的第一行包括两个整数m和n(1<=m,n<=1000):表示矩阵的维数为m行n列。

接下来的m行,每行包括n个整数,表示矩阵的元素,其中每个元素a的取值范围为(1<=a<=10000)。

输出:

对应每个测试案例,输出一行,

按照从外向里以顺时针的顺序依次打印出每一个数字,每个数字后面都有一个空格。

样例输入:
4 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
样例输出:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 

这个题一开始是用哨兵的思想做的,在矩阵周围都设为0,碰到哨兵就转一个方向,开始设的哨兵值是0,结果一直超时,题目明明说 a >= 1的
只好设哨兵为-1了,代码如下
 #include <cstdio>
#include <cstring> int matrix[][];
int dir[][] = {{,},{,},{,-},{-,}}; int main(int argc, char const *argv[])
{
int m, n;
while(scanf("%d %d",&m,&n) != EOF) {
for(int i = ; i < ; i++) {
for(int j = ; j < ; j++) {
matrix[i][j] = -;
}
} for(int i = ; i <= m; i++) {
for(int j = ; j <= n; j++) {
scanf("%d",&matrix[i][j]);
}
} int tx = , ty = ;
int di = ;
int num = m*n;
int cnt = ;
while(cnt < num) {
tx = tx + dir[di][];
ty = ty + dir[di][];
while(matrix[tx][ty] != -) {
printf("%d ",matrix[tx][ty]);
matrix[tx][ty] = -;
cnt++;
tx = tx + dir[di][];
ty = ty + dir[di][];
}
tx = tx - dir[di][];
ty = ty - dir[di][];
di = (di+) % ; }
puts("");
}
return ;
}

但是代码居然跑了980ms,

试着修改了一下,按四个方向判断输出

 #include <cstdio>
#include <cstring> int matrix[][];
int dir[][] = {{,},{,},{,-},{-,}}; int main(int argc, char const *argv[])
{
int m, n;
while(scanf("%d %d",&m,&n) != EOF) {
for(int i = ; i < ; i++) {
for(int j = ; j < ; j++) {
matrix[i][j] = -;
}
} for(int i = ; i <= m; i++) {
for(int j = ; j <= n; j++) {
scanf("%d",&matrix[i][j]);
}
} int tx = , ty = ;
int di = ;
int num = m*n;
int cnt = ;
while(cnt < num) {
int i;
for(i = ty+; matrix[tx][i] != -; i++) {
printf("%d ",matrix[tx][i]);
cnt++;
matrix[tx][i] = -;
}
ty = i-;
for(i = tx+; matrix[i][ty] != -; i++) {
printf("%d ",matrix[i][ty]);
cnt++;
matrix[i][ty] = -;
}
tx = i-;
for(i = ty-; matrix[tx][i] != -; i--) {
printf("%d ",matrix[tx][i]);
cnt++;
matrix[tx][i] = -;
}
ty = i+;
for(i = tx-; matrix[i][ty] != -; i--) {
printf("%d ",matrix[i][ty]);
cnt++;
matrix[i][ty] = -;
}
tx = i+;
}
puts("");
}
return ;
}

跑了940ms

考虑时间这么长,是不是赋值语句导致的,只好换了个思路,用普通的界限来判断输出了

 #include <cstdio>
#include <cstring> int matrix[][]; int main(int argc, char const *argv[])
{
int m, n;
while(scanf("%d %d",&m,&n) != EOF) { for(int i = ; i <= m; i++) {
for(int j = ; j <= n; j++) {
scanf("%d",&matrix[i][j]);
}
} int tx = , ty = ;
int num = m*n;
int cnt = ;
int xt = m, yt = n, xf = , yf = ;
while(cnt < num) {
int i;
for(i = ty+; i <= yt; i++) {
printf("%d ",matrix[tx][i]);
cnt++;
}
yt--;
ty = i-;
if(cnt == num) {
break;
}
for(i = tx+; i <= xt; i++) {
printf("%d ",matrix[i][ty]);
cnt++;
}
xt--;
tx = i-;
if(cnt == num) {
break;
}
for(i = ty-; i >= yf; i--) {
printf("%d ",matrix[tx][i]);
cnt++;
}
ty = i+;
yf++;
if(cnt == num) {
break;
} for(i = tx-; i >= xf; i--) {
printf("%d ",matrix[i][ty]);
cnt++;
}
tx = i+;
xf++;
}
puts("");
}
return ;
}

时间果然变少,只花了500ms

九度oj 题目1391:顺时针打印矩阵的更多相关文章

  1. 剑指Offer - 九度1391 - 顺时针打印矩阵

    剑指Offer - 九度1391 - 顺时针打印矩阵2013-11-24 04:55 题目描述: 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 ...

  2. 九度OJ 题目1384:二维数组中的查找

    /********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...

  3. hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人

    钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  4. 九度oj题目&amp;吉大考研11年机试题全解

    九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码).    http://ac.jobdu.com/problem.php?pid=11 ...

  5. 九度oj 题目1007:奥运排序问题

    九度oj 题目1007:奥运排序问题   恢复 题目描述: 按要求,给国家进行排名. 输入:                        有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...

  6. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  7. 九度OJ题目1105:字符串的反码

    tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...

  8. 九度oj题目1009:二叉搜索树

    题目描述: 判断两序列是否为同一二叉搜索树序列 输入:                        开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...

  9. 九度oj题目1002:Grading

    //不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...

随机推荐

  1. vue.js的package.json相关问题解惑

    使用vue-cli创建vue.webpack项目,在项目中用到了iSlider移动端滑动插件,只在本地命令工具中npm install islider.js:提交之后,partner下载代码后一直运行 ...

  2. SingletonLoginUser

    package cn.com.jgt.view{ import flash.errors.IllegalOperationError; /** * actionscript类的构造方法不能是priva ...

  3. HYSBZ 1010 玩具装箱toy (决策单调DP)

    题意: 有n个玩具,要将它们分为若干组,玩具长度C可能不同.给出n个玩具的摆放顺序,连续的任意多个玩具都可以成为一组.区间[i,j]成为一组的费用是cost=(j-i+Sigma(Ck)-L)2且i& ...

  4. UVA 10891 Game of Sum (决策优化)

    这是一个零和博弈,最高得分只和序列以及谁先手有关. d[i][j],表示i到j的序列当前取的这个人的最高得分,转移以后状态是新的区间和另一个人取,从中取最小值. 决策的最小值也可递推. #includ ...

  5. word中在空白处加下划线不显示解决

    终极解决:Ctrl + Shift + Space Alt + 选择,竖向选择.和VS,其他一些编辑器一样

  6. c++作业:输入两个整数,用函数求两数之和。函数外部声明有什么作用?

    #include <iostream> using namespace std; int main(){ //求两数的和? int a,b,s; cout<<"请你输 ...

  7. 在 Ubuntu 环境下实现插入鼠标自动关闭触摸板

    Ubuntu 以及其他衍生版本,如 Linux Mint 等等都可以用官方的 PPA 来安装"触摸板指示"应用程序.打开一个终端,运行以下命令: sudo add-apt-repo ...

  8. MySQL 创建函数失败提示1418

    MySQL 创建函数失败提示1418 在创建函数时,往往会遇到创建函数失败的情形,除去书写的创建函数的sql语句本身语法错误之外,还会碰到一个错误就是, 1418:This function has ...

  9. Python开发环境与开发软件的安装

    Python开发的必要因素: 开发软件:PyCharm 社区版 PyCharm安装过程: 首先去官网下载:(链接为:  https://www.jetbrains.com/pycharm/downlo ...

  10. 【Arduino开发板刷Bootloader01】

    其接线方式就是:   Programmer(工具开发板)                Being programmed(目标开发板)                              Vcc ...