Exercise 8-3 Array Loop right shift (20 points)

Source: Internet
Author: User

The subject requires the realization of a simple function that loops the right of an array.A is stored inN>0) Integer to move each integer loop to the rightM≥0) a position that willThe data in a is determined by (A?0??A?1???A?N?1?? ) Transformation to (A?N?M???A?N?1??A?0??A?1???a? n? M? 1?? ) (Last m number of loops moved to the front < span class= "Strut" > M position).

function Interface Definition:
int ArrayShift( int a[], int n, int m );

This is the user-passed array, the size of the array, and the a[] number of bits to n m move right. The function ArrayShift must still exist in the array after the loop is moved right a[] .

Example of a referee test procedure:
#include <stdio.h>#define MAXN 10int ArrayShift( int a[], int n, int m );int main(){    int a[MAXN], n, m;    int i;    scanf("%d %d", &n, &m);    for ( i = 0; i < n; i++ ) scanf("%d", &a[i]);    ArrayShift(a, n, m);    for ( i = 0; i < n; i++ ) {        if (i != 0) printf(" ");        printf("%d", a[i]);    }    printf("\n");    return 0;}/* 你的代码将被嵌在这里 */
Input Sample:
6 21 2 3 4 5 6
Sample output:
5 6 1 2 3 4
#include <stdio.h>#defineMAXN 10intArrayshift (intA[],intNintm);intMain () {intA[MAXN], N, m; inti; scanf ("%d%d", &n, &m);  for(i =0; I < n; i++) scanf ("%d", &A[i]);    Arrayshift (A, n, m);  for(i =0; I < n; i++ ) {        if(I! =0) printf (" "); printf ("%d", A[i]); } printf ("\ n"); return 0;}/*your code will be embedded here.*/intArrayshift (intA[],intNintm) {     while(m--)//Number of moves    {         inttemp = a[n-1];//Keep Last one         for(inti = n-1; I >=1; i--) {A[i]= A[i-1]; } a[0] =temp; }}

Exercise 8-3 Array Loop right shift (20 points)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.