编辑代码

#include <iostream>
using namespace std;
int main() {
    //JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 
    int n = 5,p=4;
    int a[n] = {1,2,3,4,5};
    int k = n-p,j=0,c;//左移p个等于右移n-p个;j用来计算每次右移的位置;
    int m=a[l];//c和m用来做交换的中间变量
    for(int i = 0;i<n;i++){
        cout<<"j=="<<j;
        j = (j+1+k);
        if(j>5) j=j%n;  //这个地方如果直接取余那么当j=n时就会错误
        j = j-1;

        c = a[j];//c保存要被替换位置的值
        a[j] = m;//m保存上一次被替换位置的值;
        m = c;
    }
    cout<<endl;
    for(int i=0;i<n;i++)
        cout<<a[i]<<endl;
	cout << "Hello world!    - cpp.jsrun.net." << endl;
	return 0;
}