array element loop right Shift

Source: Internet
Author: User
Tags gcd

include<stdio.h> #include <process.h> #pragma warning (disable:4996) #include "gcd.h" #include "Reverse.h" void ShiftRight1 (int m,int *a,int n) {int _i;int k;int nxt;int tmp;if (m = = 0) return;if (n >= m) _i = GCD (n, m); else_i = gcd (M, n); for (int i = 0; i < _i; i++) {k = i;            The start of the jump write inxt = a[k];       To jump write element do {k = (k + m)% n;tmp = A[k];         The bit element is staged before being written to a[k] = NXT;        NXT = tmp;          } while (k! = i);}} void ShiftRight2 (int m,int *a,int n) {m%= n;   M is a valid shift quantity if (M = = 0) return; Reverse (A, n);      Array inverse reverse (a, m);      The right loop shift number is divided into reverse order reverse (A + M, n-m);} int main () {int n;int m;int *a;int _i;scanf ("%d%d", &n, &m), a = new Int[n];for (int i = 0; i<n; i++) {scanf ("% D ", &a[i]);} ShiftRight1 (M, A, n); ShiftRight2 (M, A, N), for (int i = 0; i<n; i++) {printf ("%d", A[i]);} printf ("\ n"); system ("Pause");}

array element loop right Shift

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.