14046: 【原4046】矩阵旋转
题目
题目描述
author: 程序设计思想与方法助教组黄海鑫 原OJ链接:https://acm.sjtu.edu.cn/OnlineJudge-old/problem/4046
问题描述
编写一个程序,读入一个矩阵,输出该矩阵以第一行第一列数字为中心,顺时针旋转90度后的新矩阵,例如:
输入的矩阵为:
1 2 3
4 5 6
顺时针旋转90度后输出的矩阵为:
4 1
5 2
6 3
输入输出描述
输入
- 输入的第一行为两个正整数m和n,指定输入矩阵的行数和列数(0<m<=10,0<n<=10)
- 剩下的输入为矩阵的内容,共m行,每行n个整数
- 数据之间用空格分隔
输出
- 输出的第一行为两个正整数m和n,为输出的新矩阵的行数和列数
- 剩下的输出为新矩阵的内容,共m行,每行n个整数
- 数据之间用空格分隔
- 每行数据的最后无空格
- 最后的一行输出后面无换行符
程序运行示例1
Sample Input 1
2 3
1 2 3
4 5 6
Sample Output 1
3 2
4 1
5 2
6 3
程序运行示例2
Sample Input 2
1 5
1 3 5 7 9
Sample Output 2
5 1
1
3
5
7
9
注意
- 不要显示多余的提示信息,避免输出判定错误。
- 注意判断输出信息是否符合要求。
FineArtz's solution
/* 矩阵旋转 */
#include <iostream>
using namespace std;
int main(){
int m, n;
int a[15][15];
cin >> m >> n;
for (int i = 1; i <= m; ++i)
for (int j = 1; j <= n; ++j)
cin >> a[i][j];
cout << n << ' ' << m << endl;
for (int i = 1; i <= n; ++i){
for (int j = m; j >= 1; --j)
cout << a[j][i] << ' ';
cout << endl;
}
return 0;
}
victrid's solution
#include <iostream>
using namespace std;
int main()
{
int m, n;
int MAT[10][10]={0};
bool ENDL_FLAG = false, SPACE_FLAG = false;
cin >> m >> n;
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < n; ++j)
{
cin >> MAT[i][j];
}
}
cout << n << ' ' << m << endl;
for (int j = 0; j < n; j++)
{
if (ENDL_FLAG)
cout << endl;
for (int i =(m-1); i >=0; i--)
{
if (SPACE_FLAG)
cout << ' ';
cout << MAT[i][j];
SPACE_FLAG = true;
}
ENDL_FLAG = true;
SPACE_FLAG = false;
}
return 0;
}