面试过程中遇到的编程题整理,于此备录。分享,共勉。(持续更新中......欢迎补充)
(1)用户输入M, N值,从1至N开始顺序循环数数,每数到M输出该数值,直至全部输出。写出C程序。
程序代码如下:
1 #include
2 #include
3
4 //节点结构定义
5 typedef structLink_Node6 {7 intdata;8 Link_Node*next;9 }Node, *pNode;10
11 //创建循环链表
12 void CreateList(pNode& head, pNode& tail, intn)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, intm)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 voidmain()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 2111 10112 打印链表数据信息如下:113 1 2 3 4 5 6 7 8 9 10114
115 循环数数,遇到M输出结果如下:116 2 4 6 8 10 3 7 1 9 5117 请按任意键继续. . .118 */
(2)从键盘输入10个学生的学号和成绩,按成绩从大到小建立一个有序链表,并输出。
程序代码如下:
1 #include
2 #include
3
4 typedef structnode5 {6 intxh;7 intcj;8 struct node *next;9 }Node, *pNode;10
11 voidmain()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 6965 第2个学生(学号 成绩):2 8966 第3个学生(学号 成绩):3 5967 第4个学生(学号 成绩):4 10068 第5个学生(学号 成绩):5 6869 第6个学生(学号 成绩):6 8570 第7个学生(学号 成绩):7 8271 第8个学生(学号 成绩):8 9172 第9个学生(学号 成绩):9 7273 第10个学生(学号 成绩):10 8074
75 输出结果:76 (4)-->10077 (8)-->9178 (2)-->8979 (6)-->8580 (7)-->8281 (10)-->8082 (9)-->7283 (1)-->6984 (5)-->6885 (3)-->5986 请按任意键继续. . .87 */
(3)利用无序数组元素构建一个有序单链表。
1 #include
2 #include
3
4 typedef structnode5 {6 intdata;7 struct node *next;8 }Node, *pNode;9
10 voidmain()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 10066 9067 8968 6569 4570 4571 3272 2373 1274 775 请按任意键继续. . .76 */
(4)写一个函数找出一个整数数组中,第二大的数 (microsoft)
程序代码如下:
1 #include
2 using namespacestd;3
4 const int MINNUMBER = -32767;5
6 int find_sec_max(int data[], intcount)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 returnsec_max;25 }26
27 voidmain()28 {29 int nArray[10] =