阅读以下说明和Java代码,回答问题
[说明]
对多个元素的聚合进行遍历访问时,需要依次推移元素,例如对数组通过递增下标的方式,数组下标功能抽象化、一般化的结果就称为迭代器(Iterator)。模式以下程序模拟将书籍(Book)放到书架(BookShelf)上并依次输出书名。这样就要涉及到遍历整个书架的过程。使用迭代器Iterator实现。图6-1显示了各个类间的关系。以下是JAVA语言实现,能够正确编译通过。
[图6-1]

[Java代码]
//Iterator. java文件
public interface Iterator {
public abstract boolean hasNext();
public abstract Object next();
}
//Aggregate. java文件
public interface Aggregate {
public abstract Iterator iterator();
}
//Book. java
public class Book {
//省略具体方法和属性
}
//BookshelfIterator. java文件
public class Bookshelf工terator (1) Iterator{
private BookShelf bookShelf;
private int index;
public BookshelfIterator(BookShelf bookShelf) {
this. bookShelf = bookShelf;
this. index = 0;
}
public boolean hasNext(){//判断是否还有下一个元素
if(index < bookShelf. getLength()){
return true;
}else{
return false;
}
}
public Object next()f//取得下一个元素
Book book = bookShelf. getBookAt(index);
index++;
return book;
}
}
//BookShelf. java
import java. util. Vector;
public class BookShelf {
private Vector books;
public BookShelf(int initialsize){
this. books = new Vector(initialsize);
}
public Book getBookAt(int index){
return(Book)books.get(index);
}
public int getLength(){
return books.size();
}
public Iterator iterator(){
return new BookShelfIterator( (2) );
}
}
//Main. java文件
public class Main {
public static void main(String args){
BookShelf bookShelf = new BookShelf(4);
//将书籍上架,省略代码
Iterator it = bookShelf. (3) ;
while( (4) ){//遍历书架,输出书名
Book book = (Book)it. (5) ;
System.out.printin(" "+book.getName());
}
}
}
第1题:
阅读下列函数说明和C++代码,回答问题
[说明]
对多个元素的聚合进行遍历访问时,需要依次推移元素,例如对数组通过递增下标的方式,数组下标功能抽象化、一般化的结果就称为迭代器(Iterator)。模式以下程序模拟将书籍(Book)放到书架(BookShelf)上并依次输出书名。这样就要涉及到遍历整个书架的过程。使用迭代器Iterator实现。图5-1显示了各个类间的关系。以下是C++语言实现,能够正确编译通过。
[图5-1]

[C++代码]
template (1) >
class Iterator{
public:
virtual bool hasNext() = 0;
(2) Object* next() = 0;
};
class Book{
//省略具体方法和属性
};
class BookShelf{
private:
vector books;
public:
BookShelf(){
}
Book* getBookAt(int index){
return &booksindex;
}
int getLength(){
return books. size();
}
};
template
class BookshelfIterator : public (3) {
private:
BookShelf * bookShelf;
int index;
public:
BookshelfIterator(BookShelf *bookShelf){
this->bookShelf = bookShelf;
index = 0;
}
bool hasNext(){//判断是否还有下一个元素
if(index < bookShelf->getLength()){
return true;
}else{
return false;
}
}
Objeot* next(){//取得下一个元素
return bookShelf->getBookAt(index++);
}
};
int main()
{
BookShelf bookShelf;
//将书籍上架,省略代码
Book *book;
Iterator *it = new BookShelfIterator( (4) );
while( (5) ){//遍历书架,输出书名
book=(Book*)it->next();
/*访问元素*/
}
return 0;
}
第2题:
试题三(共15分)
阅读以下说明和C函数,回答问题 l和问题 2,将解答填入答题纸的对应栏内。
【说明】
对于具有n个元素的整型数组a,需要进行的处理是删除a中所有的值为 0的数组元素,并将a中所有的非 O元素按照原顺序连续地存储在数组空间的前端。下面分别用函数CompactArr_v1 和CompactArr v2来实现上述处理要求,函数的返回值为非零元素的个数。 函数CompactArr_vl(int a[],int n)的处理思路是:先申请一个与数组a的大小相同的动态数组空间,然后顺序扫描数组a的每一个元素,将遇到的非O元素依次复制到动态数组空间中,最后再将动态数组中的元素传回数组a中。
函数CompactArr_v2(int a[],int n)的处理思路是:利用下标i(初值为 0)顺序扫描数组a的每一个元素,下标k(初值为0)表示数组 a中连续存储的非0元素的下标。扫描时,每遇到一个数组元素,i就增 1,而遇到非 0元素并将其前移后k才增 1。

【问题1】 (12分)
请根据说明中函数CompactArr_v1的处理思路填补空缺(1)~(3),根据CompactArr_v2的处理
思路填补空缺(4)。
【问题2】(3分)
请说明函数CompactArr vl存在的缺点。
第3题:
6、下列关于数组下标的描述中,错误的是()。
A.C++语言中数组元素的下标是从0开始的
B.数组元素下标只能是常量
C.数组元素可以通过数组名和下标来表示
D.数组元素的某维下标值应小于该维的大小
第4题:
阅读下列说明和C代码,回答问题l至问题3.将解答写在答题纸的对应栏内。
【说明】
计算一个整数数组a的最长递增子序列长度的方法描述如下:
假设数组a的长度为n,用数组b的元素b[i]记录以a[i](0≤i<n)为结尾元素的最长
递增子序列的长度,则数组a的最长递增子序列的长度为器
;其中b[i]满足最优子结构,可递归定义为:
【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;

【问题l】(8分)
根据说明和C代码,填充C代码中的空(1)~(4)。
【问题2】(4分)
根据说明和C代码,算法采用了(5)设计策略,时间复杂度为(6)(用O符号表示)。
【问题3】(3分)
已知数组a={3,10,5,15,6,8},根据说明和C代码,给出数组b的元素值。
第5题: