实验7-2-7 方阵循环右移 (20 分)
1. 题目摘自
https://pintia.cn/problem-sets/13/problems/518
2. 题目内容
本题要求编写程序,将给定n×n方阵中的每个元素循环向右移m个位置,即将第0、1、⋯、n−1列变换为第n−m、n−m+1、⋯、n−1、0、1、⋯、n−m−1列。
输入格式:
输入第一行给出两个正整数m和n(1≤n≤6)。接下来一共n行,每行n个整数,表示一个n阶的方阵。
输出格式:
按照输入格式输出移动后的方阵:即输出n行,每行n个整数,每个整数后输出一个空格。
输入样例:
2 3
1 2 3
4 5 6
7 8 9
输出样例:
2 3 1
5 6 4
8 9 7
3. 源码参考
#include <iostream>
using namespace std;
int main()
{
int m, n;
int i, j;
int a[30][30], b[30][30];
int t;
cin >> m >> n;
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
cin >> a[i][j];
}
}
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
if(j == 0)
{
cout << a[i][(j + n - m) % n];
}
else
{
cout << " " << a[i][(j + n - m) % n];
}
}
cout << endl;
}
return 0;
}