【PAT】1008. 数组元素循环右移问题 (20)
1008. 数组元素循环右移问题 (20)
一个数组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 2
1 2 3 4 5 6
输出样例:
5 6 1 2 3 4
方法一:
程序说明:
1.本题直接打印出需要的数组元素即可,不需要进行数组的移位
2.为了防止出现 m>=n 的情况,加入一句 m%=n,增加鲁棒性 C++ 代码如下:
#include <bits/stdc++.h>
using namespace std; int main(){
int m,n,i;
cin>>n>>m;
m %= n;
int num[n];
for(i=;i<n;i++)
cin>>num[i];
for(i=n-m;i<n;i++)
cout<<num[i]<<' ';
for(i=;i<n-m-;i++)
cout<<num[i]<<' ';
cout<<num[n-m-];
system("pause");
}
C++ Code 1
方法二:
使用标准模板库中的vector,定义一个 int 型的容器来存储数字,并不断向后平移 m 次,最后打印即可 C++ 代码如下:
#include <bits/stdc++.h>
using namespace std; int main() {
int n,m,temp,flag=;
vector<int> num;
cin>>n>>m;
m%=n;
for(int i=;i<n;i++){
cin>>temp;
num.push_back(temp);
}
for(int i=;i<m;i++){
temp=num[n-];
for(int j=n-;j>;j--)
num[j]=num[j-];
num[]=temp;
}
for(int i=;i<n;i++){
if(flag==){
cout<<num[i];
flag=;
}
else cout<<' '<<num[i];
}
system("pause");
return ;
}
C++ Code 2
方法三:
使用标准模板库(STL)中的list,将输入的值存入链表中,在进行向后移动时,将最后一个元素的值取出、删除然后放入首元素中,最后打印
list是STL中的双向链表,使用list需添加头文件 #include <list>
函数功能说明:
1.list.begin():返回一个当前list中起始元素的迭代器;
2.list.end():返回一个当前list中末尾元素的迭代器;
3.list.push_back():在list的尾端插入一个数据;
4.list.pop_back():删除list中尾端的数据;
5.list.push_front():在list的首部插入一个数据 C++ 代码如下
#include <bits/stdc++.h>
using namespace std; int main() {
int n,m,temp,flag=;
cin>>n>>m;
m%=n;
list<int> num;
while(n--){
cin>>temp;
num.push_back(temp);
}
while(m--){
temp=num.back();
num.pop_back(); //删除最后一个元素
num.push_front(temp); //将取出的最后一个元素的值放在首位置
}
list<int>::iterator num_itr = num.begin(); //定义一个int型链表的迭代器,并赋值为num链表的首位置
while(num_itr!=num.end()){
if(flag==){
cout<<*num_itr++;
flag=;
}
else cout<<' '<<*num_itr++;
}
system("pause");
return ;
}
C++ Code 3
方法四:
使用 void advance() 函数和 list 中的 void splice() 函数
1.关于advance() 函数:
1)函数功能:加减迭代器,适用于 list/vector/map/deque/stack 等容器中
2)函数原型:
template <class InputIterator, class Distance>
void advance (InputIterator& i, Distance n);
3)参数: i 为迭代器,n 为偏移的位置,n=0,不移动 n>0,迭代器+n,向右移动 n<0,迭代器-n,向左移动
2.关于splice() 函数:
1)函数功能:将一个列表中的元素移除到另一个列表中
2)函数原型及参数
void splice (iterator position, list& x); //将列表x中的所有元素移到当前list中,从当前列表的position指向的位置开始,执行结束后列表x变成空列表 void splice (iterator position, list& x, iterator i); //position是当前列表的迭代器,i是列表x的迭代器
//将列表x中迭代器 i 指向的元素移到当前list的position指向的位置处,
//由于i指向的元素从列表x中被移除,所以迭代器 i 此时是invalid的
void splice (iterator position, list& x, iterator first, iterator last); //position是当前列表的迭代器,first,last是列表x的迭代器,
//将列表x中[first,last)的元素从position指向的位置开始移到当前列表中,
//注意:first指向的元素从列表x中移除,而last指向的元素未被移除
C++ 代码如下:
#include <bits/stdc++.h>
using namespace std; int main(){
list<int> num;
int m,n,temp,count=;
cin>>n>>m;
m%=n;
while(n--){
cin>>temp;
num.push_back(temp);
}
list<int>::iterator it;
it=num.begin();
advance(it,n-m);
num.splice(num.begin(),num,it,num.end());
for(it=num.begin();it!=num.end();it++){
cout<<*it<<(count<num.size()-?' ':'\n');
count++;
}
system("pause");
}
C++ Code 4
【PAT】1008. 数组元素循环右移问题 (20)的更多相关文章
- PAT 1008 数组元素循环右移问题 (20)(代码)
1008 数组元素循环右移问题 (20)(20 分) 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A ...
- PAT 1008. 数组元素循环右移问题 (20)
一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0 A1--AN-1)变换为(AN-M -- AN-1 A0 ...
- PAT乙级 1008. 数组元素循环右移问题 (20)
1008. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数,在不允 ...
- PAT乙级真题1008. 数组元素循环右移问题 (20)
原题: 1008. 数组元素循环右移问题 (20) 时间限制400 ms内存限制65536 kB 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M&g ...
- [C++]PAT乙级1008.数组元素循环右移问题 (20/20)
/* 1008. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数, ...
- PAT 乙级 1008 数组元素循环右移问题 (20) C++版
1008. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数,在不允 ...
- PAT-乙级-1008. 数组元素循环右移问题 (20)
1008. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数,在不允 ...
- PAT (Basic Level) Practise (中文)- 1008. 数组元素循环右移问题 (20)
一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0A1……AN-1)变换为(AN-M …… AN-1 A0 ...
- PAT Basic 1008 数组元素循环右移问题 (20 分)
一个数组A中存有N(>)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥)个位置,即将A中的数据由(A0A1⋯AN−1)变换为(AN−M⋯AN−1A ...
随机推荐
- 图像灰度化方法总结及其VC实现
http://blog.csdn.net/likezhaobin/article/details/6915754 最近一段时间作者开始进行运动目标识别定位系统设计,本文以及后续的几篇文章都是从一个图像 ...
- OpenResty初涉
关于openresty可参考官方文档: http://openresty.org/cn/download.html 1.这个是什么? 在互联网公司,Nginx可以说是标配组件,但是主要场景还是负载均衡 ...
- git相关网址
git入门教程: 廖雪峰的官方网站 https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b00 ...
- extjs grid demo
Ext.onReady(function () { var store = Ext.create('Ext.data.Store', { fields: ['id', 'name', 'account ...
- GridControl GridView 修改表格中的标题居中
Grid Designer>Views>Appearance>HeaderPanel>TextOptions>HAIignment{Center} 依次打开并找到HAIL ...
- opencv 高级拼接函数Stitcher
Stitcher https://docs.opencv.org/trunk/d8/d19/tutorial_stitcher.html http://blog.csdn.net/czl389/art ...
- select多选框
select多选框 效果: 代码: <HTML> <HEAD> <TITLE>选择下拉菜单</TITLE> <meta http-equiv=&q ...
- 【leetcode 简单】 第五十一题 有效电话号码
给定一个包含电话号码列表(一行一个电话号码)的文本文件 file.txt,写一个 bash 脚本输出所有有效的电话号码. 你可以假设一个有效的电话号码必须满足以下两种格式: (xxx) xxx-xxx ...
- 【leetcode 简单】第三题 回文数
判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向 ...
- HDU 1394 Minimum Inversion Number (树状数组)
题目链接 Problem Description The inversion number of a given number sequence a1, a2, ..., an is the numb ...