试题四[题目略] 有点看不懂意思,算法是根据题目给的流程图写的,我个人认为有点问题,不多说贴码。
#include <iostream>
using namespace std;
const int MAXN=50;
int n;
int D[MAXN],J[MAXN],P[MAXN];
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) 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)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
r=k;
while(D[J[r]]>D[i]&&D[J[r]]>r)
r=r-1;
if(D[J[r]]<=D[i]&&D[i]>r)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
q=k;
while(q>=r+1)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
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;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) int main() {
freopen("in.cpp","r",stdin);
while(cin>>n&&n)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
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;
}
试题五
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) typedef enum { point,circle } shape_type; /**//* 程序中的两种图形:点和圆 */
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) typedef struct { /**//* 基本的图形类型 */
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) shape_type type; /**//* 图形种类标识:点或者圆 */
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) void (*destroy)(); /**//* 销毁图形操作的函数指针 */
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) void (*draw)(); /**//* 绘制图形操作的函数指针 */
} shape_t;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) typedef struct { shape_t common; int x; int y; } point_t; /**//* 定义点类型,x、y为点坐标 */
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) void destroyPoint(point_t* this) { free(this); printf("Point destoryed!\n"); } /**//* 销毁点对象 */
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) void drawPoint(point_t* this) { printf("P(%d,%d)", this->x, this->y); } /**//* 绘制点对象 */
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) 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;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) p_point->x = va_arg(*ap, int); /**//* 设置点的横坐标 */
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) p_point->y = va_arg(*ap, int); /**//* 设置点的纵坐标 */
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) return (shape_t*)p_point; /**//* 返回点对象指针 */
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) typedef struct { /**//* 定义圆类型 */
shape_t common;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) point_t *center; /**//* 圆心点 */
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) int radius; /**//* 圆半径 */
} circle_t;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) void destroyCircle(circle_t* this) {
free( (1) ); free(this); printf("Circle destoryed!\n");
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) void drawCircle(circle_t* this) {
printf("C(");
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) (2) .draw( this->center ); /**//* 绘制圆心 */
printf(",%d)", this->radius);
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) 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;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) (3) = createPoint(ap); /**//* 设置圆心 */
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) p_circle->radius = va_arg(*ap, int); /**//* 设置圆半径 */
return p_circle;
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) shape_t* createShape(shape_type st, ) { /**//* 创建某一种具体的图形 */
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) va_list ap; /**//* 可变参数列表 */
shape_t* p_shape = NULL;
(4) (ap, st);
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if( st == point ) p_shape = createPoint( &ap); /**//* 创建点对象 */
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if( st == circle ) p_shape = createCircle(&ap); /**//* 创建圆对象 */
va_end(ap);
return p_shape;
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) int main( ) {
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) int i; /**//* 循环控制变量,用于循环计数 */
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) shape_t* shapes[2]; /**//* 图形指针数组,存储图形的地址 */
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) shapes[0] = createShape( point, 2, 3); /**//* 横坐标为2,纵坐标为3 */
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) shapes[1] = createShape( circle, 20, 40, 10); /**//* 圆心坐标(20,40),半径为10 */
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) for(i=0; i<2; i++) { shapes[i]->draw(shapes[i]); printf("\n"); } /**//* 绘制数组中图形 */
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) 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;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) class PurchaseRequest {
public:
double Amount; // 一个采购的金额
int Number; // 采购单编号
string Purpose; // 采购目的
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) class Approver { // 审批者类
public:
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) Approver() { successor = NULL; }
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) virtual void ProcessRequest(PurchaseRequest aRequest) {
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if (successor != NULL) { successor-> (1) ; } //ProcessRequest(aRequest)
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) void SetSuccessor(Approver *aSuccesssor) { successor = aSuccesssor; }
private:
(2) successor; //Approver*
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) class Congress : public Approver {
public:
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) void ProcessRequest(PurchaseRequest aRequest) {
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(aRequest.Amount >= 500000) { /**//* 决定是否审批的代码省略 */ }
else (3) ProcessRequest(aRequest);//Approver::
}
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) class Director : public Approver {
public:
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) void ProcessRequest(PurchaseRequest aRequest) { /**//* 此处代码省略 */ }
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) class President : public Approver {
public:
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) void ProcessRequest(PurchaseRequest aRequest) { /**//* 此处代码省略 */ }
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) class VicePresident : public Approver {
public:
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) void ProcessRequest(PurchaseRequest aRequest) { /**//* 此处代码省略 */ }
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) 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;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
|
|
随笔:5
文章:28
评论:1
引用:0
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
28 | 29 | 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 | 31 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|
公告
Blog里的内容如果没有注明为转载,就是原创文章,需要转载的朋友请注明出处。文章中如有错误,请指出。转载内容如果有侵权行为,请与我联系,----issac_asimoy@qq.com。
常用链接
留言簿(1)
随笔分类(5)
随笔档案(5)
文章分类(28)
文章档案(28)
相册
My World
Study Web
最新随笔
搜索
积分与排名
最新评论
![](/images/xml.gif)
阅读排行榜
评论排行榜
|
|