PAT (Basic Level) Practice (中文)1008 数组元素循环右移问题 (20 分)
生活随笔
收集整理的這篇文章主要介紹了
PAT (Basic Level) Practice (中文)1008 数组元素循环右移问题 (20 分)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目
一個數(shù)組A中存有N(>0)個整數(shù),在不允許使用另外數(shù)組的前提下,將每個整數(shù)循環(huán)向右移M(≥0)個位置,即將A中的數(shù)據(jù)由(A?0?? A?1?? ?A?N?1?? )變換為(A?N?M?? ?A?N?1?? A?0?? A?1?? ?A?N?M?1?? )(最后M個數(shù)循環(huán)移至最前面的M個位置)。如果需要考慮程序移動數(shù)據(jù)的次數(shù)盡量少,要如何設(shè)計(jì)移動的方法?
輸入格式:
每個輸入包含一個測試用例,第1行輸入N(1≤N≤100)和M(≥0);第2行輸入N個整數(shù),之間用空格分隔。
輸出格式:
在一行中輸出循環(huán)右移M位以后的整數(shù)序列,之間用空格分隔,序列結(jié)尾不能有多余空格。
輸入樣例:
6 2
1 2 3 4 5 6
輸出樣例:
5 6 1 2 3 4
C++實(shí)現(xiàn)
#include <iostream> #include <cstring> using namespace std; int main() {int n,m,num[100+7];cin>>n>>m;m%=n;memset(num,0, sizeof(num));for (int i = 0; i < n; ++i) {cin>>num[i];}for (int k = n-m; k < n; ++k) {cout<<num[k]<<' ';}for (int j = 0; j < n-m; ++j) {cout<<num[j];if (j!=n-m-1) cout<<' ';}return 0; }python實(shí)現(xiàn)
a=input().split() b=input().split() n=int(a[0]) m=int(a[1]) right=b[n-m:] left=b[:n-m] num=right+left print(' '.join(num))總結(jié)
以上是生活随笔為你收集整理的PAT (Basic Level) Practice (中文)1008 数组元素循环右移问题 (20 分)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PAT (Basic Level) Pr
- 下一篇: PAT (Basic Level) Pr