整理一些C/C++語言筆試題目

來源:互聯網
上載者:User

僅供參考,免責聲明。

1. 判斷一個整數是否是2的N次方

#include "stdafx.h"#include "stdio.h"bool IsPowerOfTwo(int x){    return (x > 0) && ((x & (x - 1)) == 0);}void main(){int a = 0;bool result = false;printf("Input a integer:");scanf("%d",&a);result = IsPowerOfTwo(a);printf("%s\n",result ? "true":"false");}

2. 關於傳值調用

#include "stdafx.h"#include "stdio.h"void Swap(int a, int b){int temp;temp = a;b = a;a = temp;}void main(){int a = 3, b = 5;Swap(a, b);printf("a = %d,b = %d\n", a, b);}

題目簡單,知識點:函數調用中發生的資料傳送是單向的。 即只能把實參的值傳送給形參,而不能把形參的值反向地傳送給實參。 因此在函數調用過程中,形參的值發生改變,而實參中的值不會變化。

所以結果是a = 3,b = 5

需要修改的話,傳引用調用即可。

同樣的題目還有:

#include "stdafx.h"#include "stdio.h"void test(char *p){p+=3;}int main(){char *p = "hello";printf("%s-",p);test(p);printf("%s\n",p);return 0;}

同樣答案為 hello-hello

3. 將字串逆序

#include "stdafx.h"#include "string.h"void Swap(char &a, char &b){a = a ^ b;b = a ^ b;//(a ^ b) ^ b = aa = a ^ b;//(a ^ b) ^ a = b}void Reverse(char *str){unsigned int length;unsigned int i = 0;length = strlen(str);//printf("[debug]string length is %d\n",length);for(i = 0; i < length / 2; i++){//XORSwap(str[i], str[length - i - 1]);}}void main(){char string[200];//不要用char *str="hello"; 這樣直接存在常量區了。printf("Input String:\n");gets(string);Reverse(string);printf("After reverse:\n%s\n",string);}

4. 用二進位表示int型負數 (例 -1,-5)

概念:
1、原碼:一個正數,按照絕對值大小轉換成的位元;一個負數按照絕對值大小轉換成的位元,然後最高位補1,稱為原碼。
比如 00000000 00000000 00000000 00000101 是 5的 原碼。
     10000000 00000000 00000000 00000101 是 -5的 原碼。
2、反碼:正數的反碼與原碼相同,負數的反碼為對該數的原碼除符號位外各位取反[每一位取反(除符號位)]。
取反操作指:原為1,得0;原為0,得1。(1變0; 0變1)
比如:正數00000000 00000000 00000000 00000101  的反碼還是 00000000 00000000 00000000 00000101
      負數10000000 00000000 00000000 00000101  的反碼則是 11111111 11111111 11111111 11111010。
3、補碼:正數的補碼與原碼相同,負數的補碼為對該數的原碼除符號位外各位取反,然後在最後一位加1.
比如:
10000000 00000000 00000000 00000101 的補碼是:11111111 11111111 11111111 11111010。
那麼,補碼為:
11111111 11111111 11111111 11111010 + 1 = 11111111 11111111 11111111 11111011
所以,-5 在電腦中表達為:11111111 11111111 11111111 11111011。轉換為十六進位:0xFFFFFFFB
我們來看整數-1在電腦中如何表示。假設這也是一個int類型,那麼:
1、先取-1的原碼:10000000 00000000 00000000 00000001
2、得反碼:     11111111 11111111 11111111 11111110(除符號位按位取反)
3、得補碼:     11111111 11111111 11111111 11111111

可見,-1在電腦裡用二進位表達就是全1。16進位為:0xFFFFFF

5. 用C++函數模版寫三個數中的最大數(int,double,long為例)

備忘:尼瑪,模版都不會用?翻翻書去!

#include "stdafx.h"#include "iostream.h"template <typename T>T max(T num1,T num2,T num3){T max = 0;max = num1 > num2 ? num1:num2;max = max  > num3 ? max:num3;return max;}void main(){int i1 = -123, i2 = 0, i3 = 321, i_max;double d1= -11.12, d2 = 0.03, d3 = 14.14, d_max;long l1 = -890, l2 = 0, l3 = 67849321, l_max;i_max = max(i1, i2, i3);d_max = max(d1, d2, d3);l_max = max(l1, l2, l3);cout<<"i_max = "<<i_max<<endl;cout<<"d_max = "<<d_max<<endl;cout<<"l_max = "<<l_max<<endl;}

6.儲存類型auto,static,extern,register的區別

下面這位仁兄寫了很多。

http://blog.csdn.net/firefly_2002/article/details/7940802

7. 2的N次方引出的。

#include "stdafx.h"#include "iostream.h"int count = 0;int x = 2007;void main(){while(x){count++;x = x&(x-1);printf("x = %d\n",x);}printf("count = %d\n",count);}

count的值是多少?

x &(x-1)這個很熟悉吧。。。2的N次方就這樣判斷的。

x = x&(x-1)

會慢慢減小,能發現其中規律。實際測試printf列印內容x值為2006,2004,2000,1984,1920,1792,1536,1024,0 count值為9

x = x &(x-1)會逐漸減小到離它近的2的N次方數。

(2007-1024)= 983 = 512 + 256 + 128 + ..... + 1 = 2^0 + ... + 2^9  , count = 9;

有沒有更清晰的解法,請回複,我這是列印出結果才分析的。

8. char *str和char str[]區別
#include "stdafx.h"#include<iostream>char *str(){char *str="hejk ";return str;}void main(){str();}
#include "stdafx.h"#include<iostream>char *str(){char str[]="hejk ";return str;}void main(){str();}

其中程式二編譯時間就會報錯,warning C4172: returning address of local variable or temporary程式一是存在常量區的,程式二是存在棧上需要補充知識點一、預備知識----程式的記憶體配置

一個由C/C++編譯的程式佔用的記憶體分為以下幾個部分

1、棧區(stack):由編譯器自動分配釋放,存放函數的參數值,局部變數的值等。其操作方式類似於資料結構中的棧。
2、堆區(heap):一般由程式員分配釋放,若程式員不釋放,程式結束時可能由OS回收。
3、全域區(靜態區)(static):全域變數和靜態變數的儲存是放在一塊的,初始化的全域變數和靜態變數在一塊地區;未初始化的全域變數和未初始化的靜態變數在相鄰的另一塊地區。程式結束後由系統釋放。
4、文字常量區:常量字串就是放在這裡的。程式結束後由系統釋放。
5、程式碼區下面有個例子:

//main.cppint a=0;    //全域初始化區char *p1; //全域未初始化區main(){    int b;                  //棧    char s[]="abc";  //棧    char *p2;           //棧    char *p3="123456";   //123456\0在常量區,p3在棧上。    static int c=0;     //全域(靜態)初始化區    p1 = (char*)malloc(10);    p2 = (char*)malloc(20);   //分配得來得10和20位元組的地區就在堆區。    strcpy(p1,"123456");   //123456\0放在常量區,編譯器可能會將它與p3所向"123456"最佳化成一個地方。}

9. 如何判斷CPU是大端還是小端?

#include "stdafx.h"#include <stdio.h>union u_T{short s;char c[2];};int main(){u_T u;if(sizeof(short) == 2){u.s = 0x0102;if(u.c[0] == 1 && u.c[1] == 2){printf("big enidan\n");}else if(u.c[0] == 2 && u.c[1] == 1){printf("little endian.\n");}return 0;}return -1;}

10. 進棧出棧摘自:http://zhidao.baidu.com/question/14053845.html設棧S的初始狀態為空白,元素a,b,c,d,e,f,g依次入棧,以下出棧序列不可能出現的是( )。
A.a,b,c,e,d,f,g  B.b,c,a,f,e,g,d  C.a,e,d,c,b,f,g
D.d,c,f,e,b,a,g  E.g,e,f,d,c,b,a
答案是E。標準答案講得很好,依次進棧的意思說明不是一次性進完,是有進進出出的。(不要邪惡!)棧的定義:棧是一種特殊的表這種表只在表頭進行插入和刪除操作。因此,表頭對於棧來說具有特殊的意義,稱為棧頂。相應地,表尾稱為棧底。不含任何元素的棧稱為空白棧。 
棧的邏輯結構:假設一個棧S中的元素為an,an-1,..,a1,則稱a1為棧底元素,an為棧頂元 素。棧中的元素按a1 ,a2,..,an-1,an的次序進棧。在任何時候,出棧的元素都是棧頂元素。換句話說,棧的修改是按後進先出的原則進行的.因此,棧又稱為後進先出(Last In First Out)表,簡稱為LIFO表。所以,只要問題滿足LIFO原則,就可以使用棧。 
notice:換句話說,棧就是可以一個元素進後,可以接著進行輸出的表.
這道題各個選項的進出次序為:
A:進,出,進,出,進,出,進,進,出,出,進,出,進,出
B:進,進,出,進,出,出,進,進,進,出,出,進,出,出
C:進,出,進,進,進,進,出,出,出,出,進,出,進,出
D:進,進,進,進,出,出,進,進,出,出,出,出,進,出
E:錯誤.原因自己仿照上面做做看.

11.有N階的台階,一個人每次只能走一階或兩階,用一個遞迴演算法求出共有多少種走法。參考http://2274594.blog.51cto.com/2264594/422225假設共有i階台階, 走完所有的台階有n種走法,則:
1. i  = 1時,  n = 1;   {1}
2. i =  2,       n = 2;   {[1, 1],  [2]}
3. i =  3,       n = 3;   {[1, 1, 1],  [1, 2], [2, 1]}
4. i =  4,       n = 5;   {[1, 1, 1, 1], [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2]}
5. i =  5,       n = 8;    ............
.........................................
.........................................
因此,我們可以得到規律
f(n) = f(n-1) + f(n-2)  (n>=3), 從而得到遞迴演算法。 12.sizeof和strlen,今天栽跟頭的地方。
#include <stdio.h>#include <string.h>void test0(const char *p){printf("%d ",sizeof(p));printf("%d ",strlen(p));}void test1(char p[]){printf("%d ",sizeof(p));printf("%d ",strlen(p));}void main(){char *s = NULL;char str[]="1234567";printf("%d ",sizeof(s));printf("%d ",sizeof(str));printf("%d ",strlen(str));printf("%d ",sizeof((const char*)str) );printf("%d ",strlen((const char*)str) );test0(str);test1(str);}

結果是4 8 7 4 7 4 7 4 7

請自行分析。 13. 結構體的SIZE
struct test_T{        char test0;        char *p;        int int_32_1;        int int_32_2;        char test1;        char test2;        int int_32_3;}t1;

請問sizeof(t1)為多少?在GCC預設參數配置前提下。使用預設參數測試是24.

struct test_T{        char test0;     //4        char *p;        //4        int int_32_1;   //4        int int_32_2;   //4        char test1;        char test2;     //一共4        int int_32_3;   //4}t1;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.