Love丶FFC's Blog

PAT乙级:1008 数组元素循环右移问题

2020-01-30 11:20:35
阅读:528   •   评论:5
标签:,

一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置,即将A中的数据由(A​0​​A​1​​⋯AN−1​​)变换为(ANM​​⋯AN−1​​A​0​​A​1​​⋯ANM−1​​)(最后M个数循环移至最前面的M个位置)。如果需要考虑程序移动数据的次数尽量少,要如何设计移动的方法?

输入格式:

每个输入包含一个测试用例,第1行输入N(1≤N≤100)和M(≥0);第2行输入N个整数,之间用空格分隔。

输出格式:

在一行中输出循环右移M位以后的整数序列,之间用空格分隔,序列结尾不能有多余空格。

输入样例:

6 2
1 2 3 4 5 6

输出样例:

5 6 1 2 3 4

编程语言:Python

解题思想:

1.使用Python中列表截取的方法

新列表=原列表末尾+原列表开头

例如NON=6,MoveDistance=2,则先截取Number[6-2:](索引4,5的元素)

再截取Number[:6-2])(索引0,1,2,3的元素)

时间复杂度:O(n)

代码如下:

  1. NON, MoveDistance = map(int, input().split())
  2. Number = list(map(int, input().split()))
  3. Number = Number[NON - MoveDistance:] + Number[:NON - MoveDistance]
  4. # 按要求格式输出
  5. for i in range(NON - 1):
  6. print(Number[i], end=" ")
  7. print(Number[NON - 1])

评论板

共有 5 条评论

  1. fruinly

    If you want to change the Cockatiels staple diet e nolvadex for sale If the Qing Dynasty wants to build a space station, it can only belong to magic

  2. FoorryNox

    PMID 3756855 buy cialis canada pharmacy The glucometer measures glucose levels in a sample of your blood dabbed on a strip of treated paper

  3. cheap louis vuitton handbags

    Read reviews and was a little hesitant since I had already inputted my order. otherwise but thank god, I had no issues. for instance received item in a timely matter, they are in new condition. anyway so happy I made the purchase. Will be definitely be purchasing again.
    cheap louis vuitton handbags https://www.louisvuittonsoutletstore.com/

  4. louis vuitton outlet sale online

    Read reviews and was a little hesitant since I had already inputted my order. or perhaps even but thank god, I had no issues. similar to received item in a timely matter, they are in new condition. in any event so happy I made the purchase. Will be definitely be purchasing again.
    louis vuitton outlet sale online https://www.louisvuittonsoutletonline.com/

  5. Preliaf

    Gene expression signatures have helped us understand the inter tumor heterogeneity between breast cancer tumors 6, 7, separating tumors into subgroups with different underlying biology, prognosis, and treatment benefit 8, 9, 10 buy cialis online no prescription

--------查看该分类下最新文章--------
^
新版博客正在完善中!域名:http://www.loveffc:8080,点击跳转,完全移植后将去除端口号。

Copyright © 2018 - 2021 FFC的小站 - 滇 ICP 备 18010780 号 - 1

- Powered by WordPress & AliYun · Theme by FFC -

- Environment by Windows & XAMPP · Designed by WebStorm & VSCode -

已运行:

访问量:491224