当前位置:网站首页>PTA 习题2.2 数组循环左移
PTA 习题2.2 数组循环左移
2022-08-10 08:24:00 【盖世馒头】
题目
本题要求实现一个对数组进行循环左移的简单函数:一个数组a中存有n(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向左移m(≥0)个位置,即将a中的数据由(a0a1…an-1)变换为(am…an-1a0a1…am-1)(最前面的m个数循环移至最后面的m个位置)。如果还需要考虑程序移动数据的次数尽量少,要如何设计移动的方法?
输入格式:
输入第1行给出正整数n(≤100)和整数m(≥0);第2行给出n个整数,其间以空格分隔。
输出格式:
在一行中输出循环左移m位以后的整数序列,之间用空格分隔,序列结尾不能有多余空格。
输入样例:
8 3
1 2 3 4 5 6 7 8
输出样例:
4 5 6 7 8 1 2 3
1、循环队列法
利用循环队列的思想,可以简单地做出来
#include<stdio.h>
int main(){
int front=0,rear=0,size;
scanf("%d%d",&size,&front);
int arr[size];
rear=front-1;
for(int i=0;i<size;i++)
scanf("%d",&arr[i]);
for(int i=front;i<front+size;i++){
if(i==front)
printf("%d",arr[i%size]);
else
printf(" %d",arr[i%size]);
}
return 0;
}

2、逆转法
分别先对前半部分和后半部分进行逆转,然后再对整个数组进行逆转。
#include<stdio.h>
void reverse(int arr[],int start,int end){
while(start<end){
int temp=arr[start];
arr[start]=arr[end];
arr[end]=temp;
start++;
end--;
}
}
int main(){
int size,movements;
scanf("%d%d",&size,&movements);
movements=movements%size;
int arr[size];
for(int i=0;i<size;i++)
scanf("%d",&arr[i]);
reverse(arr,0,movements-1);
reverse(arr,movements,size-1);
reverse(arr,0,size-1);
for(int i=0;i<size;i++){
printf("%d",arr[i]);
if(i<size-1)
printf(" ");
}
return 0;
}

边栏推荐
- day16--抓包工具Charles的使用
- ABAP Data Types 和XSD Type 映射关系以及XSD Type属性
- VS2013-调试汇编代码-生成asm文件-结构体内存布局-函数参数压栈-调用约定
- Guys, may I ask, the oraclecdc error report is not serialized, but I see that the source code does not inherit serialization, what is the reason?
- In the SQL SERVER database, if the data of the table is added, deleted, or modified, will the index of the table be recorded in the ldf log?
- If the data of the oracle business table is added, deleted, or modified, will the index of the table write redo and undo?
- 问下cdc mysql to doris.不显示具体行数,怎么办?
- 一文2600字手把手教你编写性能测试用例
- 解决win10win7win8系统找不到指定的模块,注册不了大漠插件的问题
- Rust learning: 6.4_ enumeration of composite types
猜你喜欢

VS2013-debug assembly code-generate asm file-structure memory layout-function parameter stack-calling convention

【NeRF】原始论文解读

关于数据中心的设计方案,数据中心网络规划设计

NPU架构与算力分析

DGIOT supports industrial equipment rental and remote control

高性能短连接设计

一文2600字手把手教你编写性能测试用例

【Unity入门计划】制作RubyAdventure03-使用碰撞体&触发器实现世界交互

UGUI—事件,iTween插件

张驰课堂:老板会武术,谁也挡不住!六西格玛培训的魅力
随机推荐
MySQL的用户临时表与内部临时表
编程老手如何在autojs和冰狐智能辅助之间选择?
探索神经网络架构教程视频,设计神经网络的步骤
进程管理(动态的)
uni 小程序腾讯地图polygon背景透明度
Ask next CDC mysql to Doris. Don't show the specific number of lines, how to do?
The implementation of the seemingly useless component (text gradient) in NaiveUI is so simple
明明加了唯一索引,为什么还是产生重复数据?
Introduction to the C language to realize bubble sort
自动化测试框架搭建 ---- 标记性能较差用例
phpstudy starts automatically
34. Talk about why you want to split the database?What methods are there?
本地生活商家如何通过短视频赛道,提升销量曝光量?
Using the color picker
Pieces of TensorFlow 2.9 (1)
Go-Excelize API source code reading (11) - GetActiveSheetIndex()
Obtain - 65 [chances] : "soldiers, subtlety also - 7-36 meter reading - defeat
js函数聚合的三种实现方式
Rust learning: 6.1_Slices of composite types
Class Notes (7) (1) - #647. Find the root and the child (root)