Basic Level 1008. 数组元素循环右移问题 (20)
生活随笔
收集整理的這篇文章主要介紹了
Basic Level 1008. 数组元素循环右移问题 (20)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一個數組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 C++ #include <functional> #include <algorithm> #include <cstdio> #include <vector> using namespace std;int main() {int N,M;int arr[101];scanf("%d %d",&N,&M);for(int i=0;i<N;++i)scanf("%d",&arr[i]);M = M % N;for(int i=N-M;i<N;++i)printf("%d ",arr[i]);for(int i=0;i<N-M;++i)printf("%d%c",arr[i],i==N-M-1?'\n':' ');return 0; }Python num = [int(x) for x in raw_input().split()][1] arr = raw_input().split() num = num % len(arr) print " ".join(arr[len(arr)-num:]+arr[:len(arr) - num])
轉載于:https://www.cnblogs.com/madao1024/p/4044372.html
總結
以上是生活随笔為你收集整理的Basic Level 1008. 数组元素循环右移问题 (20)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP学习笔记1.2——预定义变量参考
- 下一篇: Ubuntu xrdp 遠端桌面連線