本题要求编写程序,将给定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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string s = Console.ReadLine();
string[] a = s.Split(' ');
int m = Convert.ToInt32(a[0]);
int n = Convert.ToInt32(a[1]);
int[,] b = new int[n, n];//定义数组b。
int[,] c = new int[n, n];//定义数组c。
for(int i = 0; i < n; i++)
{
string x = Console.ReadLine();
string[] d = x.Split(' ');
for(int j = 0; j < n; j++)
{
b[i, j] = Convert.ToInt32(d[j]);
}
}
for(int i = 0; i < n; i++)
{
int temp;
for(int j = 0; j < n; j++)
{
temp = b[i, j];//定义随机变量temp.
if (j + m < n)
{
c[i, j + m] = temp;
}
else
{
c[i, j + m - n] = temp;
}
}
}
for(int i= 0; i< n; i++)
{
for(int j = 0; j< n; j++)
{
Console.WriteLine( "{0}",c[i, j]);
}
Console.WriteLine();
}
}
}
}//将数组每列的元素循环右移m个位置也就是把每个元素的列坐标加上m即为这个元素的新的列坐标但如过其原列坐标加上m后的结果如果大于n-1那么它的新的列坐标应为原列坐标加m后减去n;