C/C++筆試題(4)

 1.    寫出程序運行結果<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

int sum(int a)

{ auto int c=0;

  static int b=3;

c+=1;

b+=2;

return(a+b+C);

}

void main()

{ int I;

int a=2;

for(I=0;I<5;I++)

{ printf("%d,", sum(a))}

}

 

2.  int func(int a)

{int b;

 switch(a)

  { case 1: 30;

    case 2: 20;

    case 3: 16;

default: 0

}

return b;

}

func(1)=?

 

3:

int a[3];

a[0]=0; a[1]=1; a[2]=2;

int *p, *q;

p=a;

q=&a[2];

a[q-p]=?

 

4. 定義 int **a[3][4], 則變量佔有的內存空間爲:_____

 

5.編寫一個函數,要求輸入年月日時分秒,輸出該年月日時分秒的下一秒。如輸入200412

31235959秒,則輸出<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />200511000

 

6.寫一個函數,判斷一個int型的整數是否是2的冪,即是否可以表示成2^X的形式(不可

以用循環)   我只知道是用遞推,大概寫了一下,如下:

int IsTwoPow(int s)

{ if(s==1)return FALSE;

  s=s>>1;

  if(s>1)IsTwoPow(s);

  return (s==1)?TRUE:FALSE;//大概是這個意思,但是這一句似乎不該這麼返回!

}

 

7、 AB從一堆玻璃球(共100個)裏向外拿球,規則如下:

 (1)A先拿,然後一人一次交替着拿;

2)每次只能拿1個或2個或4個;

3)誰拿最後一個球,誰就是最後的失敗者;

  AB誰將是失敗者?寫出你的判斷步驟。

 

8.已知:無序數組,折半查找,各元素值唯一。函數原型是:Binary_Seach(int array[], int iValue, int iCount)array是數組,在裏面用折半查找的方法找等於iValue的值,找到返回1否則0iCount是元素個數

 

9.統計一個字符串中字符出現的次數

 

10.100位以上的超大整數的加法(主要考慮數據結構和加法的實現)

 

11、.對如下電文:"CASTCASTSATATATASA"給出Huffman編碼。

 

12、.int (* (*f)(int, int))(int)表示什麼含義?

 

13、.x=x+1x+=1x++,爲這三個語句的效率排序。並說明爲什麼。

 

14、中綴表達式 A-(B+C/D)*E的後綴形式是什麼?

 

15、struct S1

{ char c;

int i;

};

sizeof(S1) = ?

class X{

public:

X();

virtual ~X();

void myMemberFunc();

static void myStaticFunc();

virtual void myVirtualFunc();

private:

int i;

char * pstr;

char a;

}

sizeof(X) = ?

 

16、找出兩個字符串中最大子字符串,"abractyeyt","dgdsaeactyey"的最大子串爲"acty

et"

 

17、有一百個整數,其中有負數,找出連續三個數之和最大的部分.

 

18、寫一程序實現快速排序. 假設數據輸入爲一文件

快速算法描述如下

Algorithm Partition

Input: sequence a0, ..., an-1 with n elements

Output: permutation of the sequence such that all elements a0, ..., aj are les

s than or equal to all

elements ai, ..., an-1 (i > j)

Method:

 

choose the element in the middle of the sequence as comparison element x

let i = 0 and j = n-1

while ij

 

    search the first element ai which is greater than or equal to x

    search the last element aj which is less than or equal to x

    if ij

 

    exchange ai and aj

    let i = i+1 and j = j-1

After partitioning the sequence, Quicksort treats the two parts recursively by

 the same procedure.

The recursion ends whenever a part consists of one element only.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章