您的位置:首页 > 其它

浙大 pat Basic Level 1008

2014-02-28 13:13 239 查看

1008. 数组元素循环右移问题 (20)

一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0 A1……AN-1)变换为(AN-M …… AN-1 A0 A1……AN-M-1)(最后M个数循环移至最前面的M个位置)。如果需要考虑程序移动数据的次数尽量少,要如何设计移动的方法?

输入格式:每个输入包含一个测试用例,第1行输入N ( 1<=N<=100)、M(M>=0);第2行输入N个整数,之间用空格分隔。

输出格式:在一行中输出循环右移M位以后的整数序列,之间用空格分隔,序列结尾不能有多余空格。

输入样例:
6 2
1 2 3 4 5 6

输出样例:
5 6 1 2 3 4


//类似冒泡排序,要求你对冒泡排序有一定理解
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n,m;
cin >> n >> m;
vector<int> num;
for (int i = 0; i != n; i++)
{
int temp;
cin >> temp;
num.push_back(temp);
}
for(int i = 0; i != m; i++)
{
for(vector<int>::size_type j = num.size()-1; j != 0; j--)
{
int temp = num[j];
num[j] = num[j-1];
num[j-1] = temp;
}
}
for(vector<int>::size_type i = 0; i != num.size(); i++)
{
if(i != num.size()-1)
cout << num[i] << ' ';
else
cout << num[i] << endl;
}
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: