PAT 1008数组元素循环右移
来源:互联网 发布:软件部署实施方案 编辑:程序博客网 时间:2024/06/12 08:23
一个数组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
分析:
题目虽说不允许使用另外的数组,但是我考虑到简便性还是用了...为了写这篇博客,我还是学习了其他人没有使用数组的方法,当然方法有很多,这里说一种.
这种方法可以在草稿纸上推演一下.
源代码:
#include<iostream>
using namespace std;
void reverse(int* list,int start,int last)
{
int temp;
for(int i=start,j=last;i<j;i++,j--)
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}
int main()
{
int N,M;
cin>>N>>M;
int* list=new int[N];
for(int i=0;i<N;i++)
cin>>list[i];
M=M%N;
reverse(list,0,N-M-1);
reverse(list,N-M,N-1);
reverse(list,0,N-1);
for(int i=0;i<N-1;i++)
cout<<list[i]<<" ";
cout<<list[N-1]<<endl;
return 0;
}
- PAT 1008数组元素循环右移
- PAT数组元素循环右移问题
- pat数组元素循环右移问题
- PAT basic 1008 : 数组元素循环右移问题
- PAT 1008 数组元素循环右移问题
- PAT 1008 数组元素循环右移问题
- 1008 数组元素循环右移问题 PAT
- PAT-1008 数组元素循环右移问题
- PAT乙级1008: 数组元素循环右移问题 (20)
- PAT(basic level) 1008 数组元素循环右移问题
- PAT 1008 数组元素循环右移问题
- PAT乙级1008 数组元素循环右移问题
- PAT 1008 数组元素循环右移问题
- PAT——1008数组元素循环右移问题
- PAT---1008数组元素循环右移问题
- PAT 1008 数组元素循环右移问题
- PAT 1008. 数组元素循环右移问题 (20)
- PAT - 1008. 数组元素循环右移问题 (20)
- popen函数
- html_day04_字符实体
- [UVa1626]括号序列
- 利用边框制作三角形
- python和java中斜杠的写法及区分
- PAT 1008数组元素循环右移
- raspberry pi 2安装rtl8811/ 8821网卡驱动
- 极乐小程序榜单(第三期)
- LeetCode 232 Implement Queue using Stacks(利用栈实现队列)
- 编程实现删除字符串中所有指定的字符
- CCF201312-2 ISBN号码
- 两张图片合成
- char,short ,int ,long,long long,unsigned long long数据范围
- MTK GPIO操作