九度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 ...
随机推荐
- HTTP错误码汇总(转)
响应码由三位十进制数字组成,它们出现在由HTTP服务器发送的响应的第一行.响应码分五种类型,由它们的第一位数字表示:1.1xx:信息,请求收到,继续处理2.2xx:成功,行为被成功地接受.理解和采纳3 ...
- URAL 1057 Amount of Degrees (数位DP,入门)
题意: 求给定区间[X,Y]中满足下列条件的整数个数:这个数恰好等于K个互不相等的,B的整数次幂之和.例如,设X=15,Y=20,K=2,B=2,则有且仅有下列三个数满足了要求: 17 = 24+2 ...
- 【Python图像特征的音乐序列生成】关于数据集的分享和样例数据
数据集还在制作中,样例数据如下: 我将一条数据作为一行,X是ID,O代表了情感向量,S是速度,是一个很关键的参数,K是调式,M是节拍,L是基本拍.后面是ABC格式的序列,通过embedding化这些音 ...
- 《spss统计分析与行业应用案例详解》:实例十二 卡方检验
卡方检验的功能与意义 SPSS的卡方检验是非参数检验方法的一种,其基本功能足通过样本的 频数分布来推断总体是否服从某种理论分布或某种假设分布,这种检验过程是通过分析实际的频数与理论的频数之间的差别或是 ...
- mysql数据库操作手册
1 存储过程的写法 以下是一个带有入参的存储过程模板, #删除方案-存储过程 CREATE PROCEDURE procPersonAppointRecallPlanByPlanUuidDelet ...
- python 基础之while无限循环
用户登录程序 username = "chenxi" passwed = "testki" counter = 0 while counter < 3: ...
- mysql 速度检索
授权GRANT ALL PRIVILEGES ON zabbix.* TO 'zabbix'@'127.0.0.1' IDENTIFIED BY 'zabbixpwd' WITH GRANT OPTI ...
- 重新postgresql出现错误:Problem running post-install step. Installation may not complete correctly. The database cluster initialisation failed.
以前正常使用的postgresql,今天出现问题:报*.dll错误.百度了一下,只能重新安装 . 在重新安装过程中报:Problem running post-install step. Instal ...
- Object-C知识点 (六) 开发中的技巧
本文主要介绍开发中的一些实用技巧 #pragma mark - 代码控制Home按键 [[UIApplication sharedApplication] performSelector:@selec ...
- Factorialize a Number-freecodecamp算法题目
Factorialize a Number(计算一个整数的阶乘) 要求 给定一个整数,求其阶乘(用字母n来代表一个整数,阶乘代表着所有小于或等于n的整数的乘积) 思路 确定乘的次数 用for循环进行累 ...