循环左移

思路:将前P个元素逆置,再将后n-p个元素逆置,最后整体逆置。

#include<iostream>
#include<cstdio>
#define N 50
using namespace std;
void Reverse (int R[],int l,int r)
{
	int i,j;
	int temp;
	for(i=l,j=r;i<j;++i,--j)
	{
		temp=R[i];
		R[i]=R[j];
		R[j]=temp;
		printf("%d~~%d\n",R[i],R[j]);
	}
	printf("~~~~~~~~~\n");
}
void RCR(int R[],int n,int p)
{
	if (p<=0||p>=n)
		cout<<"ERROR"<<endl;
	else
	{
		Reverse(R,0,p-1);
		Reverse(R,p,n-1);
		Reverse(R,0,n-1);
	}
}
int main()
{
	int L,i;
	int R[N],n;
	cin>>L;
	cin>>n;
	for(i=0;i<=n-1;++i)
	{
		cin>>R[i];
	}
	RCR(R,n,L);
	for(i=0;i<=n-1;++i)
		cout<<R[i]<<" ";
	cout<<endl;
	return 0;
}


注意:不要把l当做1,找了半天才找到。

发布了74 篇原创文章 · 获赞 27 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_37507976/article/details/89416701
今日推荐