菜鸟教程 -- 学的不仅是技术,更是梦想!

Python 基础教程
(追記) (追記ここまで)

Python 练习实例68

Python 100例 Python 100例

题目:有 n 个整数,使其前面各数顺序向后移 m 个位置,最后 m 个数变成最前面的 m 个数

程序分析:无。

程序源代码:

实例

#!/usr/bin/python# -*- coding: UTF-8 -*-if__name__ == '__main__': n = int(raw_input('整数 n 为:\n'))m = int(raw_input('向后移 m 个位置为:\n'))defmove(array,n,m): array_end = array[n - 1]foriinrange(n - 1,-1,- 1): array[i] = array[i - 1]array[0] = array_endm -= 1ifm > 0:move(array,n,m)number = []foriinrange(n): number.append(int(raw_input('输入一个数字:\n')))print'原始列表:',numbermove(number,n,m)print'移动之后:',number

以上实例输出结果为:

整数 n 为:
8
向后移 m 个位置为:
5
输入一个数字:
2
输入一个数字:
8
输入一个数字:
6
输入一个数字:
1
输入一个数字:
78
输入一个数字:
45
输入一个数字:
34
输入一个数字:
2
原始列表: [2, 8, 6, 1, 78, 45, 34, 2]
移动之后: [1, 78, 45, 34, 2, 2, 8, 6]

Python 100例 Python 100例

AI 思考中...

11 篇笔记 写笔记

  1. #0

    kui

    che***[email protected]

    3

    以下实例提供 7 个数,每个数向后移动3位,最后 3 位数变为最前面的 3 位。

    实例解法一:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    from collections import deque
    m = 3
    a = [1,2,3,4,5,6,7] # 7 个数
    f = deque(a)
    f.rotate(m)
    print list(f)
    

    实例解法二:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    m = 3
    a = [1,2,3,4,5,6,7]
    def rot(a,n):
     l = len(a)
     n = l-n
     return a[n:l]+a[0:n]
    b = rot(a,m)
    print a
    print b
    

    kui

    che***[email protected]

    9年前 (2017年03月31日)
  2. #0

    等一个人

    252***[email protected]

    42

    参考方案:

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    a = [1, 2, 3, 4, 5] # 测试列表
    m = 3 # 设置向后移动 3 位
    for _ in range(m):
     a.insert(0, a.pop())
    print(a)

    等一个人

    252***[email protected]

    9年前 (2017年05月05日)
  3. #0

    朦胧

    253***[email protected]

    3

    参考方法:

    # coding:utf-8
    n=int(input("输入整数1~n:"))
    List=[]
    for i in range(1,n+1):
     List.append(i)
    print("打印1~n:",List)
    print()
    m=int(input("输入要移动的位数:"))
    List2=List[n-m:n+1]+List[0:n-m]
    print("打印移动后的结果:",List2)
    

    朦胧

    253***[email protected]

    9年前 (2017年06月14日)
  4. #0

    helloworld

    hol***[email protected]

    0

    参考方法:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    from random import randint
    data = [randint(1,110) for i in range(20)]
    print(data)
    n = 20
    m = 5
    # data[5:20]= data[0:15] 
    data[5:20], data[0:5] = data[0:15] ,data[15:20]
    print(data)

    helloworld

    hol***[email protected]

    9年前 (2017年06月26日)
  5. #0

    CosmosHua

    cos***[email protected]

    5

    Python3 测试:

    def rLoop(ls, m):
     n = len(ls)
     return ls[n-m:n]+ls[0:n-m]
    ls = [i for i in range(1, 10)]
    print(rLoop(ls, 3))
    #result:
    [7, 8, 9, 1, 2, 3, 4, 5, 6]
    

    CosmosHua

    cos***[email protected]

    9年前 (2017年07月13日)
  6. #0

    sssss

    lij***[email protected]

    0

    参考方法:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    # 设置 9 个数
    n=[1,2,3,4,5,6,7,8,9]
    # 前面各数顺序向后移 5 个位置
    m = 5 
    c=[]
    for i in range(len(n)):
     if i<(len(n)-m): 
     c.append(n[i])
    for i in c:
     n.remove(i)
    n.extend(c)
    print(n)
    

    sssss

    lij***[email protected]

    9年前 (2017年08月01日)
  7. #0

    薄荷可乐

    r49***[email protected]

    3

    参考方法:

    #coding=utf-8
    a=[]
    n=input('您想输入几位数:')
    for i in range(1,n+1):
     s=input('请输入第%d位数:'%i)
     a.append(s)
    m=input('您想将列表移动几位:')
    print '排序前列表:%s'%a
    b=a[-m:] #获取后面的m个数
    c=a[:-m] #获取前面除了m的数
    a=b+c #拼接成新列表
    print '排序后列表:%s'%a
    

    薄荷可乐

    r49***[email protected]

    9年前 (2017年09月18日)
  8. #0

    大大大大大大大熊

    382***[email protected]

    0

    参考方法:

    #coding:utf-8
    #移动位数不应该超过数组长度大小
    a=[]
    b=[]
    n=int(input("几个数:" ))
    m=int(input("移动位数:" ))
    for i in range(n):
     a.append(int(input("请输入第 %d 个数:" %(i+1))))
    b.extend(a*2)
    print(a)
    print(b[len(a)-m:2*len(a)-m])
    

    大大大大大大大熊

    382***[email protected]

    9年前 (2017年09月20日)
  9. #0

    iMax4ever

    jia***@foxmail.com

    2

    Python3:切片

    #coding:utf-8
    n = int(input('整数 n 为:'))
    m = int(input('向后移 m 个位置为:'))
    L = []
    for i in range(n):
     print('请输入第{}个数字:'.format(i+1), end='')
     L.append(int(input('')))
    print('原始列表为:', L)
    print('更新列表为:', L[n-m:] + L[:n-m])

    iMax4ever

    jia***@foxmail.com

    9年前 (2017年10月09日)
  10. #0

    郭扬乔

    944***[email protected]

    4

    参考:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    c = [2, 8, 6, 1, 78, 45, 34, 2]
    for x in range(3):
     c.append(c.pop(0))
    print c

    郭扬乔

    944***[email protected]

    9年前 (2017年11月29日)
  11. #0
    2

    python3 实测通过

    # -*- coding:UTF-8 -*-
    a=[2, 8, 6, 1, 78, 45, 34, 2]
    print(a)
    m=int(input('输入m:'))
    if(m>=len(a)):
     print('error')
    for i in range(m):
     a.insert(0,a.pop())
    print(a)
    9年前 (2017年11月29日)

点我分享笔记

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址

AltStyle によって変換されたページ (->オリジナル) /