GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.runoob.com — Python 练习实例39 | 菜鸟教程
Python 基础教程
Python 基础教程
Python 简介
Python 环境搭建
Python 中文编码
Python VS Code
Python 基础语法
Python 变量类型
Python 运算符
Python 条件语句
Python 循环语句
Python While 循环语句
Python for 循环语句
Python 循环嵌套
Python break 语句
Python continue 语句
Python pass 语句
Python Number(数字)
Python 字符串
Python 列表(List)
Python 元组
Python 字典(Dictionary)
Python 日期和时间
Python 函数
Python 模块
Python 文件I/O
Python File 方法
Python 异常处理
Python OS 文件/目录方法
Python 内置函数
Python 高级教程Python 面向对象 Python 正则表达式 Python CGI 编程 Python MySQL Python 网络编程 Python SMTP Python 多线程 Python XML 解析 Python GUI 编程(Tkinter) Python2.x 与 3.x 版本区别 Python IDE Python JSON Python AI 绘画 Python 100例 Python 测验Python 练习实例39题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。 程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后此元素之后的数,依次后移一个位置。 程序源代码: 实例(Python 2.0+)#!/usr/bin/python
# -*- coding: UTF-8 -*-
if __name__ == '__main__':
# 方法一 : 0 作为加入数字的占位符
a = [1,4,6,9,13,16,19,28,40,100,0]
print ('原始列表:')
for i in range(len(a)):
print (a[i])
number = int(input("\n插入一个数字:\n"))
end = a[9]
if number > end:
a[10] = number
else:
for i in range(10):
if a[i] > number:
temp1 = a[i]
a[i] = number
for j in range(i + 1,11):
temp2 = a[j]
a[j] = temp1
temp1 = temp2
break
print ('排序后列表:')
for i in range(11):
print (a[i])
以上实例输出结果为: 原始列表: 1 4 6 9 13 16 19 28 40 100 0 插入一个数字: 7 排序后列表: 1 4 6 7 9 13 16 19 28 40 100
AI 思考中...
|
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |