九度oj 题目1391:顺时针打印矩阵
- 题目描述:
-
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵:
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:顺时针打印矩阵的更多相关文章
- 剑指Offer - 九度1391 - 顺时针打印矩阵
剑指Offer - 九度1391 - 顺时针打印矩阵2013-11-24 04:55 题目描述: 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 ...
- 九度OJ 题目1384:二维数组中的查找
/********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...
- hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人
钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 九度oj题目&吉大考研11年机试题全解
九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码). http://ac.jobdu.com/problem.php?pid=11 ...
- 九度oj 题目1007:奥运排序问题
九度oj 题目1007:奥运排序问题 恢复 题目描述: 按要求,给国家进行排名. 输入: 有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...
- 九度oj 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ题目1105:字符串的反码
tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...
- 九度oj题目1009:二叉搜索树
题目描述: 判断两序列是否为同一二叉搜索树序列 输入: 开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...
- 九度oj题目1002:Grading
//不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...
随机推荐
- Jenkins中启动从节点时,出现问题如何解决,问题:No Known Hosts...
Jenkins中,启动从节点时,出现如下问题如何解决:/root/.ssh/known_hosts [SSH] No Known Hosts file was found at /root/.ssh/ ...
- Mongodb之failed to create service entry worker thread
Mongodb "failed to create service entry worker thread" 错误. 系统:CentOS release 6.8 mongod.lo ...
- js 获取当前年月日时分秒星期
$("#aa").click(function () { var date = new Date(); this.year = date.getFullYear(); this.m ...
- JsonPath 语法 与 XPath 对比
JsonPath 语法 与 XPath 对比 XPath JSONPath Description / $ the root object/element . @ the current obje ...
- SQL 隔离级别
在SQL标准中定义了四种隔离级别,每一种级别都规定了一个事务中所做的修改,哪些在事务内和事务间是可见的,哪些是不可见的.较低级别的隔离通常可以执行更高的并发,系统的开销也更低. 简单的介绍四种隔离级别 ...
- MySQL8.0.12安装及配置
一.下载 下载页面http://dev.mysql.com/downloads/mysql/ 选择系统平台后,点击download(根据系统选择64或32位) 二.配置 1.下载成功后,解压安装包到要 ...
- 【Linux】开放指定端口设置
这里以开放tomcat的8080端口为例 1.开放Linux的8080端口 vi /etc/sysconfig/iptables 进入编辑页面,在指定位置新增以下配置 -A INPUT -m stat ...
- DC 课程内容
- 【nginx】 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream
2013/10/22 20:05:49 [error] 12691#0: *6 FastCGI sent in stderr: "Primary script unknown" w ...
- Python基础——字典(dict)
由键-值对构建的集合. 创建 dic1={} type(dic1) dic2=dict() type(dic2) 初始化 dic2={'hello':123,'world':456,'python': ...