1 / 11

编写程序对以下数据从小到大排序: 12 , 24 , 5 , 10 , 58 , 64 , 21

问题. 编写程序对以下数据从小到大排序: 12 , 24 , 5 , 10 , 58 , 64 , 21. 分析. 12 , 24 , 5 , 10 , 58 , 64 , 21 ( 1 )如何输入和存储这几个数字? ( 2 )怎样对这些数据进行排序? ( 3 )如何输出?. ( 1 )如何输入和存储这几个数字?. 我们可以用数组来存储。 数组的格式 1 : type 数组类型名 =array [ 下标类型 ] of 基类型; var 变量名: 数组类型名 例如: type a=array[1..7] of integer;

dulcea
Download Presentation

编写程序对以下数据从小到大排序: 12 , 24 , 5 , 10 , 58 , 64 , 21

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 问题 • 编写程序对以下数据从小到大排序: • 12,24,5,10,58,64,21

  2. 分析 • 12,24,5,10,58,64,21 • (1)如何输入和存储这几个数字? • (2)怎样对这些数据进行排序? • (3)如何输出?

  3. (1)如何输入和存储这几个数字? • 我们可以用数组来存储。 • 数组的格式1: • type • 数组类型名 =array [下标类型] of 基类型; • var 变量名:数组类型名 • 例如: • type • a=array[1..7] of integer; • Var a1:a; 即存储数据的个数

  4. 即存储数据的个数 数组的格式2 var 变量名:array [下标类型] of 基类型 例如: Var a1:array[1..7] of integer;

  5. 存储:12,24,5,10,58,64,21 • 定义7个空间的数组来存储上面的7个数据 • Var • a1:array[1..7] of integer; • i:integer; • Begin • for i=1 to 7 do • read(a[i]); • … • End. 从键盘上输入上面7个数据 a[1] a[2] a[3] a[4] a[5] a[6] a[7]

  6. 数组应用 • 用a[1]表示第一个元素 • 用a[2]表示第二个元素 • 用a[i]表示第i个元素 a[1] a[2] a[3] a[4] a[5] a[6] a[7]

  7. 排序:12,24,5,10,58,64,21 • 选择排序思想: • (1)对待排序的记录序列进行n-1遍的处理; • (2)第1遍处理是将a[1..n]中最小者与a[1]交换位置; • (3)第2遍处理是将a[2..n]中最小者与a[2]交换位置; • ......; • (4)第i遍处理是将a[i..n]中最小者与a[i]交换位置。 • 这样,经过i遍处理之后,前i个记录的位置就已经按从小到大的顺序排列好了

  8. 二维数组 • 格式一: • Type • 数组类型名=array[行下界..行上界,列下界..列上界] of 基类型 • 例如:type • t1=array[1..3,1..4] of real; • var • a:t1; • 格式二: • var •  数组变量名:array[行下界..行上界,列下界..列上界] of 基类型 • 例如:var • a:array[1..3,1..4] of real;

  9. 2、元素的引用 (1)与一维类似,只是需要指定两个下标 a[1,1]:=35; a[2,4]:=a[2,3]+1; (2)操作二维数组时,常使用二重循环。 for I:=1 to 3 do for j:=1 to 4 do readln(a[I,j]); (3)定义一维数组时,如果基类型仍是一维数组类型,则得到的是一个二维数组 type t1:array[1..5] of integer; t2:array[1..3] of t1; 实际上是一个3行5列的数组 var a:t2;

More Related