陸游愛(ài)國(guó)名言
一, 選擇題

1,求z的結(jié)果
[cpp] view plaincopyprint?
#define N 3
#define Y(n) ((N+1)*n)
z = 2*(N+Y(5+1));
#define N 3
#define Y(n) ((N+1)*n)
z = 2*(N+Y(5+1));
解答:48
2,有關(guān)多線程,多進(jìn)程的描述錯(cuò)誤的是
A, 子進(jìn)程獲得父進(jìn)程的數(shù)據(jù)空間,堆和棧的復(fù)制品
B, 線程可以與同進(jìn)程的其他線程共享數(shù)據(jù),但是它擁有自己的棧空間且擁有獨(dú)立的執(zhí)行序列
C, 線程執(zhí)行開(kāi)銷小,但是不利于資源管理和保護(hù)
D, 進(jìn)程適合在SMP機(jī)器上進(jìn)行,而線程則可以跨機(jī)器遷移
解答:D
3,
[cpp] view plaincopyprint?
struct s
{ int x:3;
int y:4;
int z:5;
double a;
}
struct s
{ int x:3;
int y:4;
int z:5;
double a;
}
求sizeof(s)
解答:
16
。菏侨∥坏淖饔茫叭齻(gè)變量是為兩個(gè)字節(jié),最后double變量是8個(gè)字節(jié),
結(jié)構(gòu)體以8字節(jié)對(duì)齊,則為16字節(jié)。
4,序列{2,1,4,9,8,10,6,20}是某排序算法第二輪排序的結(jié)果,則該算法只能是
A快速排序 B冒泡排序
C選擇排序 D插入排序
解答:A
5,我們需要監(jiān)聽(tīng)一個(gè)事件狀態(tài),讓它在狀態(tài)發(fā)生改變時(shí)主動(dòng)發(fā)出通知,請(qǐng)問(wèn)需要哪種設(shè)計(jì)模式?
A裝飾者模式 B建造者模式
C創(chuàng)新工場(chǎng)模式 D觀察者模式
解答:D
6,有2012瓶礦泉水,其中有一瓶有毒,請(qǐng)問(wèn)需要多少只老鼠才能一次性找到有毒的礦泉水?
解答:11只
二, 問(wèn)答題
1, 有0-n這n+1個(gè)數(shù),但是其中丟了一個(gè)數(shù),請(qǐng)問(wèn)如何找出丟了哪個(gè)數(shù)?
解答:
求這n個(gè)數(shù)的sum,然后計(jì)算n(n+1)/2-sum可得。
2, 解釋
[cpp] view plaincopyprint?
#typedef char (*func)(int,char*)
#typedef char (*func)(int,char*)
解答:
定義了一個(gè)函數(shù)指針的數(shù)據(jù)類型;
該數(shù)據(jù)類型可以用來(lái)定義函數(shù)指針;
定義的函數(shù)指針指向的函數(shù)的參數(shù)為
[cpp] view plaincopyprint?
(int,char*)
(int,char*)
返回值為char型。
3, 求輸出結(jié)果
[cpp] view plaincopyprint?
int a[2][2][3]= { {{1,2,3},{4,5,6}},{{7,8,9},{10,11,12}}};
int *ptr=(int *)(&a+1);
printf(“%d %d”, *(int*)(a+1), *(ptr-1));
int a[2][2][3]= { {{1,2,3},{4,5,6}},{{7,8,9},{10,11,12}}};
int *ptr=(int *)(&a+1);
printf(“%d %d”, *(int*)(a+1), *(ptr-1));
解答:
7 12 (已修定)
考察多級(jí)指針,一定要明確指針指向的是什么,才能知道它加1后跳過(guò)了多少字節(jié)。
&a是個(gè)四級(jí)指針,指向的是a這樣的數(shù)組,所以它加1,就會(huì)跳過(guò)整個(gè)數(shù)組。
4,求輸出結(jié)果
[cpp] view plaincopyprint?
#include
using namespace std;
class A
{
public:
virtual void print()
{ cout << "A::print()" <
};
class B: public A
{
public:
virtual void print()
{ cout << "B::print()" <
};
class C: public A
{
public:
virtual void print()
{ cout << "C::print()" <
};
void print(A a)
{
a.print();
}
void main()
{
A a,*aa,*ab,*ac;
B b;
C c;
aa=&a;
ab=&b;
ac=&c;
a.print();
b.print();
c.print();
aa->print();
ab->print();
ac->print();
print(a);
print(b);
print(c);
}
#include
using namespace std;
class A
{
public:
virtual void print()
{ cout << "A::print()" <
};
class B: public A
{
public:
virtual void print()
{ cout << "B::print()" <
};
class C: public A
{
public:
virtual void print()
{ cout << "C::print()" <
};
void print(A a)
{
a.print();
}
void main()
{
A a,*aa,*ab,*ac;
B b;
C c;
aa=&a;
ab=&b;
ac=&c;
a.print();
b.print();
c.print();
aa->print();
ab->print();
ac->print();
print(a);
print(b);
print(c);
}
解答:
A::print();
B::print();
C::print();
A::print();
B::print();
C::print();
A::print();
A::print();
A::print();
三,算法編程題
1,有1分,2分,5分,10分四種硬幣,每種硬幣數(shù)量無(wú)限,給定n分錢(qián),求有多少種組合可以組合成n分錢(qián)?
解答:
思路:
①,四層循環(huán)
、,使用回溯法在空間中搜索
代碼為思路2:
[cpp] view plaincopyprint?
/pic/p>
/pic/p>
#include "stdafx.h"
#include
#include
using namespace std;
int count=0;
int Target=0;
int coin[4]={1,2,5,10};
int total=0;
vector solution;
void dfs(int index)
{
if( total == Target )
{
count++;
cout << count <<":" ;
for( int i=0; i<(int)solution.size(); i++)
{
cout << solution[i]<<" ";
}
cout << endl;
return;
}
if( total > Target )
return;
for( int i=index; i<4; i++)
{
total += coin[i];
solution.push_back( coin[i] );
dfs(i);
solution.pop_back();
total -=coin[i];
}
}
int _tmain(int argc, _TCHAR* argv[])
{
while(1)
{
count=0;
cin >> Target;
dfs(0);
cout << count <
}
return 0;
}
/pic/p>
/pic/p>
#include "stdafx.h"
#include
#include
using namespace std;
int count=0;
int Target=0;
int coin[4]={1,2,5,10};
int total=0;
vector solution;
void dfs(int index)
{
if( total == Target )
{
count++;
cout << count <<":" ;
for( int i=0; i<(int)solution.size(); i++)
{
cout << solution[i]<<" ";
}
cout << endl;
return;
}
if( total > Target )
return;
for( int i=index; i<4; i++)
{
total += coin[i];
solution.push_back( coin[i] );
dfs(i);
solution.pop_back();
total -=coin[i];
}
}
int _tmain(int argc, _TCHAR* argv[])
{
while(1)
{
count=0;
cin >> Target;
dfs(0);
cout << count <
}
return 0;
}
2,馬戲團(tuán)里有個(gè)疊羅漢的表演,為了便于美觀,下面的人身高和體重都要大于上面的人。現(xiàn)在知道n個(gè)演員的身高和體重,請(qǐng)問(wèn)最多能疊多少層?
解答:
思路:
首先生成一個(gè)有向圖,用連接矩陣的方式來(lái)表示。
map[i][j]==1表示第i個(gè)人上面可以放第j個(gè)人。
然后開(kāi)始對(duì)每個(gè)人進(jìn)行深度搜索,這個(gè)圖中不可能有環(huán)。
所以對(duì)于每個(gè)人來(lái)說(shuō)就是一棵樹(shù),搜索樹(shù)的高度。
再找出最高的高度即是答案。
[cpp] view plaincopyprint?
#include "stdafx.h"
#include
#include
#include
#include
using namespace std;
int N=0;
double *weight;
double *height;
int **map;
int maxDepth=0;
vector bestPath;
int dfs( int index, vector &path )
{
int flag=0;
int depth = 0;
vector bestPath;
for( int i=0; i
{
if( map[index][i] != 0)
{
flag = 1;
vector tPath;
int t = dfs(i, tPath);
if( t > depth )
{
path = tPath;
depth = t;
}
}
}
if( flag==0 )
{
path.clear();
path.push_back(index);
return 1;
}
else
{
/pic/p>
path.push_back(index);
return depth+1;
}
}
void CreateMap()
{
map = new int*[N];
for( int i=0; i
{
map[i] = new int [N];
memset( map[i], 0, N*sizeof(int) );
}
for( int i=0; i
{
for( int j=0; j
{
if( weight[j]
map[i][j]=1;
}
}
}
void CreateData()
{
ofstream out( "in.txt" );
int N = 30;
out << N <
for( int i=0; i
out << rand() << " ";
out << endl;
for( int i=0; i
out << rand() << " ";
}
int main()
{
CreateData();
freopen( "in.txt", "r", stdin );
cout << "Please input N:" <
cin >> N;
height = new double[N];
weight = new double[N];
for( int i=0; i
cin >> height[i];
for( int i=0; i
cin >> weight[i];
CreateMap();
int depth=0;
for(int i=0; i
{
vector tPath;
int t=dfs(i,tPath);
if( t>depth )
{
bestPath = tPath;
depth = t;
}
}
cout << depth <
for( int i=0; i<(int)bestPath.size(); i++)
{
cout << height[bestPath[i]]<< " " << weight[bestPath[i]]<
}
return 0;
}
【陸游愛(ài)國(guó)名言】相關(guān)文章:
愛(ài)國(guó)詩(shī)人陸游有趣的故事02-27
愛(ài)國(guó)的名言警句200句07-31
愛(ài)國(guó)名言160句09-25
愛(ài)國(guó)的勵(lì)志經(jīng)典名言140句10-25
愛(ài)國(guó)詩(shī)人陸游作文(通用18篇)05-21
中國(guó)現(xiàn)代愛(ài)國(guó)名言(精選55句)07-05
關(guān)于愛(ài)國(guó)名言詩(shī)句(精選180句)09-27
作文素材:中國(guó)近代關(guān)于愛(ài)國(guó)的名言12-07
- 相關(guān)推薦