Experiment 7-2-7 Circular right shift of square matrix (20 points)

Experiment 7-2-7 Circular right shift of square matrix (20 points)
This problem requires writing a program to cyclically shift each element in a given n×n square matrix m positions to the right, that is, transform the 0th, 1st, ···, n−1th columns into n−m, n−m+1th columns , ···, n−1, 0, 1, ···, n−m−1 columns.

Input format:
The first line of input gives two positive integers m and n (1≤n≤6). Next, there are a total of n lines, each with n integers, representing a square matrix of order n.

Output format:
Output the shifted square matrix according to the input format: that is, output n lines, each line contains n integers, and output a space after each integer.

Input sample:
2 3
1 2 3
4 5 6
7 8 9

Sample output:
2 3 1
5 6 4
8 9 7

#include <stdio.h>
//Time: April 23, 2018 20:08:02
//Thinking: Through thinking, it is found that the loop output is mainly the control problem of the subscript of the array, as long as the subscript of the column can be controlled, it can be realized
// Print out the two-dimensional array after the circular right shift. It is precisely because of the circular right shift that the value m needs to be controlled between 0 and n-1.
intmain()
{
	int a[10][10];
	int i, j, n, m;
	scanf("%d%d",&m,&n);
	for (i = 0; i<n; i++)
	{
		for (j = 0; j<n; j++)
		{
			scanf("%d", &a[i][j]);
		}
	}
	m %= n; //Make the value of m less than n, clamped in 0 to n-1, to facilitate the control of the following subscripts.
	for (i = 0; i<n; i++)
	{
		for (j = 0; j<n; j++)
		{
			printf("%d ", a[i][(n - m + j) % n]); // loop output subscript control a[i][(n-m+j) % n]
		}
		printf("\n");
	}
	return 0;
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326711382&siteId=291194637