未分类题

计算一个整数数组a的最长递增子序列长度的方法描述如下:
假设数组a的长度为n,用数组b的元素b[i]记录以a[i](0≤i<n)为结尾元素的最长递增予序列的长度,则数组a的最长递增子序列的长度为中级软件设计师,历年真题,2014年下半年(下午)《软件设计师》真题;其中b[i]满足最优子结构,可递归定义为:
中级软件设计师,历年真题,2014年下半年(下午)《软件设计师》真题
【C代码】
下面是算法的C语言实现。
(1)常量和变量说明
a:长度为n的整数数组,待求其最长递增子序列
b:长度为n的数组,b[i]记录以a[i](0≤i<n)为结尾元素的最长递增子序列的长
度,其中0≤i<n
len:最长递增子序列的长度
i,j:循环变量
temp:临时变量
(2)C程序
#include<stdio.h>
int maxL(int*b,int n){
int i,temp=0;
for(i=0;i<n;i++){
if(b[i]>temp)
temp=b[i];
}
return temp;
}
int main(  ){
int n,a[100],b[100],i,j,len;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
(1);
for(i=1;i<n;i++){
for(j=0,len=0;(2);j++){
if((3)&&len<b[j])
len=b[j];
}
(4);
}
Printf("len:%d\n",maxL(b,n));
printf("\n");
}
【问题1】(8分)
根据说明和C代码,填充C代码中的空(1)~(4)。
【问题2】(4分)
根据说明和C代码,算法采用了(5)设计策略,时间复杂度为(6)(用O符号表示)。
【问题3】(3分)
已知数组a={3,10,5,15,6,8},根据说明和C代码,给出数组b的元素值。

【参考答案】

【问题1】
(1)b[0]=1
(2)j<i
(3)a[j]<=a[i]
(4)b[i]......

(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)
热门 试题

未分类题
某灯具厂商欲生产一个灯具遥控器,该遥控器具有7个可编程的插槽,每个插槽都有开关灯具的开关,现采用Command(命令)模式实现该遥控器的软件部分。Command模式的类图如图6-1所示。【Java代码】class Light{public Light( ){}public Light(String name){ *代码省略* }public void on( ){ *代码省略* } 开灯public void off( ){ *代码省略* } 关灯 其余代码省略}(1){public void execute( );}class LightOnCommand implements Command{ 开灯命令Light light;public LightOnCommand(Light light){this.light=light;}public void execute( ){(2);}}class LightOffCommand implements Command{ 关灯命令Light light;public LightOffCommand(Light light){this.light=light;}public void execute( ){(3);}}class RemoteControl{ 遥控器Command[]onCommands=new Command[7];Command[]offCommands=new Command[7];public RemoteControl( ){ *代码省略* }public void setCommand(int slot,Command onCommand,Command offCommand){(4)=onCommand;(5)=offCommand;}public void onButtonWasPushed(int slot){(6);}public void offlButtonWasPushed(int slot){(7);}}class RemoteLoader{public static void main(String[]args){RemoteControl remoteControl=new RemoteControl( );Light livingRoomLight=new Light( Living Room );Light kitchenLight=new Light( kitchen );LightOnCommand livingRoomLightOn=new LightOnCommand(livingRoomLight);LightOffCommand livingRoomLightOff=new LightOffCommand(livingRoomLight);LightOnCommand kitchenLightOn=new LightOnCommand(kitchenLight);LightOffCommand kitchenLightOff=new LightOffCommand(kitchenLight);remoteControl.setCommand(0,livingRoomLightOn,livingRoomLightOff);remoteControl.setCommand(1,kitchenLightOn,kitchenLightOff);remoteControl.onButtonWasPushed(0);remoteControl.offButtonWasPushed(0);remoteControl.onButtonWasPushed(1);remoteControl.offButtonWasPushed(1);}}