dd爱旋转—牛客小白月赛34
题目链接:https://ac.nowcoder.com/acm/contest/11211/E
输入描述:
读入n阶的矩阵,对于矩阵有如下两种操作
1顺时针旋180°
2关于行镜像
给出q个操作,输出操作完的矩阵
第一行一个数n(1≤n≤1000),表示矩阵大小
接下来n行,每行n个数,描述矩阵,其中数字范围为[1,2000]
一下来一行一个数q(1≤q≤100000),表示询问次数
接下来q行,每行一个数x(x=1或x=2),描述每次询问
输出描述:
n行,每行n个数,描述操作后的矩阵
输入
2
1 2
3 4
1
1
输出
4 3
2 1
输入
2
1 2
3 4
1
2
输出
3 4
1 2
#include#include using namespace std; int ans[1005][1005]; //int str[1005][1005]; int main() { std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> ans[i][j]; } } int q; cin >> q; int flag = 1; while (q--) { int x; cin >> x; if (flag == 1 && x == 1) { flag = 2; } else if (flag == 1 && x == 2) { flag = 3; } else if (flag == 2 && x == 1) { flag = 1; } else if (flag == 2 && x == 2) { flag = 4; } else if (flag == 3 && x == 1) { flag = 4; } else if (flag == 3 && x == 2) { flag = 1; } else if (flag == 4 && x == 1) { flag = 3; } else if (flag == 4 && x == 2) { flag = 2; } } if (flag == 1) { for (int i = 0; i < n; i++) { for (int j = 0; j < n - 1; j++) { cout << ans[i][j] << " "; } cout << ans[i][n-1] << endl; } } else if (flag == 2) { for (int i = n - 1; i >= 0; i--) { for (int j = n - 1; j > 0; j--) { cout << ans[i][j] << " "; } cout << ans[i][0] << endl; } } else if (flag == 3) { for (int i = n - 1; i >= 0; i--) { for (int j = 0; j < n - 1; j++) { cout << ans[i][j] << " "; } cout << ans[i][n-1] << endl; } } else if (flag == 4) { for (int i = 0; i < n; i++) { for (int j = n - 1; j > 0; j--) { cout << ans[i][j] << " "; } cout << ans[i][0] << endl; } } return 0; }