【正文】
Introduction to Computer Programming School of Computer and Information Science Southwest Forestry University 計算機編程導(dǎo)論 西南林業(yè)大學(xué) 計算機與信息學(xué)院 Review Chapter 1 sequential programming ? Use puters to solve problems ? Essentials of program design – Flow chart – Program running – Problemsolving process ? Sequential programming 復(fù)習(xí) 第 1章 順序程序設(shè)計 –用計算機解決問題的方法 –程序設(shè)計要素 ?流程圖 ?程序運行過程 ?解決問題的過程 –順序程序設(shè)計 Chapter 2 Using Array ? Array are frequently used in storing data in program design. Almost every kind of programming language provides a array data structure, such as C and Basic provide onedimensional, multidimensional array. ? Python provides the richest, the most flexible, and the most powerful forms in program design. ? This chapter describes the array structures (lists, tuples, dictionaries) in Python, and how to use them to achieve simple and powerful programs. 第 2章 使用序列 ? 序列 是程序設(shè)計中經(jīng)常用到的數(shù)據(jù)存儲方式,幾乎每一種程序設(shè)計語言都提供了表格數(shù)據(jù)結(jié)構(gòu),如 C和 Basic中的一維、多維數(shù)組等。 ? Python提供的 序列 類型在所有程序設(shè)計語言中是最豐富,最靈活,也是功能最強大的。 ? 本章介紹使用 Python中常用的 序列 結(jié)構(gòu)(列表、 元組、 字典)來實現(xiàn)一些簡單而又功能強大的程序。 Array Problem [Q21] Data sorting ? Description: Read 5 data from the keyboard, output in ascending order. ? Solutions 1: Use five variables to store data, pare and sort. 序列 問題 【問題 21】 數(shù)據(jù)排序問題 ? 問題描述:由用戶從鍵盤輸入 五 個數(shù)據(jù),按升序排序后輸出。 ? 解決方案一:用 五 個變量來存儲輸入的 五 個數(shù)據(jù),再用一一比較和交換來達到排序的目的。 序列 問題 問題 21程序: data sort x1 = input(39。請輸入第 1個元素 :39。) x2 = input(39。請輸入第 2個元素 :39。) x3 = input(39。請輸入第 3個元素 :39。) x4 = input(39。請輸入第 4個元素 :39。) x5 = input(39。請輸入第 5個元素 :39。) 將前兩個元素排好序 if x1x2: x1,x2=x2,x1 將前三個元素排好序 if x1x3: x1,x2,x3=x3,x1,x2 elif x2x3: x2,x3=x3,x2 將前四個元素排好序 if x1x4: x1,x2,x3,x4=x4,x1,x2,x3 elif x2x4: x2,x3,x4=x4,x2,x3 elif x3x4: x3,x4=x4,x3 將前五個元素排好序 if x1x5: x1,x2,x3,x4,x5=x5,x1,x2,x3,x4 elif x2x5: x2,x3,x4,x5=x5,x2,x3,x4 elif x3x5: x3,x4,x5=x5,x3,x4 elif x4x5: x4,x5=x5,x4 輸出排序結(jié)果 print(x1,x2,x3,x4,x5) Array Problem Shorting of Solution 1: ? Need to define many variables to store data ? The program is not plex, but tedious, errorprone ? When sorting more data, such as 50, the programming bees intolerable ? When sorting even more data, such as 1000, it will be ridiculous to write a program in this way. 序列 問題 解決方案一存在的問題: ? 需要定義多個變量來存儲數(shù)據(jù) ? 程序雖不復(fù)雜,但很繁瑣,寫起來容易出錯 ? 當 待 排序數(shù)據(jù)達到一定量,如 50個時,這樣的編程方式變得無法忍受 ? 當 待 排序數(shù)據(jù)達到一定量,如 1000個時,這樣的程序無法編,無法看 …… Array Problem Solution 2: Use List Analysis: ? Input ten data, and store them in a list ? Use python39。s sort () method to sort the list data and output. 結(jié)束 輸出排序后的結(jié)果 開始 用 sort( )方法對列表中數(shù)據(jù)進行排序 輸入 10個數(shù)據(jù) 存放在列表中 圖 21 數(shù)據(jù)排序流程圖 序列 問題 解決方案二: 使用列表 分析: ? 將用戶輸入的十個數(shù)據(jù)存放到一個列表中 ? 用 python的 sort( )方法對列表中數(shù)據(jù)進行排序后輸出。 結(jié)束 輸出排序后的結(jié)果