试题四[题目略] 有点看不懂意思,算法是根据题目给的流程图写的,我个人认为有点问题,不多说贴码。
#include <iostream>
using namespace std;
const int MAXN=50;
int n;
int D[MAXN],J[MAXN],P[MAXN];

 void Search() {
memset(J,0,sizeof(J));
D[0]=J[0]=0;
J[1]=1;
int k=1,i,q,r,MaxValue=0;
for(i=2;i<=n;++i)
 {
r=k;
while(D[J[r]]>D[i]&&D[J[r]]>r)
r=r-1;
if(D[J[r]]<=D[i]&&D[i]>r)
 {
q=k;
while(q>=r+1)
 {
J[q+1]=J[q];
q=q-1;
}
J[r+1]=i;
++k;
}
}
for(i=1;i<=n;++i)
if(J[i])
MaxValue+=P[J[i]];
cout<<MaxValue<<endl;
}

 int main() {
freopen("in.cpp","r",stdin);
while(cin>>n&&n)
 {
for(int i=1;i<=n;++i)
scanf("%d",&D[i]);
for(int j=1;j<=n;++j)
scanf("%d",&P[j]);
Search();
}
return 0;
}
试题五
 typedef enum { point,circle } shape_type; /**//* 程序中的两种图形:点和圆 */
 typedef struct { /**//* 基本的图形类型 */
 shape_type type; /**//* 图形种类标识:点或者圆 */
 void (*destroy)(); /**//* 销毁图形操作的函数指针 */
 void (*draw)(); /**//* 绘制图形操作的函数指针 */
} shape_t;
 typedef struct { shape_t common; int x; int y; } point_t; /**//* 定义点类型,x、y为点坐标 */
 void destroyPoint(point_t* this) { free(this); printf("Point destoryed!\n"); } /**//* 销毁点对象 */
 void drawPoint(point_t* this) { printf("P(%d,%d)", this->x, this->y); } /**//* 绘制点对象 */
 shape_t* createPoint(va_list* ap) { /**//* 创建点对象,并设置其属性 */
point_t* p_point;
if( (p_point = (point_t*)malloc(sizeof(point_t))) == NULL ) return NULL;
p_point->common.type = point; p_point->common.destroy = destroyPoint;
p_point->common.draw = drawPoint;
 p_point->x = va_arg(*ap, int); /**//* 设置点的横坐标 */
 p_point->y = va_arg(*ap, int); /**//* 设置点的纵坐标 */
 return (shape_t*)p_point; /**//* 返回点对象指针 */
}
 typedef struct { /**//* 定义圆类型 */
shape_t common;
 point_t *center; /**//* 圆心点 */
 int radius; /**//* 圆半径 */
} circle_t;
 void destroyCircle(circle_t* this) {
free( (1) ); free(this); printf("Circle destoryed!\n");
}
 void drawCircle(circle_t* this) {
printf("C(");
 (2) .draw( this->center ); /**//* 绘制圆心 */
printf(",%d)", this->radius);
}
 shape_t* createCircle(va_list* ap) { /**//* 创建一个圆,并设置其属性 */
circle_t* p_circle;
if( (p_circle = (circle_t*)malloc(sizeof(circle_t))) == NULL ) return NULL;
p_circle->common.type = circle; p_circle->common.destroy = destroyCircle;
p_circle->common.draw = drawCircle;
 (3) = createPoint(ap); /**//* 设置圆心 */
 p_circle->radius = va_arg(*ap, int); /**//* 设置圆半径 */
return p_circle;
}
 shape_t* createShape(shape_type st, ) { /**//* 创建某一种具体的图形 */
 va_list ap; /**//* 可变参数列表 */
shape_t* p_shape = NULL;
(4) (ap, st);
 if( st == point ) p_shape = createPoint( &ap); /**//* 创建点对象 */
 if( st == circle ) p_shape = createCircle(&ap); /**//* 创建圆对象 */
va_end(ap);
return p_shape;
}
 int main( ) {
 int i; /**//* 循环控制变量,用于循环计数 */
 shape_t* shapes[2]; /**//* 图形指针数组,存储图形的地址 */
 shapes[0] = createShape( point, 2, 3); /**//* 横坐标为2,纵坐标为3 */
 shapes[1] = createShape( circle, 20, 40, 10); /**//* 圆心坐标(20,40),半径为10 */
 for(i=0; i<2; i++) { shapes[i]->draw(shapes[i]); printf("\n"); } /**//* 绘制数组中图形 */
 for( i = 1; i >= 0; i-- ) shapes[i]->destroy(shapes[i]); /**//* 销毁数组中图形 */
return 0;
}
[运行结果]
P(2,3)
(5)
Circle destoryed!
Point destoryed!
给出参考答案 (1)this->center
(2)this->center->common
(3)p_circle->center
(4)va_start
(5)C(P(20,40),10)
试题六[还是考继承,派生,C++貌似只会考这些]
#include <string>
#include <iostream>
using namespace std;
 class PurchaseRequest {
public:
double Amount; // 一个采购的金额
int Number; // 采购单编号
string Purpose; // 采购目的
};

 class Approver { // 审批者类
public:
 Approver() { successor = NULL; }
 virtual void ProcessRequest(PurchaseRequest aRequest) {
 if (successor != NULL) { successor-> (1) ; } //ProcessRequest(aRequest)
}
 void SetSuccessor(Approver *aSuccesssor) { successor = aSuccesssor; }
private:
(2) successor; //Approver*
};

 class Congress : public Approver {
public:
 void ProcessRequest(PurchaseRequest aRequest) {
 if(aRequest.Amount >= 500000) { /**//* 决定是否审批的代码省略 */ }
else (3) ProcessRequest(aRequest);//Approver::
}
};

 class Director : public Approver {
public:
 void ProcessRequest(PurchaseRequest aRequest) { /**//* 此处代码省略 */ }
};

 class President : public Approver {
public:
 void ProcessRequest(PurchaseRequest aRequest) { /**//* 此处代码省略 */ }
};

 class VicePresident : public Approver {
public:
 void ProcessRequest(PurchaseRequest aRequest) { /**//* 此处代码省略 */ }
};

 int main() {
Congress Meeting; //董事会
VicePresident Sam; //副董事长
Director Larry ; //主任
President Tammy; //董事长
// 构造责任链
Meeting.SetSuccessor(NULL);
Sam.SetSuccessor( (4) ); //&Tammy
Tammy.SetSuccessor( (5) ); //&Meeting
Larry.SetSuccessor( (6) ); //&Sam
PurchaseRequest aRequest; // 构造一采购审批请求
cin>>aRequest.Amount; // 输入采购请求的金额
(7) .ProcessRequest(aRequest); // 开始审批
//Larry
return 0;
}

|
|
随笔:5
文章:28
评论:1
引用:0
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
30 | 31 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|
公告
Blog里的内容如果没有注明为转载,就是原创文章,需要转载的朋友请注明出处。文章中如有错误,请指出。转载内容如果有侵权行为,请与我联系,----issac_asimoy@qq.com。
常用链接
留言簿(1)
随笔分类(5)
随笔档案(5)
文章分类(28)
文章档案(28)
相册
My World
Study Web
最新随笔
搜索
积分与排名
最新评论

阅读排行榜
评论排行榜
|
|