面试题(1)

【1】设置或者清除某位。

示例代码如下:

 1 #include<iostream>
 2 using namespace std;
 3 
 4 #define  BIT3  (0x1 << 3)
 5 
 6 void Set_bit3(int &a)
 7 {
 8     a |= BIT3;
 9 }
10 
11 void Clear_bit3(int &a)
12 {
13     a &= ~BIT3;
14 }
15 
16 void main()
17 {
18     int m = 10;     //1010
19     Set_bit3(m);  
20     cout << m << endl; //1010 == 10
21     Clear_bit3(m);
22     cout << m << endl; //0010 == 2
23     
24     int n = 7;     //0111
25     Set_bit3(n);
26     cout << n << endl; //1111 == 15
27     Clear_bit3(n);
28     cout << n << endl; //0111 == 7
29 
30     int s = 3;     //0011
31     Set_bit3(s);
32     cout << s << endl; //1011 == 11
33     Clear_bit3(s);
34     cout << s << endl; //0111 == 3
35 
36     system("pause");
37 }
38 // run out
39 /*
40 10
41 2
42 15
43 7
44 11
45 3
46 请按任意键继续. . .
47  */

【2】指针引用经典笔试题

(1)

 1 #include<malloc.h>
 2 #include<string.h>
 3 
 4 void GetMemory(char *p, int num)
 5 {
 6     p = (char *)malloc(sizeof(char) * num);
 7 }
 8 
 9 void  main()
10 {
11     char *str = NULL;
12     GetMemory(str, 100);
13     strcpy(str, "hello");
14 }
15 
16 // GetMemory函数仅仅只改变了p指针的指向,而str的指向不变
17 // 类似于:
18 // (1) char  *str = NULL;
19 // (2) char  *p = str;
20 // (3) p = (char *)malloc(sizeof(char) * num);
21 // 那么这样的三行代码执行结果显然没有达到改变str的作用

(2)

 1 #include<string.h>
 2 #include<malloc.h>
 3 #include<stdio.h>
 4 
 5 void  GetMemory2(char **p, int num)
 6 {
 7     *p = (char *)malloc(sizeof(char) * num);
 8 }
 9 
10 void main()
11 {
12     char *str = NULL;
13     GetMemory2(&str, 100);
14     strcpy(str, "hello");
15     printf("%s", str);
16     free(str);
17     getchar();
18 }
19 
20 // GetMemory2函数“隔山打牛”
21 // 类似于:
22 // char  *str = NULL;
23 // char  **p = &str;
24 // *p = (char *)malloc(sizeof(char) * num);
25 // 那么这样的三行代码执行结果显然达到改变str的作用

(3)

 1 #include<string.h>
 2 #include<malloc.h>
 3 #include<stdio.h>
 4 
 5 char * GetMemory3(int num)
 6 {
 7     char *p = (char *)malloc(sizeof(char) * num);
 8     return p;
 9 }
10 
11 void  main()
12 {
13     char *str = NULL;    
14     str = GetMemory3(100);
15     strcpy(str, "hello");
16     printf("%s", str);
17     free(str);
18     getchar();
19 }
20 
21 // GetMemory3函数功在当代,利在千秋
22 // 尽管p变量在栈中,它当生则生,当死则死
23 // 但是它在世的一瞬间,却创造了举世的成就,开天辟地!!
24 // p = (char *)malloc(sizeof(char) * num);一句足矣
25 // 返回p的值,意义无量!

(4)

 1 #include<string.h>
 2 #include<malloc.h>
 3 #include<stdio.h>
 4 
 5 char *GetMemory4()
 6 {
 7     char p[] = "hello  world";
 8     return p;  //编译器警告!返回局部变量
 9 }
10 
11 void  main()
12 {
13     char *str = NULL;
14     str = GetMemory4();
15     printf("%s", str);
16 }
17 
18 // GetMemory4函数山寨版
19 // p变量在栈中,当生则生,当死则死
20 // 但是它在世的一瞬间,却目光短浅,于后世无功!
21 // 返回p的值,一文不值!

(5)

 1 #include<string.h>
 2 #include<malloc.h>
 3 #include<stdio.h>
 4 
 5 char * GetMemory5()
 6 {
 7     char *p = "hello  world!";
 8     return p;
 9 }
10 
11 void  main()
12 {
13     char *str = NULL;
14     str = GetMemory5();
15     printf("%s", str);
16     getchar();
17 }
18 
19 // GetMemory5函数正版
20 // p变量在栈中,当生则生,当死则死
21 // 但是它在世的一瞬间,却赋予了一份常量,不随它的消失而泯灭!

(6)

 1 #include<string.h>
 2 #include<malloc.h>
 3 #include<stdio.h>
 4 
 5 void GetMemory6(char *&p)
 6 {
 7     p = (char *)malloc(sizeof(char) * 10);
 8 }
 9 
10 void  main()
11 {
12     char *str = NULL;
13     GetMemory6(str);
14     strcpy(str, "hello");
15     printf("%s", str);
16     free(str);
17     str = NULL;
18     getchar();
19 }

以上几个例子是面试时遇到的最频繁的试题,在此特意备份,以便学习。

【3】这道题是最典型的数组越界示例:

 1 #include<iostream.h>
 2 
 3 #define MAX  255
 4 
 5 void main()
 6 {
 7     unsigned char A[MAX];
 8     for (int i = 0; i <= MAX; i++)
 9     {
10         A[i] = i;
11     }
12 }

无限循环.......

【4】求最大字段和

示例代码如下:

 1 #include<iostream>
 2 using namespace std;
 3 
 4 inline int Max(int a, int b)
 5 {
 6     return a > b ? a : b; 
 7 }
 8 
 9 int MaxSubSum(int br[], int n)
10 {
11     int data = 0, max = 0;
12     for (int i = 0; i < n; ++i)
13     {
14         data = Max(0, br[i]);       
15         max = Max(data + max, max); 
16     }
17     return max;
18 }
19 
20 void main()
21 {
22     int ar[] = {1, -4, -2, -1, 7, -3, 9};
23     int result1 = MaxSubSum(ar, 7);
24     cout << result1 << endl;  //17
25 }

【5】字节对齐

示例代码:

  1 #include<iostream>
  2 using namespace std;
  3 
  4 class A
  5 {
  6 public:
  7   int i;
  8 };
  9 class  B
 10 {
 11 public:
 12   char  ch;
 13 };
 14 class C
 15 {
 16 public:
 17     int i;
 18     short j;
 19 };
 20 class D
 21 {
 22 public:
 23     int i;
 24     short j;
 25     char ch;
 26 };
 27 class E
 28 {
 29 public:
 30     int i;
 31     int ii;
 32     short j;
 33     char ch;
 34     char chr;
 35 };
 36 class F
 37  {
 38 public:
 39     int i;
 40     int ii;
 41     int iii;
 42     short j;
 43     char ch;
 44     char chr;
 45 };
 46 class G
 47 {
 48 public:
 49     int i;
 50     int ii;
 51     short j;
 52     char ch;
 53     char chr;
 54     int iii;
 55 };
 56 class H
 57 {
 58 public:
 59     int i;
 60     int ii;
 61     char ch;
 62     char chr;
 63     int iii;
 64     short j;
 65 };
 66 class I
 67 {
 68 public:
 69     int i;
 70     char ch;
 71     int ii;
 72     char chr;
 73     int iii;
 74     short j;
 75 };
 76 void  main()
 77 {
 78     cout << "sizeof(int): " << sizeof(int) << endl;
 79     cout << "sizeof(short): " << sizeof(short) << endl;
 80     cout << "sizeof(char): " << sizeof(char) << endl;
 81     cout << endl;
 82     cout << "sizeof(A): " << sizeof(A) << endl;
 83     cout << "sizeof(B): " << sizeof(B) << endl;
 84     cout << "sizeof(C): " << sizeof(C) << endl;
 85     cout << "sizeof(D): " << sizeof(D) << endl;
 86     cout << "sizeof(E): " << sizeof(E) << endl;
 87     cout << "sizeof(F): " << sizeof(F) << endl;
 88     cout << "sizeof(G): " << sizeof(G) << endl;
 89     cout << "sizeof(H): " << sizeof(H) << endl;
 90     cout << "sizeof(I): " << sizeof(I) << endl;
 91     system("pause");
 92 }
 93 
 94 //run out:
 95 /*
 96 sizeof(int): 4
 97 sizeof(short): 2
 98 sizeof(char): 1
 99 
100 sizeof(A): 4
101 sizeof(B): 1
102 sizeof(C): 8
103 sizeof(D): 8
104 sizeof(E): 12
105 sizeof(F): 16
106 sizeof(G): 16
107 sizeof(H): 20
108 sizeof(I): 24
109 请按任意键继续. . .
110 */

【6】大小端判断

题目:请写一个C函数,若处理器是Big_endian的,则返回0;若是Little_endian的,则返回1

分析:
为什么会有大小端模式之分呢?这是因为在计算机系统中,我们是以字节为单位的,每个地址单元都对应着一个字节,一个字节为 8bit。

但是在C语言中除了8bit的char之外,还有16bit的short型,32bit的long型(要看具体的编译器),

另外,对于位数大于 8位的处理器,例如16位或者32位的处理器,由于寄存器宽度大于一个字节,那么必然存在着一个如何将多个字节安排的问题。

因此就导致了大端存储模式和小端存储模式。

例如一个16bit的short型x,在内存中的地址为0x0010,x的值为0x1122,那么0x11为高字节,0x22为低字节。

对于大端模式,就将0x11放在低地址中,即0x0010中,0x22放在高地址中,即0x0011中。

小端模式,刚好相反。我们常用的X86结构是小端模 式,而KEIL C51则为大端模式。

嵌入式开发对大小端都比较敏感。所谓的大端模式是指:

数据的低位(就是权值较小的后面那几位)保存在内存的高地址中,而数据的高位,保存在内存的低地址中。

这样的存储模式有点儿类似于把数据当作字符串顺序处理:地址由小向大增加,而数据从高位往低位放;

所谓的小端模式是指:数据的低位保存在内存的低地址中,而数据的高位保存在内存的高地址中,

这种存储模式将地址的高低和数据位权有效地结合起来,高地址部分权值高,低地址部分权值低,和我们的实际逻辑方法一致。

示例代码如下:

 1 #include<iostream>
 2 using namespace std;
 3 
 4 int CheckCpu()
 5 {
 6     union w
 7     {
 8         int a;
 9         char b;
10     }c;
11     c.a = 1;
12     return (c.b == 1);
13 }
14 
15 void main()
16 {
17     cout << CheckCpu() << endl;   // 1   //说明是小端模式
18 }

总结:

其实大小端的问题很简单的,就是因为数据在同样的内存可以有两种存储模式。

简单记住:低低小,低高大。

也就是说:低位数据存入低地址小端;低位数据存入高地址大端。

【7】求整型数组中的最小以及次小项

示例代码如下:

 1 #include<iostream>
 2 using namespace std;
 3 
 4 void  select(int ar[],int n)
 5  {
 6      int m1, m2;
 7      m1 = m2 = 0xffff;
 8      int x1, x2;
 9      x1 = x2 = 0;
10      for(int j = 0; j < n ;j++)    
11      {    
12          if (ar[j] < m1)
13          {
14              m2 = m1;    //暂存次小
15              x2 = x1;    //暂存次小索引
16              m1 = ar[j]; //暂存最小
17              x1 = j;     //暂存最小索引
18          }
19          else if(ar[j] < m2 )
20          {
21              m2 = ar[j]; //保存次小
22              x2 = j;     //保存次小索引             
23          }
24      }
25      cout << x1 << "   " << m1 << endl;   //输出最小
26      cout << x2 << "   " << m2 << endl;   //输出次小
27  }
28 
29  void  main()
30  {
31      int ar[5] = {1, 7, 5, 4, 2};
32      select(ar, 5);
33      system("pause");
34  }
35 
36  /* run out:
37  0   1
38  4   2
39  */

 

Good Good Study, Day Day Up.

顺序  选择  循环  坚持

posted @ 2013-01-04 20:26  kaizenly  阅读(1704)  评论(2编辑  收藏  举报
打赏