设数组a中有N个元素,并已按递增次序排列,下面______程序段可以使a数组的元素按递减次序排列.(A) For i = 1 To N (B) For i = 1 To N / 2 a(N - i + 1) = a(i) a(i) = a(N - i + 1) Next i Next i (C)For i = 1 To N
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/27 02:12:34
设数组a中有N个元素,并已按递增次序排列,下面______程序段可以使a数组的元素按递减次序排列.(A) For i = 1 To N (B) For i = 1 To N / 2 a(N - i + 1) = a(i) a(i) = a(N - i + 1) Next i Next i (C)For i = 1 To N
设数组a中有N个元素,并已按递增次序排列,下面______程序段可以使a数组的元素按递减次序排列.
(A) For i = 1 To N (B) For i = 1 To N / 2
a(N - i + 1) = a(i) a(i) = a(N - i + 1)
Next i Next i
(C)For i = 1 To N (D)For i = 1 To N / 2
t = a(i) t = a(i)
a(i) = a(N - i + 1) a(i) = a(N - i + 1)
a(N - i + 1) = t a(N - i + 1) = t
Next i Next i
设数组a中有N个元素,并已按递增次序排列,下面______程序段可以使a数组的元素按递减次序排列.(A) For i = 1 To N (B) For i = 1 To N / 2 a(N - i + 1) = a(i) a(i) = a(N - i + 1) Next i Next i (C)For i = 1 To N
选第四个
递增变递减就是把数组转置过来.
具体的就是把第一个元素和倒数第一个元素交换,一直交换到中间两个元素,一共交换 N/2次,N/2 向下取整,这样就达到目的了.
例如:
1 2 3 4 5
N = 5 N/2 = 2
执行过程为:
-->
5 2 3 4 1
-->
5 4 3 2 1