C/C++筆試題(編程題)


面試過程中遇到的編程題整理,於此備錄。分享,共勉。(持續更新中......歡迎補充)

(1)用戶輸入M, N值,從1至N開始順序循環數數,每數到M輸出該數值,直至全部輸出。寫出C程序。

程序代碼如下:

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 // 節點結構定義
  5 typedef struct Link_Node
  6 {
  7     int data;
  8     Link_Node* next;
  9 }Node, *pNode;
 10 
 11 // 創建循環鏈表
 12 void CreateList(pNode& head, pNode& tail, int n)
 13 {
 14     if (n < 1)
 15     {
 16         head = NULL;
 17         return;
 18     }
 19 
 20     head = (pNode)malloc(sizeof(Node));
 21     head->data = 1;
 22     head->next = NULL;
 23     
 24     pNode p = head;
 25     for (int i = 2; i < n+1; ++i)
 26     {
 27         p->next = (pNode)malloc(sizeof(Node));
 28         p = p->next;
 29         p->data = i;
 30         p->next = NULL;
 31     }
 32     
 33     tail = p;
 34     p->next = head;
 35 }
 36 
 37 // 打印循環鏈表
 38 void Print(pNode& head)
 39 {
 40     pNode p = head;
 41     while (p != NULL && p->next != head)
 42     {
 43         printf("%d ", p->data);
 44         p = p->next;
 45     }
 46     if (p != NULL)
 47     {
 48         printf("%d\n", p->data);
 49     }
 50 }
 51 
 52 // 用戶輸入M, N值,從1至N開始順序循環數數,每數到M輸出該數值。
 53 // 直至全部輸出
 54 void LoopPrint(pNode& head, pNode& tail, int m)
 55 {
 56     pNode pPre = tail, pCur = head;
 57     
 58     int nCount = m - 1;
 59     while (pCur != NULL && pCur != pCur->next)
 60     {
 61         if (nCount > 0)
 62         {
 63             nCount--;
 64             pPre = pCur;
 65             pCur = pCur->next;
 66         }
 67         else
 68         {
 69             pPre->next = pCur->next;
 70             printf("%d ", pCur->data);
 71             free(pCur);
 72 
 73             pCur = pPre->next;
 74             nCount = m - 1;
 75         }    
 76     }
 77     
 78     if (pCur != NULL)
 79     {
 80         printf("%d ", pCur->data);
 81         free(pCur);
 82 
 83         head = tail = NULL;
 84     }
 85 
 86     printf("\n");
 87 }
 88 
 89 void main()
 90 {
 91     pNode head = NULL, tail = NULL;
 92     int m = 0, n = 0;
 93     printf("請輸入m,n的值:\n");
 94     scanf("%d", &m);
 95     scanf("%d", &n);
 96     // 創建循環鏈表
 97     CreateList(head, tail, n);
 98     // 打印鏈表
 99     printf("打印鏈表數據信息如下:\n");
100     Print(head);
101     printf("\n");
102     // 循環輸出
103     printf("循環數數,遇到M輸出結果如下:\n");
104     LoopPrint(head, tail, m);
105     system("pause");
106 }
107 // run out:
108 /*
109 請輸入m,n的值:
110 2
111 10
112 打印鏈表數據信息如下:
113 1 2 3 4 5 6 7 8 9 10
114 
115 循環數數,遇到M輸出結果如下:
116 2 4 6 8 10 3 7 1 9 5
117 請按任意鍵繼續. . .
118 */

(2)從鍵盤輸入10個學生的學號和成績,按成績從大到小建立一個有序鏈表,並輸出。

程序代碼如下:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 typedef struct node
 5 {
 6     int xh;
 7     int cj;
 8     struct node *next;
 9 }Node, *pNode;
10 
11 void main()
12 {
13     pNode head = NULL, s, p, pre;
14     
15     int i = 0;
16     while (i++ < 10)
17     {
18         s = (pNode)malloc(sizeof(Node));
19         s->next = NULL;
20         printf("第%d個學生(學號 成績):", i);
21         scanf("%d%d", &s->xh, &s->cj);
22         if (head == NULL)
23         {
24             head = s;  // 第一個學生
25         }
26         else
27         {
28             p = head;
29             pre = p;
30             while ((p != NULL) && (s->cj < p->cj))
31             {
32                 pre = p;
33                 p = p->next;
34             }
35             if (p == head)
36             {
37                 s->next = head;
38                 head = s;
39             }
40             else if (p == NULL)   
41             {
42                 pre->next = s;
43             }
44             else
45             {
46                 s->next = pre->next;
47                 pre->next = s;
48             }
49         }
50     }
51 
52     printf("\n 輸出結果: \n");
53     p = head;
54     while (p != NULL)
55     {
56         printf("(%d)-->%d \n", p->xh, p->cj);
57         p = p->next;
58     }
59 
60     system("pause");
61 }
62 // run out:
63 /*
64 第1個學生(學號 成績):1 69
65 第2個學生(學號 成績):2 89
66 第3個學生(學號 成績):3 59
67 第4個學生(學號 成績):4 100
68 第5個學生(學號 成績):5 68
69 第6個學生(學號 成績):6 85
70 第7個學生(學號 成績):7 82
71 第8個學生(學號 成績):8 91
72 第9個學生(學號 成績):9 72
73 第10個學生(學號 成績):10 80
74 
75  輸出結果:
76 (4)-->100
77 (8)-->91
78 (2)-->89
79 (6)-->85
80 (7)-->82
81 (10)-->80
82 (9)-->72
83 (1)-->69
84 (5)-->68
85 (3)-->59
86 請按任意鍵繼續. . .
87 */

(3)利用無序數組元素構建一個有序單鏈表。

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 typedef struct node
 5 {
 6     int data;
 7     struct node *next;
 8 }Node, *pNode;
 9 
10 void main()
11 {
12     pNode head = NULL, s, p, pre;
13     // 構建有序鏈表
14     int nArray[10] = {23, 45, 12, 89, 65, 90, 32, 100, 7, 45};
15     for (int i = 0; i < 10; ++i)
16     {
17         s = (pNode)malloc(sizeof(Node));
18         s->data = nArray[i];
19         s->next = NULL;
20         
21         if (head == NULL)
22         {
23             head = s;
24         }
25         else
26         {
27             p = head;
28             pre = p;
29             while ((p != NULL) && (s->data < p->data))
30             {
31                 pre = p;
32                 p = p->next;
33             }
34 
35             if (p == head)
36             {
37                 s->next = head;
38                 head = s;
39             }
40             else if (p == NULL)   
41             {
42                 pre->next = s;
43             }
44             else
45             {
46                 s->next = pre->next;
47                 pre->next = s;
48             }
49         }
50     }
51 
52     printf("輸出結果: \n");
53     p = head;
54     while (p != NULL)
55     {
56         printf("%d \n", p->data);
57         p = p->next;
58     }
59 
60     system("pause");
61 }
62 // run out
63 /*
64 輸出結果:
65 100
66 90
67 89
68 65
69 45
70 45
71 32
72 23
73 12
74 7
75 請按任意鍵繼續. . .
76 */

(4)寫一個函數找出一個整數數組中,第二大的數 (microsoft)

程序代碼如下:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 const int MINNUMBER = -32767; 
 5 
 6 int find_sec_max(int data[], int count) 
 7 { 
 8     int maxnumber = data[0]; 
 9     int sec_max = MINNUMBER; 
10     for (int i = 1; i < count; i++) 
11     { 
12         if (data[i] > maxnumber) 
13         { 
14             sec_max = maxnumber; 
15             maxnumber = data[i]; 
16         } 
17         else 
18         { 
19             if (data[i] > sec_max) 
20                 sec_max = data[i]; 
21         } 
22     }
23 
24     return sec_max; 
25 }
26 
27 void main()
28 {
29     int nArray[10] = {23, 1, 45, 1000, 990, 7, 89, 34, 45, 70};
30     cout << find_sec_max(nArray, 10) << endl;
31     system("pause");
32 }
33 // run out:
34 /*
35 990
36 請按任意鍵繼續. . .
37 */

(5)求整型數組中的最小以及次小項

參見隨筆《面試題(1)-->【7】

(6)如何判斷一個單鏈表是有環的?(注意不能用標志位,最多只能用兩個額外指針)

程序代碼如下:

 1 // 方法1:
 2 bool checkLoop(node * head) 
 3 { 
 4     if (NULL == head) 
 5         return false;
 6 
 7     node *low = head;
 8     node *fast = head->next; 
 9     while (fast != NULL && fast->next != NULL) 
10     { 
11         low = low->next; 
12         fast = fast->next->next; 
13         if (low == fast) 
14             return true; 
15     }
16 
17     return false;
18 }
19 
20 // 方法2:
21 bool IsLoop(node *head)
22 {
23     if (NULL == head || NULL == head->next)
24     {
25         return false;
26     }
27 
28     node *p1 = head;
29     node *p2 = head;
30     do
31     {
32         p1 = p1->next;
33         p2 = p2->next->next;
34     } while (p2 && p2->next && p1 != p2);
35     
36     return (p1 == p2);
37 }

(7)字符串功能函數

參見隨筆《字符串strcpy

參見隨筆《字符串strlen

參見隨筆《字符串strcat

參見隨筆《字符串strcmp

參見隨筆《字符串memcpy

(8)字符串函數集合

1、字符數組的環形移動如何實現?

2、如何判斷一個字符串是否是回文串?

3、如何把數字字符串轉換為整型數據?

4、如何把整型數據轉換為字符串?

5、如何對字符串進行排序?

6、如何把字符串中某個指定的字符刪除?

7、如何找出01字符串中0與1出現的最大次數?

8、如何從字符串的某一個位置刪除指定個數的字符?

9、寫一個函數把字符串反轉

10、寫一個函數查找兩個字符串中的第一個公共字符串

11、寫一個函數在字符串N中查找第一次出現子串M的位置。

12、寫一個函數把字符串A中的B字符子串用字符串C進行替換。

(9)c語言 文件讀寫代碼

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 void main()
 5 {
 6     FILE *fp;
 7     char ch, filename[10];
 8     scanf("%s", filename);
 9     if ((fp = fopen(filename, "w")) == NULL)
10     {
11         printf("cann't open file\n");
12         exit(0);
13     }
14     ch = getchar();
15     while (ch != '#')
16     {
17         fputc(ch, fp);
18         putchar(ch);
19         ch = getchar();
20     }
21 
22     fclose(fp);
23     system("pause");
24 }

(10)memcpy內存拷貝函數

參見隨筆《字符串memcpy

(11)判斷大小端模式。

程序代碼如下:

 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     system("pause");
19 }

大小端模式分析:

嵌入式系統開發者應該對Little-endian和Big-endian模式非常了解。

采用Little-endian模式的CPU對操作數的存放方式是從低字節到高字節。而Big-endian模式對操作數的存放方式是從高字節到低字節。

例如,16bit寬的數0x1234在Little-endian模式CPU內存中的存放方式(假設從地址0x4000開始存放)為:

內存地址 存放內容

0x4000 0x34

0x4001 0x12

而在Big-endian模式CPU內存中的存放方式則為:

內存地址 存放內容

0x4000    0x12

0x4001    0x34

32bit寬的數0x12345678在Little-endian模式CPU內存中的存放方式(假設從地址0x4000開始存放)為:

內存地址 存放內容

0x4000    0x78

0x4001    0x56

0x4002    0x34

0x4003    0x12

而在Big-endian模式CPU內存中的存放方式則為:

內存地址 存放內容

0x4000    0x12

0x4001    0x34

0x4002    0x56

0x4003    0x78

聯合體union的存放順序是所有成員都從低地址開始存放,面試者的解答利用該特性,輕松地獲得了CPU對內存采用Little-endian還是Big-endian模式讀寫。

(12)寫一個宏,求結構體中成員變量的偏移量。

 1 #include<iostream>
 2 #include<cstddef>
 3 using namespace std;
 4 
 5 #define  offset(s, a)   ((int)(&(((s *)0)->a)))
 6 
 7 struct  s
 8 {
 9     int  a;
10     char d;
11     int  b;
12     char c;
13 };
14 
15 void main()
16 {
17     cout << offset(s, a) << endl;  // 0
18     cout << offset(s, b) << endl;  // 8
19     cout << offset(s, c) << endl;  // 12
20     cout << offset(s, d) << endl;  // 4
21     system("pause");
22 }
23 // run out:
24 /*
25 0
26 8
27 12
28 4
29 請按任意鍵繼續. . .
30 */

解析圖如下:

(13)用戶輸入兩個整數,求最大公約數和最大公倍數。

程序代碼如下:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 void main()
 5 {
 6     int  max_divisor, min_multiple;
 7     int  n1, n2, m, n;
 8     cout << "輸入整數 n1 = ";
 9     cin >> n1;
10     cout << "輸入整數 n2 = ";
11     cin >> n2;
12     if (n1 < n2)
13     {
14         swap(n1, n2);
15     }
16     max_divisor = n1;
17     n = n2;
18     while (n != 0)
19     {
20         m = max_divisor % n;
21         max_divisor = n;
22         n = m;
23     }
24     min_multiple = n1 * n2 / max_divisor;
25     cout << "最大的公約數是: " << max_divisor << endl;
26     cout << "最小的公倍數是: " << min_multiple << endl;
27     system("pause");
28 }
29 // run out:
30 /*
31 輸入整數 n1 = 6
32 輸入整數 n2 = 3
33 最大的公約數是: 3
34 最小的公倍數是: 6
35 請按任意鍵繼續. . .
36 */

(14)單鏈表

1、創建有序單鏈表

2、向有序鏈表添加一個新節點

3、求鏈表的中間節點

4、逆置鏈表

5、判斷是否有環

程序代碼如下:

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 typedef struct node
  5 {
  6     int data;
  7     struct node *next;
  8 }Node, *pNode;
  9 
 10 // 打印鏈表數據
 11 void PrintList(pNode head)
 12 {
 13     if (NULL == head || NULL == head->next)
 14         return;
 15 
 16     pNode p = head->next;
 17     while (p != NULL)
 18     {
 19         printf("%d \n", p->data);
 20         p = p->next;
 21     }
 22 }
 23 
 24 // 查找鏈表的中間節點
 25 Node* FindMiddleNode(pNode head)
 26 {
 27     int i = 0, j = 0;
 28     pNode current = NULL;
 29     pNode middle = NULL;
 30     current = middle = head->next;
 31     while (current != NULL)
 32     {
 33         if (i/2 > j)
 34         {
 35             ++j;
 36             middle = middle->next;
 37         }
 38         ++i;
 39         current = current->next;
 40     }
 41 
 42     return middle;
 43 }
 44 
 45 void CreateList(pNode& head, int nValue)
 46 {
 47     pNode s, p, pre;
 48     s = (pNode)malloc(sizeof(Node));
 49     s->data = nValue;
 50     s->next = NULL;
 51         
 52     if (NULL == head)
 53     {
 54         head = s;
 55     }
 56     else
 57     {
 58         p = head;
 59         pre = p;
 60         while ((p != NULL) && (s->data < p->data))
 61         {
 62             pre = p;
 63             p = p->next;
 64         }
 65 
 66         if (p == head)
 67         {
 68             s->next = head;
 69             head = s;
 70         }
 71         else if (NULL == p)   
 72         {
 73             pre->next = s;
 74         }
 75         else
 76         {
 77             s->next = pre->next;
 78             pre->next = s;
 79         }
 80     }
 81 }
 82 
 83 pNode Reverse(pNode head)
 84 {
 85    pNode p = NULL, q = NULL;
 86    if (NULL == head || head->next == NULL)
 87    {
 88        return head;
 89    }
 90 
 91    p = head->next;
 92    q = p->next;
 93    p->next = NULL;
 94    while (q != NULL)
 95    {
 96        p = q;
 97        q = q->next;
 98        p->next = head->next;
 99        head->next = p;
100    }
101 
102    return  head;
103 }
104 
105 // 逆置無頭節點的單鏈表
106 /*
107 pNode Reverse(pNode firstNode)
108 {
109    pNode p = NULL, q = NULL;
110    if (NULL == firstNode || firstNode->next == NULL)
111    {
112        return firstNode;
113    }
114 
115    p = firstNode;
116    q = p->next;
117    p->next = NULL;
118    while (q != NULL)
119    {
120        p = q;
121        q = q->next;
122        p->next = firstNode;
123        firstNode = p;
124    }
125 
126    return  firstNode;
127 }
128 */
129 
130 bool IsLoop(pNode headNode)
131 {
132     pNode p1 = headNode;
133     pNode p2 = headNode;
134     if (NULL == headNode || headNode->next == NULL)
135     {
136         return false;
137     }
138     do
139     {
140         p1 = p1->next;
141         p2 = p2->next->next;
142     } while(p2  &&  p2->next && p1 != p2);
143 
144     return (p1 == p2);
145 }
146 
147 // 有序鏈表插入節點
148 pNode Insert_node(pNode head, int nValue)
149 {
150     pNode item = (pNode)malloc(sizeof(Node));
151     item->data = nValue;
152     item->next = NULL;
153 
154     if (NULL == head->next)
155     {
156         head->next = item;
157         return head;
158     }
159 
160     Node *p = head->next;
161     Node *q = NULL;
162     while (p != NULL && (p->data > item->data))
163     {
164         q = p;
165         p = p->next;
166     }
167 
168     if (p == head->next)
169     {
170         item->next = p;
171         head->next = item;
172         return head;
173     }
174 
175     q->next = item;
176     item->next = p;
177     return head;
178 }
179 
180 void main()
181 {
182     // 構建有頭節點的有序鏈表
183     pNode headNode = (pNode)malloc(sizeof(Node));
184     headNode->data = -1;
185     headNode->next = NULL;
186 
187     int nArray[10] = {23, 45, 12, 89, 65, 90, 32, 100, 7, 45};
188     for (int i = 0; i < 10; ++i)
189     {
190         CreateList(headNode->next, nArray[i]);
191     }
192 
193     printf("有序鏈表(由大到小)輸出結果: \n");
194     PrintList(headNode);
195 
196     headNode = Insert_node(headNode, 110);  // 最前面插入
197     headNode = Insert_node(headNode, 30);  // 中間插入
198     headNode = Insert_node(headNode, 5);  // 末尾插入
199 
200     printf("有序鏈表(由大到小)輸出結果: \n");
201     PrintList(headNode);
202 
203     pNode midNode = FindMiddleNode(headNode);
204     if (midNode != NULL)
205     {
206         printf("中間節點的數據值: %d \n", midNode->data);
207     }
208 
209     pNode head = Reverse(headNode);
210     if (head != NULL)
211     {
212         printf("逆置后輸出結果: \n");
213         PrintList(head);
214     }
215 
216     printf("是否有環? %d \n", IsLoop(headNode));
217     system("pause");
218 }
219 // run out
220 /*
221 有序鏈表(由大到小)輸出結果:
222 100
223 90
224 89
225 65
226 45
227 45
228 32
229 23
230 12
231 7
232 有序鏈表(由大到小)輸出結果:
233 110
234 100
235 90
236 89
237 65
238 45
239 45
240 32
241 30
242 23
243 12
244 7
245 5
246 中間節點的數據值: 45
247 逆置后輸出結果:
248 5
249 7
250 12
251 23
252 30
253 32
254 45
255 45
256 65
257 89
258 90
259 100
260 110
261 是否有環? 0
262 請按任意鍵繼續. . .
263 */

(15)字符串長度函數strlen

參見隨筆《字符串(strlen) 》

(16)排序集

1、冒泡排序

2、選擇排序

3、插入排序

4、快速排序

5、希爾排序

6、堆排序

7、歸並排序

8、桶排序

9、基數排序

(17)寫一個函數返回1 + 2 + 3 +…+ n的值(假定結果不會超過長整型變量的范圍)

程度代碼如下:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int Sum(int n)
 5 {
 6     return ((long)1 + n) * n / 2;
 7 }
 8 
 9 void main()
10 {
11     printf("%d\n", Sum(10)); // 55
12     system("pause");
13 }

(18)合並有序鏈表,並且合並后仍然為有序鏈表

程序代碼如下:

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 typedef struct node
  5 {
  6     int data;
  7     struct node *next;
  8 }Node, *pNode;
  9 
 10 // 創建鏈表
 11 void CreateList(pNode& head, int nValue)
 12 {
 13     pNode s, p, pre;
 14     s = (pNode)malloc(sizeof(Node));
 15     s->data = nValue;
 16     s->next = NULL;
 17         
 18     if (NULL == head)
 19     {
 20         head = s;
 21     }
 22     else
 23     {
 24         p = head;
 25         pre = p;
 26         while ((p != NULL) && (s->data > p->data))
 27         {
 28             pre = p;
 29             p = p->next;
 30         }
 31 
 32         if (p == head)
 33         {
 34             s->next = head;
 35             head = s;
 36         }
 37         else if (NULL == p)   
 38         {
 39             pre->next = s;
 40         }
 41         else
 42         {
 43             s->next = pre->next;
 44             pre->next = s;
 45         }
 46     }
 47 }
 48 
 49 // 合並有序鏈表
 50 pNode Merge(pNode head1, pNode head2)
 51 {
 52     if (NULL == head1)
 53         return head2;
 54     if (NULL == head2)
 55         return head1;
 56 
 57     pNode head = NULL;
 58     pNode p1 = NULL;
 59     pNode p2 = NULL;
 60     if (head1->data < head2->data)
 61     {
 62         head = head1;
 63         p1 = head1->next;
 64         p2 = head2;
 65     }
 66     else
 67     {
 68         head = head2;
 69         p2 = head2->next;
 70         p1 = head1;
 71     }
 72     pNode pcurrent = head;
 73     while (p1 != NULL && p2 != NULL)
 74     {
 75         if (p1->data <= p2->data )
 76         {
 77             pcurrent->next = p1;
 78             pcurrent = p1;
 79             p1 = p1->next;
 80         }
 81         else
 82         {
 83             pcurrent->next = p2;
 84             pcurrent = p2;
 85             p2 = p2->next;
 86         }
 87     }
 88 
 89     if (p1 != NULL)
 90         pcurrent->next = p1;
 91 
 92     if (p2 != NULL)
 93         pcurrent->next = p2;
 94 
 95     return head;
 96 }
 97 
 98 // 遞歸合並有序鏈表
 99 pNode MergeRecursive(pNode head1, pNode head2)
100 {
101     if (NULL == head1)
102         return head2;
103 
104     if (NULL == head2)
105         return head1;
106 
107     pNode head = NULL ;
108     if (head1->data < head2->data )
109     {
110         head = head1 ;
111         head->next = MergeRecursive(head1->next, head2);
112     }
113     else
114     {
115         head = head2;
116         head->next = MergeRecursive(head1, head2->next);
117     }
118 
119     return head ;
120 }
121 // 打印無頭結點的鏈表數據
122 void PrintListHead(pNode head)
123 {
124     if (NULL == head)
125         return;
126 
127     pNode p = head;
128     while (p != NULL)
129     {
130         printf("%d ", p->data);
131         p = p->next;
132     }
133     printf("\n");
134 }
135 
136 // 構建有序鏈表
137 void  create(pNode& headNode)
138 {
139     for (int i = 0; i < 10; ++i)
140     {
141         CreateList(headNode, rand() % 200);
142     }
143 
144     PrintListHead(headNode);
145     printf("\n");
146 }
147 
148 void main()
149 {
150     // 構建有序鏈表1
151     pNode headNode1 = NULL, headNode2 = NULL;
152     pNode headNode3 = NULL, headNode4 = NULL;
153     
154     printf("打印有序鏈表1:\n");
155     create(headNode1);
156 
157     printf("打印有序鏈表2:\n");
158     create(headNode2);
159 
160     printf("打印有序鏈表3:\n");
161     create(headNode3);
162 
163     printf("打印有序鏈表4:\n");
164     create(headNode4);
165 
166     pNode newHead1 = Merge(headNode1, headNode2);
167     pNode newHead2 = MergeRecursive(headNode3, headNode4);
168     printf("打印合並有序鏈表(1、2):\n");
169     PrintListHead(newHead1);
170     printf("打印合並有序鏈表(3、4):\n");
171     PrintListHead(newHead2);
172 
173     system("pause");
174 }
175 // run out:
176 /*
177 打印有序鏈表1:
178 41 64 67 78 100 124 134 158 162 169
179 
180 打印有序鏈表2:
181 27 27 36 81 91 105 142 145 161 195
182 
183 打印有序鏈表3:
184 4 21 92 95 102 116 118 153 182 191
185 
186 打印有序鏈表4:
187 35 47 67 69 94 99 112 126 138 171
188 
189 打印合並有序鏈表(1、2):
190 27 27 36 41 64 67 78 81 91 100 105 124 134 142 145 158 161 162 169 195
191 打印合並有序鏈表(3、4):
192 4 21 35 47 67 69 92 94 95 99 102 112 116 118 126 138 153 171 182 191
193 請按任意鍵繼續. . .
194 */

(19)assert宏的實現

程序代碼如下:

1 void _assert(const char *p, const char *f, int n)
2 {
3     cout << p << endl;
4     cout << f << endl;
5     cout << n << endl;
6 }
7 #define assert(e)\
8        ((e) ? (void)0 : _assert(#e, __FILE__, __LINE__))

(20)實現全排列函數。

程序代碼如下:

 1 #include<iostream>
 2 using namespace std;
 3 
 4 template<class  Type>
 5 void  Perm(Type list[], int k, int m)
 6 {
 7     static int nCount = 0;
 8     if (k == m)
 9     {
10         cout << ++nCount << ": ";
11         for (int i = 0; i <= m; i++)
12             cout << list[i];
13 
14         cout << endl;
15     }
16     else
17     {
18         for (int i = k; i <= m; i++)
19         {
20             Swap(list[k], list[i]);
21             Perm(list, k + 1, m);
22             Swap(list[k], list[i]);
23         }
24     }
25 }
26 
27 template  <class  Type>
28 inline  void  Swap(Type &a, Type &b)
29 {
30     Type temp = a; a = b; b = temp;
31 }  
32      
33 void  main()
34 {
35     int ar[5] = {1, 2, 3, 4, 5};
36     Perm(ar, 1, 4);
37 
38     system("pause");
39 }
40 // run out:
41 /*
42 1: 12345
43 2: 12354
44 3: 12435
45 4: 12453
46 5: 12543
47 6: 12534
48 7: 13245
49 8: 13254
50 9: 13425
51 10: 13452
52 11: 13542
53 12: 13524
54 13: 14325
55 14: 14352
56 15: 14235
57 16: 14253
58 17: 14523
59 18: 14532
60 19: 15342
61 20: 15324
62 21: 15432
63 22: 15423
64 23: 15243
65 24: 15234
66 請按任意鍵繼續. . .
67 */

(21)將一個數值由N進制轉換為M進制。

程序代碼如下:

 1 #include <iostream>
 2 #include <queue>
 3 #include <stack>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 //冪函數的小遞歸 不解釋
 8 int npow(int value, int pow)
 9 { 
10     int res = 0;
11     if (pow > 0)
12     {
13         res = value * npow(value, pow - 1);
14         return res;
15     }
16     else 
17         return 1;
18 }
19 
20 // N進制轉換為M進制
21 char* ntom(int n, int m, char *data, char *res_str)
22 {
23     queue<int> iq;
24     stack<int> is; 
25 
26     int len = strlen(data);
27     //處理輸入data中的字符 也就是10進制以上的進制中出現的ABCD…
28     while (len > 0)
29     {
30         if (data[len - 1] >= 'A' && data[len - 1] <= 'F')
31         data[len - 1] = 10 + (data[len - 1] - 'A') + '0';
32         iq.push(data[len - 1] - '0');
33         len--;
34     }
35     //將data轉為10進制 並保存在val1中 val1是個中間值
36     int q_size = iq.size();
37     int val1 = 0;
38     for (int ix = 0;ix < q_size; ix++)
39     {
40         val1 += iq.front() * npow(n, ix);
41         iq.pop();
42     }
43     //將10進制數val1轉為M進制 並依次壓棧
44     int tmp2;
45     while (val1 > 0)
46     {
47         tmp2 = val1%m;
48         is.push(tmp2);
49         val1 = val1 / m;
50     }
51     
52     int j = 0;
53     char res[20];
54     //轉換后的數如果存在ABCD…則處理 否則直接轉為字符 並保存於res中
55     while (!is.empty())
56     {
57         if (is.top() >= 10)
58         {
59             res[j] = 'A' + (is.top() - 10);
60         }
61         else
62         {
63             res[j] = is.top() + '0';
64         }
65         j++;
66         is.pop();
67     }
68     res[j] = '\0';//從不忘記為字符數組的最后以為加上結束符 方便進行下面的strcpy
69     strcpy(res_str, res);
70 
71     return res_str;
72 }
73 
74 void main()
75 {   
76     int n, m;
77     char data[20];
78     char res_str[20];
79     cout << "請輸入M、N的值(N進制轉換為M進制): " << endl;
80     cin >> n >> m;
81     cout << "請輸入轉換的數值: " << endl;
82     cin >> data;
83     cout << "把數值" << data << "" << n << "進制" << "轉換為" << m << "進制的結果為:";
84     cout << ntom(n, m, data, res_str) << endl;
85 
86     system("pause");
87 }
88 
89 // run out:
90 /*
91 請輸入M、N的值(N進制轉換為M進制):
92 10 16
93 請輸入轉換的數值:
94 100
95 把數值100由10進制轉換為16進制的結果為:64
96 請按任意鍵繼續. . .
97 */

(22)寫一個函數求整型數組中的次大數。

程序代碼如下:

 1 #include<iostream>
 2 using namespace std;
 3 
 4 // 寫一個函數找出一個整數數組中第二大的數(次大數)
 5 int Max2(int ar[], int n)
 6 {
 7     int Max1 = ar[0] > ar[1] ? ar[0] : ar[1];
 8     int Max2 = ar[0] > ar[1] ? ar[1] : ar[0];
 9     for (int i = 2; i < n; i++)
10     {
11         if (ar[i] > Max2)
12         {
13             Max2 = ar[i];
14         }
15         if (ar[i] > Max1)
16         {
17             Max2 = Max1;
18             Max1 = ar[i];
19         }
20     }
21 
22     return Max2;
23 }
24 
25 void  main()
26 {
27     int  ar[8] = {1, 25, 89, 47, 101, 8888, 9999, 66};
28     cout << Max2(ar, 8) << endl;  // 888
29     system("pause");
30 }

(23)寫程序實現由鍵盤輸入內容,並將內容保存到一個文本文件中。

程序代碼如下:

 1 #include<iostream>
 2 #include<fstream>
 3 using namespace std;
 4 
 5 void main() 
 6 {
 7     ofstream fout("test.txt");// 定義輸出文件流並打開文件得2分
 8     if (!fout)
 9     {
10         cerr << "文件沒有打開!" << endl;
11         exit(1);
12     }
13     int x;
14     cin >> x;
15     while (x != -1) 
16     {
17         fout << x << ' ';
18         cin >> x;
19     } // 能夠從鍵盤向文件正確輸出數據得6分
20 
21     fout.close();// 關閉輸出文件流得1分
22 }

(24)寫一個函數從字符串N中查找子串字符串M第一次出現的位置。

程序代碼如下:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 // 在字符串n中查找第一次出現子串m的索引值
 5 int StrStr(const char *src, const char *sub)
 6 {  
 7     const char *bp;
 8     const char *sp;
 9     int nIndex = -1;
10     if ((NULL == src)||(NULL == sub))
11     {
12         return nIndex;
13     }
14 
15     while (*src)
16     {
17         bp = src;
18         sp = sub;
19         do
20         {
21             if (!*sp)
22             {
23                 return nIndex;
24             }
25         } while (*bp++ == *sp++);
26 
27         ++src;
28         ++nIndex;
29     }
30 
31     return -1;
32 }
33 
34 void main()
35 {
36     char *pStr = "abcdefghijklmn";
37     char *pDes = "ghi";
38     char *pSec = "sec";
39     cout << StrStr(pStr, pDes) << endl;  // 5
40     cout << StrStr(pStr, pSec) << endl;  // -1
41     cout << StrStr(pStr, NULL) << endl;  // -1
42     cout << StrStr(NULL, pSec) << endl;  // -1
43     system("pause");
44 }
45 // run out:
46 /*
47 5
48 -1
49 -1
50 -1
51 請按任意鍵繼續. . .
52 */

(25)有N個大小不等的自然數(1--N),請將它們由小到大排序。

要求程序算法:時間復雜度為O(n),空間復雜度為O(1)。

算法:N個不等的自然數1~N,排序完成后必然為1~N。

所以可以一次遍歷,遇到a[i] != i的就把a[i] 和 a[a[i]]交換。

函數實現以及測試代碼如下:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 void sort(int a[], int n)
 5 {
 6      int i;
 7      int t; /*臨時變量:空間復雜度O(1)*/
 8 
 9      for (i = 1; i < n + 1; ++i) /*時間復雜度O(n)*/
10      {
11          while (a[i] != i)
12           {
13              t = a[a[i]];
14              a[a[i]] = a[i];  // 排好一個元素
15              a[i] = t;
16           }
17      }
18 }
19 
20 void print(int a[], int n)
21 {
22     for (int i = 0; i < n; ++i)
23     {
24         cout << a[i] << " ";
25     }
26 }
27 
28 void main()
29 {
30     int nArray[10] = {3, 5, 7, 9, 1, 4, 8, 0, 2, 6};
31     sort(nArray, 9);
32     print(nArray, 10);
33     system("pause");
34 }
35 
36 // run out:
37 // 0 1 2 3 4 5 6 7 8 9 請按任意鍵繼續. . .

(26)建立單鏈表,把'a'--'z'26個字母插入到單鏈表中,並且倒敘,再打印數據。

程序實現以及測試程序如下:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 // 單連表的建立,把'a'--'z'26個字母插入到連表中,並且倒敘,還要打印! 
 5 typedef struct link_node 
 6 { 
 7     char data; 
 8     struct link_node *next; 
 9 }node, *pNode; 
10 
11 void print(pNode head)
12 {
13     if (NULL == head || (head->next == NULL))
14     {
15         return;
16     }
17     pNode p = head->next;
18     while (p != NULL)
19     {
20         printf(" %c ", p->data);
21         p = p->next;
22     }
23     printf("\n");
24 }
25 
26 void main(void) 
27 {  
28     pNode p = NULL; 
29     pNode q = NULL;
30 
31     pNode head = (pNode)malloc(sizeof(node)); 
32     head->data = ' ';
33     head->next = NULL; 
34 
35     pNode firstNode = (pNode)malloc(sizeof(node)); 
36     firstNode->data = 'a';
37     firstNode->next = NULL;
38     head->next = firstNode; 
39     p = firstNode; 
40 
41     int longth = 'z' - 'b'; 
42     int i = 0; 
43     while (i <= longth ) 
44     { 
45         pNode tempNode = (pNode)malloc(sizeof(node)); 
46         tempNode->data = 'b' + i;
47         tempNode->next = NULL;
48         q = tempNode;
49 
50         head->next = tempNode; 
51         tempNode->next = p;
52         p = q; 
53         i++; 
54     } 
55 
56     print(head);
57 
58     system("pause");
59 }
60 // run out:
61 /*
62  z  y  x  w  v  u  t  s  r  q  p  o  n  m  l  k  j  i  h  g  f  e  d  c  b  a
63 請按任意鍵繼續. . .
64 */

(27)用指針的方法,將字符串“ABCD1234efgh”前后對調顯示。

程序實現以及測試程序如下:

 1 #include <stdio.h> 
 2 #include <string.h>
 3 #include <stdlib.h>
 4 
 5 void main() 
 6 { 
 7     char str[] = "ABCD1234efgh"; 
 8     int length = strlen(str); 
 9     char * p1 = str; 
10     char * p2 = str + length - 1; 
11     while (p1 < p2) 
12     { 
13         char c = *p1; 
14         *p1 = *p2; 
15         *p2 = c; 
16         ++p1; 
17         --p2; 
18     } 
19     printf("str now is %s\n", str); 
20     system("pause"); 
21 }
22 // run out:
23 /*
24 str now is hgfe4321DCBA
25 請按任意鍵繼續. . .
26 */

(28)《 字符串匹配KMP算法

(29)編寫一個不定形參的函數(計算不計數的實參的平均數)

程序示例代碼如下:

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 #define _INTSIZEOF(n)   ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
 5                             //  4 + 4 - 1 = 7                   // 4-1 = 3
 6                             //  0111                          // ~0011 = 1100
 7                             // 0111 & 1100 = 0100   = 4   
 8 #define va_start(ap,v)  ( ap = (va_list)&v + _INTSIZEOF(v) )
 9                          // 指針 = (char*)&V(數值個數)  +  4   
10 #define va_arg(ap,t)    ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
11                          // (*(int *)((指針ap += INTSIZEOF(int)) - INTSIZEOF(int)))
12                          // *(int *) ((ap = ap + INTSIZEOF(int)) - INTSIZEOF(int))
13                          // *(int *)(本段代碼執行結果ap向前走4個字節,但是地址不變)
14 #define va_end(ap)      ( ap = (va_list)0 )
15                          // ap = (char *)0
16 
17 int average(int n_values, ...)
18 {
19     int sum = 0;
20 
21     va_list var_arg;  // char *
22 
23     va_start(var_arg, n_values);
24 
25     for (int i = 0 ; i < n_values; ++i)
26     {
27         sum += va_arg(var_arg, int);
28     }
29 
30     va_end(var_arg);
31     return sum / n_values;
32 }
33 
34 void main()
35 {
36     printf("%d \n", average(3, 10, 20, 30));  // 20
37     system("pause");
38 }

(30)計算無符號長整型的二進制每四位的和。

程序代碼如下:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int Count(unsigned long value)
 5 {
 6     int sum = 0;
 7     while (value)
 8     {
 9         sum += value & 0x0f;
10          value >>= 4;    
11     }
12 
13     return sum;
14 }
15 
16 void main()
17 {
18     cout << Count(2773) << endl; // 0000 1010 1101 0101  // 0 + 10 + 13 + 5 = 28
19     system("pause");
20 }

(31)字符串類String的實現。請參見隨筆《 字符串String

(32)

(33)

(34)

(35)

 

Good Good  Study, Day Day Up.

順序  選擇  循環  總結


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM