本题要求实现一个对数组进行循环左移的简单函数:一个数组
a中存有n(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向左移m(≥0)个位置,即将a中的数据由(a0a1⋯an−1)变换为(am⋯an−1a0a1⋯am−1)(最前面的m个数循环移至最后面的m个位置)。如果还需要考虑程序移动数据的次数尽量少,要如何设计移动的方法?
输入格式:
输入第1行给出正整数n(≤100)和整数m(≥0);第2行给出n个整数,其间以空格分隔。
输出格式:
在一行中输出循环左移m位以后的整数序列,之间用空格分隔,序列结尾不能有多余空格。
输入样例:
8 3
1 2 3 4 5 6 7 8
输出样例:
4 5 6 7 8 1 2 3
解题思路:m可能大于n,m需要对n进行取余,不然m太大时模拟会超时
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std;
#define LL long long
const int INF=0x3f3f3f3f;
int n,k;
int a[200];
int main()
{
while(~scanf("%d %d",&n,&k))
{
for(int i=0;i<n;i++) scanf("%d",&a[i]);
k=k%n;
printf("%d",a[k]);
for(int i=k+1;i<n;i++) printf(" %d",a[i]);
for(int i=0;i<=k-1;i++) printf(" %d",a[i]);
printf("\n");
}
return 0;
}