試題四[題目略]
有點看不懂意思,算法是根據題目給的流程圖寫的,我個人認為有點問題,不多說貼碼。
#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!
#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;
}
有點看不懂意思,算法是根據題目給的流程圖寫的,我個人認為有點問題,不多說貼碼。


























































試題五













































































































給出參考答案
(1)this->center
(2)this->center->common
(3)p_circle->center
(4)va_start
(5)C(P(20,40),10)
試題六[還是考繼承,派生,C++貌似只會考這些]






























































































