九度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 ...
随机推荐
- SharePoint 2016 功能比较
SharePoint 2016中有很多功能.我们经常和客户谈论SharePoint安装时,我问他们是否计划安装SharePoint Server 2016 Standard或Enterprise.通常 ...
- Jquery库插件大全(工作中遇到总结)
Jquery UI所有插件下载:http://jqueryui.com/download/all/ Jquery layer灯箱等演示与帮助:http://sentsin.com/jquery/lay ...
- Kafka 完全分布式集群环境搭建
思路: 先在主机s1上安装配置,然后远程复制到其它两台主机s2.s3上, 并分别修改配置文件server.properties中的broker.id属性. 1. 搭建前准备 示例共三台主机,主机IP映 ...
- java HashMap 内存泄漏
import java.util.HashMap; import java.util.Map; public class HashMapOver { public static void main(S ...
- nfs-ganesha使用
一 nfs-ganesha在centos7上安装 yum -y install centos-release-gluster yum install -y nfs-ganesha.x86_64yum ...
- ajax的序列化表单提交
通过传统的 form 表单提交的方式上传文件 ? 1 2 3 4 <form id="uploadForm" action="" method=" ...
- Bootstrap 缩略图
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- SQL Server 游标的应用
----------------SQL游标应用----------------- 今天由于业务需求,需要在存储过程中实现有一个表的主键去匹配在另一个表中作为外键所对应的数值 ,若在C#中则非常简单只需 ...
- MySQL 5.7.20绿色版安装详细图文教程
MySQL 5.7.20绿色版安装详细图文教程 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle旗下产品.这篇文章主要介绍了MySQL 5.7.20绿色版安装 ...
- 将unity3d项目嵌入到Android App中使用
创建一个新的AndroidStudio app项目. 1.添加库文件:拷贝unity安装目录下的库文件:Unity\Editor\Data\PlaybackEngines\AndroidPlayer\ ...