PTA 1008 数组元素循环右移问题

作者: freesan44 | 来源:发表于2021-08-17 07:08 被阅读0次

    题目

    一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置,即将A中的数据由(A
    0

    A
    1

    ⋯A
    N−1

    )变换为(A
    N−M

    ⋯A
    N−1

    A
    0

    A
    1

    ⋯A
    N−M−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
    结尾无空行
    

    解题思路

    str1 = input()
    # str1 = "6 2"
    str2 = input()
    # str2 = "1 2 3 4 5 6"
    list1 = str1.split(" ")
    # 移动步数
    moveStep = int(list1[1])
    # 原始数组
    numList = str2.split(" ")
    
    # print(numList)
    for i in range(moveStep):
        item = numList.pop()
        numList.insert(0, item)
    # 输出结果
    res = " ".join(numList)
    print(res)
    

    相关文章

      网友评论

        本文标题:PTA 1008 数组元素循环右移问题

        本文链接:https://www.haomeiwen.com/subject/hxyebltx.html