c# 方阵循环右移

本题要求编写程序,将给定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;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值