1#include <iostream>2 using namespacestd;3 #defineSwap (b) {a^= B; b^=a; a^=b;}//You can also define macros like this4 5 voidRightShift (int*array,intNintM)6 {7 intI, J;8 if(m>0&& m!=n)//M==0 | | Do not cycle right when m==n9 {Ten for(i=0, j=n-1; i<j; i++, j-- ) One swap (array[i],array[j]); A - for(i=0, j=m-1; i<j; i++, j-- ) - swap (array[i],array[j]); the - for(I=m, j=n-1; i<j; i++, j-- ) - swap (array[i],array[j]); - } + } - + intMain () A { at intN, M; - while(cin>>n>>M) - { - //if (n>100| | n<1| | M<0) continue; -m%=N; - int*A; inA =New int[N]; Don't forget the array space - for(intI=0; i<n; i++ ) toCin>>A[i]; + RightShift (A, N, M); - for(intI=0; i<n-1; i++) thecout<<a[i]<<' '; *cout<<a[n-1]<<Endl;; $ }Panax Notoginseng return 0; -}
1008. Array element Loop right shift problem (20): macro definition new related knowledge consolidation