[C++]PAT乙级1008.数组元素循环右移问题 (20/20)
/*
1008. 数组元素循环右移问题 (20) 时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard 一个数组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
*/
/*
思路1:
数组长度为N+1;每次移动一位
思路2:
新建缓冲数组;
*/
#include<stdio.h>
using namespace std; //int count=0;//test bool shift(int *array, int length){//length:数组长度;数组真实元素个数:从下标1开始,共计length-1个元素
if(length<1)
return false;//移动失败
//printf("shifting %d.\n", ++count);//test
array[0] = array[length-1];
for(int i=length;i>0;i--){
array[i] = array[i-1];
}
return true;
} void print(int *array, int length){
for(int i=1;i<length;i++){
printf("%d%s", array[i], (i-length+1<0?" ":"\n"));//技巧:对于消去末尾处最后一个空格的巧妙解决办法
}
} int main()
{
int n,shift_distance;
int *array;
scanf("%d %d", &n, &shift_distance);
array = new int[n+1]; // input data
array[0] = 0;
for(int i=1,len = n + 1;i<len;i++){
scanf("%d", array+i);
}
for(int i=0;i<shift_distance;i++){
shift(array, n + 1);
//print(array,n+1);//test
}
print(array,n+1);
return 0;
}
/*
测试用例:
10 4
78 67 56 34 90 671 1 58 37 70 6 2
1 2 3 4 5 6
*/
[C++]PAT乙级1008.数组元素循环右移问题 (20/20)的更多相关文章
- PAT乙级 1008. 数组元素循环右移问题 (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.数组元素循环右移问题 C++/Java
1008 数组元素循环右移问题 (20 分) 题目来源 一个数组A中存有N(>)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥)个位置,即将A中的数据由(A0A1⋯ ...
- PAT 乙级 -- 1008 -- 数组元素循环右移问题
题目简述 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0 A1--AN-1)变换为(AN-M -- AN ...
- 【PAT】1008. 数组元素循环右移问题 (20)
1008. 数组元素循环右移问题 (20) 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0A1……AN- ...
- PAT Basic 1008 数组元素循环右移问题 (20 分)
一个数组A中存有N(>)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥)个位置,即将A中的数据由(A0A1⋯AN−1)变换为(AN−M⋯AN−1A ...
- PAT (Basic Level) Practice 1008 数组元素循环右移问题 分数 20
一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置,即将A中的数据由(A0A1⋯AN−1)变换为(AN−M⋯AN−1A0A1⋯AN ...
- PAT (Basic Level) Practice (中文)1008 数组元素循环右移问题 (20 分)
题目链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805316250615808 #include <iost ...
- PAT乙级真题1008. 数组元素循环右移问题 (20)
原题: 1008. 数组元素循环右移问题 (20) 时间限制400 ms内存限制65536 kB 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M&g ...
随机推荐
- golang go语言通道类型的通道示例 通道的通道
几点注意:go的无缓存通道 通道make 创建后,即使里面是空的,也可以取里面内容.但是程序会被阻塞. 通道的规则是没人取,是不能往里面放的.放的线程会阻塞. 最外层的requestChan相当于一个 ...
- django 分类搜索(根据不同的单选框,改变form提交的地址)
前端html部分form <form id="searchform" action="#" method="get" class=&q ...
- 遍历List过程中同时修改
public static void Main() { List<int> list = new List<int>(); ,,,,,,,,,}; list.AddRange( ...
- ThymeLeaf的eclipse插件安装
“Help”----“Install New Software...” 输入: http://www.thymeleaf.org/eclipse-plugin-update-site/ 一路Next, ...
- update linux dns,no need restart
[root@hc--uatbeta2 ~]# cd /etc[root@hc--uatbeta2 etc]# vi resolv.conf ******* nameserver 10.123.23.*
- (BFS) leetcode 690. Employee Importance
690. Employee Importance Easy 377369FavoriteShare You are given a data structure of employee informa ...
- 解决docker多开mysql报错问题
1.vim /etc/sysctl.conf fs.aio-max-nr=262144 重新加载 sysctl -p /etc/sysctl.conf
- springboot配置jsp
spring.mvc.view.prefix= /WEB-INF/jsp/ spring.mvc.view.suffix= .jsp pom.xml <!--jsp支持--> <!- ...
- CentOS7 yum安装、配置PostgreSQL 9.5
PostgreSQL 9.5安装 1.添加RPM yum install https://download.postgresql.org/pub/repos/yum/9.5/redhat/rhel-7 ...
- flask flash消息
请求完成,让用户知道状态发生了变化,可以使用flash确认消息 示例: xx.py from flask import Flask,render_template,request,redirect,u ...