编程题目:PAT 1008. 数组元素循环右移问题 (20)
来源:互联网 发布:淘宝装修用什么版本? 编辑:程序博客网 时间:2024/06/10 19:08
1008. 数组元素循环右移问题 (20)
时间限制
400 ms
内存限制
32000 kB
代码长度限制
8000 B
判题程序
Standard
一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0A1……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 21 2 3 4 5 6输出样例:
5 6 1 2 3 4
/*http://pat.zju.edu.cn/contests/pat-b-practise/1008*/#include<iostream>#include<vector>using namespace std;int main(){int num,shift,temp;cin>>num>>shift;vector<int> v;for(int i=0;i<num;i++){cin>>temp;v.push_back(temp);}for(int i=0;i<shift;i++){temp =v[num-1];for(int j=num-1;j>0;j--){v[j]=v[j-1];}v[0]=temp;}for(int i=0;i<num-1;i++){cout<<v[i]<<" ";}cout<<v[num-1];system("pause");return 0;}
//////方法二,利用list#include<iostream>#include<list>using namespace std;void main(){int num,shift;//分别表示元素的个数和移位的位数int tmp;list<int> mylist;cin>>num>>shift;while(num--){cin>>tmp;mylist.push_back(tmp);}while(shift--){tmp = mylist.back();mylist.pop_back();mylist.push_front(tmp);}list<int>::iterator itr = mylist.begin();bool first = true;while(itr!=mylist.end()){if(first){cout<<*itr++;first = false;}elsecout<<" "<<*itr++;}cout<<endl;system("pause");}
1 0
- 编程题目:PAT 1008. 数组元素循环右移问题 (20)
- PAT 1008. 数组元素循环右移问题 (20)
- PAT - 1008. 数组元素循环右移问题 (20)
- PAT Basic 1008. 数组元素循环右移问题 (20)
- PAT(B)1008. 数组元素循环右移问题 (20)
- PAT 1008. 数组元素循环右移问题 (20)
- Java - PAT - 1008. 数组元素循环右移问题 (20)
- PAT-乙级-1008. 数组元素循环右移问题 (20)
- PAT乙级.1008. 数组元素循环右移问题 (20)
- PAT乙级 1008. 数组元素循环右移问题 (20)
- PAT-B 1008. 数组元素循环右移问题 (20)
- PAT-B 1008. 数组元素循环右移问题 (20)
- PAT乙级1008. 数组元素循环右移问题 (20)
- 1008. 数组元素循环右移问题 (20)-PAT乙级
- 1008. 数组元素循环右移问题 (20) PAT
- PAT乙级 1008. 数组元素循环右移问题 (20)
- PAT乙级1008. 数组元素循环右移问题 (20)
- PAT:1008. 数组元素循环右移问题 (20)
- -------------Unable to execute dex: GC overhead limit exceeded
- linux安装oracle11g
- 一名高二辍学生的迷茫
- 动态删除ArrayList中的元素
- 机器学习之正则化与交叉验证
- 编程题目:PAT 1008. 数组元素循环右移问题 (20)
- HDU 4620 - Fruit Ninja Extreme (DFS)
- Android中应用界面主题Theme使用方法和页面定时跳转应用
- PHP模块 Memcached功能多于Memcache
- ACE中的TCP通讯
- 解决VTune错误The Data Cannot be displayed, there is no viewpoint available for data
- 使用GDAL库中的RPC校正问题
- 给商品模型添加搜索设定
- The nature of C++