问答题

阅读以下说明和C函数,填充函数中的空缺,将解答填入答题纸的对应栏内。<br>【说明】<br>下面的函数sort(int n,int a[])对保存在数组a中的整数序列进行非递减排序。由于该<br>序列中的元素在一定范围内重复取值,因此排序方法是先计算出每个元素出现的次数并<br>记录在数组b中,再从小到大顺序地排列各元素即可得到一个非递减有序序列。例如,<br>对于序列6,5,6,9,6,4,8,6,5,其元素在整数区间[4,9]内取值,因此使数组元素b[O]~b[5]的下标O~5分别对应数值4~9,顺序地扫描序列的每一个元素并累计其出现的次数,即将4的个数记入b[0],5的个数记入b[l],依此类推,9的个数记入b[5]。最后依<br>次判断数组b的每个元素值,并将相应个数的数值顺序地写入结果序列即可。<br>对于上例,所得数组b的各个元素值如下:<br>那么在输出序列中写入1个4、2个5、4个6、1个8、1个9,即得4,5,5,6,6,6,6,8,9,<br>从而完成排序处理。<br>【C函数】<br>void sort(int n,int a[])<br>(int *b;<br>int i, k, number;<br>int minimum=a[0], maximum=a 0];<br>/.minimum和maximum分别表示数组a的最小、最大元素值*/<br>For(i=1;i<n;i++) {<br>if (_(1) ) minimum = a[j];<br>else<br>if (_ (2) ) maximum = a[i];<br>}<br>number = maximum - minimum + 1;<br>if (number<=l) return;<br>b = (int *) calloc (number, sizeod (int) ;<br>if (!b) return;<br>for(f=0;i<n,i++){/*计算数组a的每个元素值出现的次数并记入数组b*/<br>k= a[i] - minimum; ++b[k];<br>}<br>/*按次序在数组a中写入排好的序列*/<br>l= (3) ;<br>for(k=0; k<number; k++)<br>for(; (4) ;一一b[k] )<br>a[i++】=minimum+ (5)’ ;<br>}


问答题

阅读以下说明和C++代码,填充代码中的空缺,将解答填入答题纸的对应栏内。<br>【说明】<br>某应急交通控制系统(TraficControISy ,tem)在红灯时控制各类车辆(Vehicle)的通<br>行,其类图如图5-1所示,在紧急状态下应急车辆红灯时也可通行,其余车辆按正常规<br>则通行。<br>下面的C++代码实现以上设计,请完善其中的空缺。<br>include <typeinfo><br>include <iostream><br>using namespace std;<br>class Vehicle {/*抽象基类,车辆*/<br>public :<br>virtual void run () = 0;<br>};<br>class Emergency(/*抽象基类,可在红灯时通行的接口,函数均为纯虚函数*/<br>public:<br>(1)=0 //isEmergent()函数接口<br>(2)=0 //runRedLight()函数接口<br>};<br>class Car: public Vehicle {<br>public :<br>-car(){ }<br>void run () { /*代码略*/ }<br>};<br>Ciass Truck:public vehicie {<br>Public;<br>-Truck(){ }<br>Void run() { /*代码略*/}<br>};<br>Class policecar: (3) {<br>Private:<br>bool isEmergency;<br>public :<br>PoliceCar () : Car () , Emergency () { this=>isEmergency = false;<br>}<br>PoliceCar (bool b) : Car () , Emergency () { this=>isEmergency = b; }<br>~PoliceCar () { }<br>bool isEmergent () { ret irn (4) ; }<br>void runRedLight () { /*代码略*/ }<br>};<br>/*类Ambulance. FireEngine/*实现代码略*/<br>class TraficControISystem {/*交通控制类*/<br>private :<br>Vehicle*v[24]; int numVeh: cles./*在构造函数中设置初始值为0*/<br>public:<br>void control(){ //控制在紧急情况下应急车辆红灯通行,其他情况按常规通行<br>for (int i = 0; i<[numVehicles;++] {<br>Emergency*ev=dynamic_cast<Emergency*(v[i]);<br>if (ev !=0(5)-: runRedLight ()<br>Else (6)-:run()<br>}<br>}<br>Void add(vehicle){v[numvehicles++]=vehicle;}<br>/*添加车辆*/<br>Void shutdown(){for (int i =0;i<numvehicles;i++){ deletev[i];} }<br>}<br>int main () {<br>TraficControlSystem* tcs =new TraficControlSystem;<br>tcs->add (new Car () );, t cs->add (new PoliceCar ()) ;<br>tcs->add (new Ambulance ()) ; tcs- >add (new Ambulance (true)) ;<br>tcs->add (new FireEngine (true)) ; -.cs->add (new FireEngine ()) ;<br>tcs->add (new Truck ()) ;<br>tcs->control () ; tcs->shul Down () ;<br>delete tcs;<br>}


问答题

阅读以下说明和Java源程序,将应填入(n)处的字句写在答题纸的对应栏内。<br>说明<br>以下程序的功能是计算三角形、矩形和正方形的面积并输出。<br>程序由5个类组成:AreaTest是主类,类Triangle、Rectangle和Square分别表示三角形、矩形和正方形,抽象类Figure提供了一个计算面积的抽象方法。<br>程序<br>public class AreaTest{<br>public static void main(String args[]){<br>Figure[]figures={<br>new Triangle(2,3,3),new Rectangle(5,8), new Square(5)<br>};<br>for(int i=0;i<figures.1ength;i++){<br>System.out.println(figures[i]+"area="+figures[i].getArea());<br>}<br>}<br>}<br>public abstract class Figure{<br>public abstract double SetAJea();<br>public class Rectangle extends (1) {<br>double height;<br>double width;<br>public Rectangle(double height,double width){<br>this.height=height;<br>this.width=width;<br>}<br>public String toString(){<br>return "Rectangle:height="+height+",width="+width+":";<br>}<br>public double getArea() { return (2);<br>} } public class Square extends (3) {<br>public Square(double width) {<br>(4);<br>}<br>public String toString() {<br>return "Square:width="+width+":";<br>} } public class Triangle extends (5). {<br>double la;<br>double lb;<br>double lc;<br>public Triangle(double la,double lb,double lc) {<br>this.la=la; this.lb=lb; this.lc=lc;<br>public String toString(){<br>return "Triangle: sides="+la+","+lb+","+lc+":";<br>public double getArea() {<br>double s=(la+lb+lc)/2.0;<br>return Math.sqrt(s*(s-la)*(s-lb)*(s?1c));<br>}<br>}


火星搜题