Python 练习实例39
题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后此元素之后的数,依次后移一个位置。
程序源代码:
实例(Python 2.0+)
以上实例输出结果为:
原始列表: 1 4 6 9 13 16 19 28 40 100 0 插入一个数字: 7 排序后列表: 1 4 6 7 9 13 16 19 28 40 100
题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后此元素之后的数,依次后移一个位置。
程序源代码:
以上实例输出结果为:
原始列表: 1 4 6 9 13 16 19 28 40 100 0 插入一个数字: 7 排序后列表: 1 4 6 7 9 13 16 19 28 40 100
啊香魂
wei***[email protected]
参考解法:
#!/usr/bin/python # -*- coding: UTF-8 -*- a = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29] b = 18 a.append(b) c = a[:] l = len(c) # 从后面开始,如果比倒数第二个数大,那就将新加入的数填在倒数第一的位置,否则倒数第二的数位置后移 for i in range(l,0,-1): if (b>c[i-2]): c[i-1] =b break else: c[i-1] = c[i-2] print c
啊香魂
wei***[email protected]
参考方案:
#!/usr/bin/python
#coding:utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
#题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
list = [1,2,3,4,5,6,8,9,10,11,12,13]
print list
#我将通过插入数字7来加入按照从小到大排列的列表中
n = int(raw_input(""))
#通过for循环来讲数字在列表中定位,然后将数字添加进去就可以了。
for i in range(0,13):
if list[i] < n < list[i+1]:
list.insert(i+1,n)
print u"插入数字后的列表为:\n",list
animo
z@g***cc
参考方法:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Lei Zhong'
x=[1,3,5,6,88,99]
y=int(raw_input("输入数字: "))
x.append(y)
x.sort()
print(x)animo
z@g***cc
等一个人
252***[email protected]
参考解法:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
L = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
print('原始列表为: {}'.format(L))
num = int(input('输入一个数:'))
L.append(num)
for i in range(1, len(L)+1):
if L[-i] < L[-(i+1)]:
L[-i], L[-(i+1)] = L[-(i+1)], L[-i]
else:
break
print('新的列表为: {}'.format(L))
等一个人
252***[email protected]
JohnLee
372***[email protected]
Python3 参考方法:
# !/usr/bin/env python3 a = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31] b = 18 #对半查找,提高效率 f0, f1 = 0, len(a) - 1 while f1 - f0 > 1: mid = f0 + int((f1 - f0) / 2) if a[mid] > b: f1 = mid else: f0 = mid a.insert(f0 + 1, b) print(a)
JohnLee
372***[email protected]
dayu
923***[email protected]
参考方法:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
l = [2,5,9,12,89]
num = input("enter the num:")
if num > l[len(l)-1]:
l.append(num)
elif num < l[0]:
l.insert(0,num)
else:
for i in range(len(l)-1):
if l[i] < num and num < l[i+1]:
l.insert(i+1,num)
break
print ldayu
923***[email protected]
朦胧
253***[email protected]
Python3 测试参考:
# coding:utf-8
List1=[2,5,4,8,3,1,9]
List1=sorted(List1)
print("打印原来的数组:",List1)
print()
num=int(input("请输入一个数:"))
List1.append(num)
print("打印新的数组:",sorted(List1))
朦胧
253***[email protected]
Think_dfrent
iwa***[email protected]
Python3实例,新建一个列表存储插入后的数组,先将原数组元素一一与该数比较,把较小值插入新列表,若该数已被插入,则将原数组剩余的数插入新列表,若原数组都被遍历完仍未插入,则将该数插到新列表最后
#!/usr/bin/python3
carray=[1,4,6,9,13,16,19,28,40,100,110]
n=int(input("please enter a number:\n"))
newarray=[]
flag,a=0,0 #flag作为n有无被插入的标志位
while a<len(array):
if array[a]<n or flag==1:
newarray.append(array[a])
else:
newarray.append(n)
a=a-1
flag=1
a=a+1
if flag==0:
newarray.append(n)
print(newarray)Think_dfrent
iwa***[email protected]
colinshi
col***[email protected]
参考方法:
#!/usr/bin/python # -*- coding: UTF-8 -*- def px(n): l=[1,4,6,9,13,16,19,28,40,100] d=[] if n>l[-1]: #首先考虑是不是这个数字大于真的字符串最大的数字。如果大约直接插入到最后,结束。 l.append(n) print (l) else: #然后对这个数字进行和字符串起始处比较,小于数字的字符串直接插入新的字符串中。 while len(l) >0 : l1=l.pop(0) if l1 < n: d.append(l1) else: #当比较结果大于插入数字时,先插入该数字,然后将比较字符插入到后面,加上剩余字符串,结束。 d.append(n) d.append(l1) d=d+l break print(d) if __name__=='__main__': px(111) px(0) px (10)
colinshi
col***[email protected]
大大大大大大大熊
382***[email protected]
排序是从小到大:
a=[1,2,3,4,5,6,9] b=7 for i in range(len(a)-1): if b >=a[i] and b<=a[i+1]: a.insert(i+1, b) break elif i==(len(a)-2): a.append(b) break print(a)
大大大大大大大熊
382***[email protected]
py托马斯
152***[email protected]
参考方法:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
arr = list(map(int, raw_input("输入一串数字:").split()))
arr.sort() #排序
print arr
arr1 = []
n = int(raw_input('请输入一个数字:'))
for i in range(len(arr)):
#将从插入n的位置分成两个列表,将n插入第一列表尾再合并第二个列表为一个列表
if arr[i] < n and (arr[i+1] == n or arr[i+1] > n):
arr1.extend(arr[0:i+1])
arr1.append(n)
arr1.extend(arr[i+1:len(arr)])
break
print arr1
测试结果:
输入一串数字:1 23 3 5 22 12 [1, 3, 5, 12, 22, 23] 请输入一个数字:9 [1, 3, 5, 9, 12, 22, 23]
py托马斯
152***[email protected]