PAT (Basic Level) Practice (中文)1008 数组元素循环右移问题 (20 分)

本文提供了一种使用C++实现数组元素循环右移的方法,通过一个实例展示了如何将数组中的元素向右移动m位,涉及到的主要步骤包括读取数组元素、计算实际移动次数、重新分配元素位置以及输出结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805316250615808

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <string>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <stack>
 7 using namespace std;
 8 int n,m;
 9 int a[105];
10 int b[105];
11 int main()
12 {
13        while(cin>>n>>m){
14            for(int i=0;i<n;i++) cin>>a[i];
15            m%=n;
16            for(int i=0;i<m;i++){
17                b[i]=a[n-m+i];
18                //cout<<b[i]<<" ";
19            }
20            for(int i=m;i<n;i++){
21                b[i]=a[i-m];
22                //cout<<b[i]<<" ";
23            }
24            for(int i=0;i<n;i++){
25                if(i==0) cout<<b[i];
26                else cout<<" "<<b[i];
27            } 
28            cout<<endl;
29        }
30     return 0;
31 }

 

转载于:https://www.cnblogs.com/shixinzei/p/10770862.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值