Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pat乙级1008 数组元素循环右移问题(简洁到极致的写法) #167

Open
1982827148 opened this issue Aug 3, 2022 · 0 comments

Comments

@1982827148
Copy link

1982827148 commented Aug 3, 2022

#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
int main() {
	cin>>n>>m;
	int a[n];
	for(i=0;i<n;i++) cin>>a[i];
	for(i=0;i<n;i++){
		int j=i-m%n;			//通过公式 i->(i+m )%n实现位置的变换,可以逆转换变换后的a[j]=a[i+kn-m] ,
		if(j<0) cout<<a[j+n];	//这里的k为使kn-m小于0时的最小值,如果其绝对值大于i只需k+1即可,所以可转换为i-m%n判断正负即可 
		else cout<<a[j];		//下标之间的逆转换只用一个式子即可表达,就cout<<a[(j>0?j:(j+n))] 
		if(i<n-1)cout<<" ";
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant