九度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 ...
随机推荐
- Win10权限问题
通过组策略打开Administrator用户后,Edge.图片查看器等内置程序不能使用,提示“无法使用内置管理员账户打开” 网上的方法: 1.组策略:本地安全策略编辑器——安全设置——本地策略——安全 ...
- asp页面无法访问,可尝试开始SQL Server等服务
存在问题 asp页面的英文提示,翻译后为: "一个错误发生在服务器在处理URL.请联系系统管理员(管理人).如果您是系统管理员,请点击这里了解更多关于这个错误." 解决方案 请 ...
- python爬虫之路——基本文件操作
介绍python如何打开文件和读取数据 新建TXT文档,为追加模式: f=open('c;/wendang/demo.txt','a+') content="abcdefg123456789 ...
- code Gym 100500D T-shirts(暴力)
因为只能买一次,暴力枚举一下买的衣服的大小. #include<cstdio> #include<map> #include<algorithm> using na ...
- 使用ErrorProvider组件验证文本框输入
实现效果: 知识运用: ErrorProvider组件的BlinkStyle属性 //指示错误图标的闪烁时间 public ErrorBlinkStyle BlinkStyle{ get;set; } ...
- Redis五种数据结构解析
Redis是一个开源的Key-Value存储引擎,它支持string.hash.list.set和sorted set等多种值类型.由于其卓越的性能表现.丰富的数据类型及稳定性,广泛用于各种需要k/v ...
- 数组char a[4] 保存了一个数据,怎么转换为unsigned int呢 ?
[待解决问题] 浏览: 701次 注意char并不表示字符的 a[0]=0; a[1]=0; a[2]=3; a[3]=0; 那么我要的unsigned int b应该等于: b= 0x0000030 ...
- C#经典面试题——递归运算
今天开始写递归,然而始终不得甚解.借鉴别人的理解:假设我们现在都不知道什么是递归,我们自然想到打开浏览器,输入到谷歌的网页,我们点击搜索递归,然后我们在为维基百科中了解到了递归的基本定义,在了解到了递 ...
- 201621123080 《Java程序设计》第10周学习总结
201621123080 <Java程序设计>第10周学习总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集异常 ...
- Voyager下的Settings方法
设置网站标题,logo,描述: 自定义setting字段,添加group为文章,key为title的字段: 添加成功: 前端页面写法: <img src="{{ Voyager::im ...