阅读以下说明和 C 函数,填充函数中的主缺,将解答填入答题纸的对应栏内。
【说明】
下面的函数 sort(int n,int a[])对保存在数在a中的整数序列进行非递减排序。由于该序列中的元素在一定范围内重复取值,因此排序方法是先计算出每个元素出现的次数并记录在数组b中,再从小到大顺序地排列各元素即可得到一个非递减有序序列。例如, 对于序列 6,5,6,9,6,4,8,6,5. 其元素在整数区间 [4,9]内取值,因此使数组元素 b[0] ~b[5]的下标。0~5分别对应数值4~9. 顺序地扫描序列的每一个元素并累计其出现的次数,即将 4的个数记入b[0],5的个数记b[1],依此类推,9的个数记入b[5]最后依次判断数组b的每个元素值,并将相应个数的数值顺序地写入结果序列即可。
对于上例,所得数组 b 的各个元素值如下:

那么在输出序列中写入 1 个 4、2个 5、4个 6、1 个 8、1 个 9,即得4,5,5,6,6,6,6,8,9,从而完成排序处理。
【C 函数】
void sort(int n ,int a[ ])
{ int *b;
int i ,k ,number;
int minimum = a[0] , maximum = a[0];
/* minimum 和 maximum 分别表示数组a的最小、最大元素值*/
for(i=1;i<n;i++){
if( (1) ) minimum=a[j];
Else
if( (2) ) maximum=a[i];
}
number = maximum - minimum + 1;
if (number<=1) return;
b = (int *)calloc (number ,sizeof(int)) ;
if (!b) return;
for(i=0;i<n;i++){ /* 计算数组a元素值出现的次数并计入数组b */
k=a[i]-minimum; ++b[k];
}
/*按次序在数组 a 中写入排好的序列*/
i=(3) ;
for( k=0; k<number; k++ )
for(; (4) ; --b[k] )
a [i++] = minimum +(5);
}
正确答案及解析
正确答案
解析
(1) a[i]<minimum ,或a[i]<= minimum ,或其等价形式
(2) a[i]>maximum ,或a[i]>= maximum ,或其等价形式
(3) 0
(4)b[k] ,或b[k]>0 ,或b[k]!=0 ,或其等价形式
(5)k
本题考查 C 程序的基本语法和运算逻辑。 首先应认真分析题目中的说明,然后确定代码结构和各变量的作用。
空(1)和 (2)所在 for 语句的功能是求出数组 a 中的最小元素 minimum 和最大元素 maxlmum 。在设置了minimum 和 maximum的初始值后,空(1)处的判断条件是只要目前的元素 a[i]小于 minimum ,就需要更新minimum ,反之,空(2)处的判断条件是只要目前的元素 a[i]大于 maximum ,就需 更新maximum ,因此空(1)处应填入 a[i]<minimum 或其等价方式,空 (2)处应填入 a[i] >maximum 或其等价方式。 minimum 和 maximum 的作用是要确定计数数组 b 的大小。
根据题目中的描述,序列中的每个元素 a[i]都对应到计数数组 b[]的一个元素 b[k] 对应方式为: k = a[i] -minimum ,其中minimum 是数组 a 中的最小元素,显然在计数时, 一个数值出现一次,就在对应的 b[k] 中累加一次。
空(3)~(5)所在的语句组是产生排序后的序列,重新写入数组 a。首先需明确
变量 i和 k 的作用,根据它们在该语句组中的出现位置, i用于表示数组 a 的元素下标, k 用于表示数组 b 中元素的下标,因此,空(3)处应填入 0 ,使得从数组a 中下标为 0 的数组元素开始。通过循环控制 "for( k=0; k<number; k++)"己经明确数组 b 的下标变化方式,而需要写入数组 a 的元素个数表示在 b[k] 中,所以 “ for(;(4); --b[k])”中空(4)处应填入 "b[k]>0" 或其等价形式。 (由于 b[k] 中记录的是元素 k+ minimum 的出 现次数,所以空(5)处应填入 "k",从而将元素值恢复后再写回去。
你可能感兴趣的试题
The Internet of( 1)(IoT) describes physical objects that are embedded with Sensor,processing abilities,softwares,and other technologies that connect with other devices and systems over the ( 2) or other communication and exchange data networks .Over the past few years, IoT has become one of the most important technologies of the ( 3 ) centuryWe can connect objects to the Internet via embedded devices. By means of( 4) computing, the cloud, big data, and mobile technologies, physical things can share and collect data with minimal human intervention. Traditional fields of embedded systems,wireless ( 5) networks (WSNs),control systems,automation, independently and collectively enable IoT.回答5处
-
- A.sensor
- B.searching
- C.service
- D.source
- 查看答案
The Internet of( 1)(IoT) describes physical objects that are embedded with Sensor,processing abilities,softwares,and other technologies that connect with other devices and systems over the ( 2) or other communication and exchange data networks .Over the past few years, IoT has become one of the most important technologies of the ( 3 ) centuryWe can connect objects to the Internet via embedded devices. By means of( 4) computing, the cloud, big data, and mobile technologies, physical things can share and collect data with minimal human intervention. Traditional fields of embedded systems,wireless ( 5) networks (WSNs),control systems,automation, independently and collectively enable IoT.回答4处
-
- A.low-level
- B.low-cost
- C.high-cost
- D.high-performance
- 查看答案
The Internet of( 1)(IoT) describes physical objects that are embedded with Sensor,processing abilities,softwares,and other technologies that connect with other devices and systems over the ( 2) or other communication and exchange data networks .Over the past few years, IoT has become one of the most important technologies of the ( 3 ) centuryWe can connect objects to the Internet via embedded devices. By means of( 4) computing, the cloud, big data, and mobile technologies, physical things can share and collect data with minimal human intervention. Traditional fields of embedded systems,wireless ( 5) networks (WSNs),control systems,automation, independently and collectively enable IoT.回答3处
-
- A.19th
- B.20th
- C.21th
- D.22th
- 查看答案
The Internet of( 1)(IoT) describes physical objects that are embedded with Sensor,processing abilities,softwares,and other technologies that connect with other devices and systems over the ( 2) or other communication and exchange data networks .Over the past few years, IoT has become one of the most important technologies of the ( 3 ) centuryWe can connect objects to the Internet via embedded devices. By means of( 4) computing, the cloud, big data, and mobile technologies, physical things can share and collect data with minimal human intervention. Traditional fields of embedded systems,wireless ( 5) networks (WSNs),control systems,automation, independently and collectively enable IoT.回答2处
-
- A.path
- B.Internet
- C.route
- D.switch
- 查看答案
The Internet of( 1)(IoT) describes physical objects that are embedded with Sensor,processing abilities,softwares,and other technologies that connect with other devices and systems over the ( 2) or other communication and exchange data networks .Over the past few years, IoT has become one of the most important technologies of the ( 3 ) centuryWe can connect objects to the Internet via embedded devices. By means of( 4) computing, the cloud, big data, and mobile technologies, physical things can share and collect data with minimal human intervention. Traditional fields of embedded systems,wireless ( 5) networks (WSNs),control systems,automation, independently and collectively enable IoT.回答1处
-
- A.this
- B.thing
- C.think
- D.things
- 查看答案