当前位置:网站首页>PTA Exercise 2.2 Rotate an Array Left
PTA Exercise 2.2 Rotate an Array Left
2022-08-10 08:29:00 【Ge Shi steamed buns】
题目
本题要求实现一个对数组进行循环左移的简单函数:一个数组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、Circular queue method
Use the idea of a circular queue,Can be made simply
#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、reversal
The first half and the second half are reversed first,Then reverse the entire array.
#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;
}

边栏推荐
- Compilation failure:找不到符号
- UGUI - Events, iTween Plugin
- J9数字论:Web3.0+互联网电商会引起怎样的火花?
- 刷题工具h
- 解决win10win7win8系统找不到指定的模块,注册不了大漠插件的问题
- 【OAuth2】十九、OpenID Connect 动态客户端注册
- 菜鸟、小白在autojs和冰狐智能辅助之间如何选择?
- 问下cdc mysql to doris.不显示具体行数,怎么办?
- JS reduce
- Based on STC8G2K64S4 single-chip microcomputer to display analog photosensitive analog value through OLED screen
猜你喜欢
随机推荐
Rust学习:6.5_复合类型之数组
初使jest 单元测试
如何远程调试对方的H5页面
模糊查询除了like+ % 还能用什么方式
90.(cesium之家)cesium高度监听事件
Relaxation class: the boss will martial arts, who also can not hold up against!The charm of six sigma training
每日一题,二叉树中增加一行
【微信小程序】一文读懂页面导航
UGUI—事件,iTween插件
Pieces of TensorFlow 2.9 (1)
阿里云数据库 RDS SQL Server 版的服务器绑定域名www.cxsdkt.cn.的呢?
【FAQ】【Push Kit】推送服务,回执配置一直报错、回执过期修改、怎么删除配置的回执
高性能短连接设计
组合数模板
怎么使用【jmeter正则表达式提取器】解决返回值作参数的问题
IDLE development wordCount program (5)
快速输入当前日期与时间
【Unity入门计划】2D游戏实现敌人来回移动控制脚本
nrm 使用详解
90. (cesium house) cesium height monitoring events









