"Topic Connection"
An array A contains N (n>0) integers, and each integer loop is shifted to the right by the M (m>=0) position, and the data from a (a0a1 ...) is removed by the use of a different array. AN-1) transform to (an-m ... AN-1 A0 A1 ... AN-M-1) (the last m-number loop moves to the first m-position). If you need to consider the number of times a program moves data, how do you design a moving method?
input Format: Each input contains a test case, line 1th input n (1<=n<=100), M (m>=0), and line 2nd enter n integers, separated by a space.
output format: the sequence of integers after the output loop is shifted to the right of the M-bit in a row, separated by a space, with no extra spaces at the end of the sequence.
Input Sample:
6 21 2 3 4 5 6
Sample output:
5 6 1 2 3 4
Submit Code:
1#include <stdio.h>2#include <stdlib.h>3 4 voidShift_for_1 (intA[],intsize)5 {6 inti, TMP;7 8TMP = a[size-1];9 Ten for(i = size-2; I >=0; i--) One { Aa[i+1] =A[i]; - } -a[0] =tmp; the } - - voidPrint_array (intA[],intsize) - { + inti; - + for(i =0; i < size; i++) A { atprintf (i = = size-1?"%d":"%d", A[i]); - } - } - - intMainvoid) - { in intarray[ -]; - intI, N, M; to +scanf"%d%d", &n, &M); - for(i =0; i < N; i++) thescanf"%d", &array[i]); * $ for(i =0; i < M; i++)Panax Notoginseng { - shift_for_1 (array, N); the } + A Print_array (array, N); the + return 0; -}
PAT (Basic level) practise:1008. Loop right shift problem for array elements