码迷,mamicode.com
首页 > 其他好文 > 详细

Chap3: question: 11 - 18

时间:2014-07-22 23:11:56      阅读:436      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   strong   

11. double 数值的整数次方

 note: 浮点数表示时有误差,判等时必须自己根据精度要求实现。

 mamicode.com,码迷

12.打印 1 到最大的 n 位数

note: n 可能很大,如 100,就容易溢出

 a. 字符串模拟法

mamicode.com,码迷
 1 #include <stdio.h>
 2 #include <string.h>
 3 bool Increment(char *);
 4 void printNumber(char *);
 5 
 6 void print1ToMax(int n)
 7 {
 8     if(n <= 0) return;
 9     char *number = new char[n+1];
10     memset(number, 0, n);
11     number[n] = \0;
12     while(!Increment(number)) // Increment() return true denote overflow
13     {
14         printNumber(number);
15     }
16     delete[] number;
17 }
18 
19 bool Increment(char *number)
20 {
21     bool overflow = false;
22     int carry = 0, len = strlen(number);
23     for(int i = len -1; i >= 0; --i)   /*   note: 从最右端开始增 is more easy    */
24     {
25         number[i] += carry;
26         if(i == len -1)  ++number[i];
27         if(number[i] > 9)
28         {
29             if(i == 0)
30                 overflow = true;
31             else
32             {
33                 number[i] -= 10;
34                 carry = 1;
35             }
36         }
37         else break;
38     }
39     return overflow;
40 }
41 
42 void printNumber(char *number)
43 {
44     if(number == NULL) return;
45     bool begin = false;
46     for(int i = 0; number[i] != \0; ++i)
47     {
48         if(begin)
49         {
50             printf("%c", number[i]);
51         }
52         else if(number[i] != 0)
53         {
54             begin = true;
55             printf("%c", number[i]);
56         }
57     }
58     printf("\t");
59 }
60 
61 
62 int main()
63 {
64     int N;
65     while(scanf("%d", &N) == 1)
66     {
67         print1ToMax(N);
68     }
69     return 0;
70 }
Code

mamicode.com,码迷

  b. 数字排列法(由后到前,递归增加)

 运行结果如上。
13. O(1) 时间删除链表结点

 其值赋为后继的值,删除后继结点。时间:[O(1) * (n-1) + O(n)] / n ≈ O(1).          //  若检测欲删除结点是否在链表内,则 O(n) .

mamicode.com,码迷
 1 #include <stdio.h>
 2 struct ListNode{
 3     int v;
 4     ListNode * next;
 5 };
 6 enum{ N = 5}; // set the number of Node in ListNode
 7 /*************************************************************************/
 8 /********  Basic function which were needed ******************************/
 9 /*  creat a List with the value of Node is 1, 2, …,N-1.  */
10 ListNode* creatList(){  
11     ListNode * pHead = NULL, *p;
12     for(int i = 1; i < N; ++i){
13         ListNode *s = new ListNode;
14         s->v = i;
15         s->next = NULL;
16         if(pHead != NULL){
17             p->next = s;
18             p = p->next;
19         }else{
20             pHead = s;
21             p = pHead;
22         }
23     }
24     return pHead;
25 }
26 void printList(ListNode *pHead){
27     if(pHead == NULL) { printf("NULL"); return; }
28     printf("%d —> ", pHead->v);
29     printList(pHead->next);
30 }
31 void release(ListNode *pHead) {
32     if(pHead == NULL) return;
33     release(pHead->next);
34     delete[] pHead;
35     pHead = NULL;
36 }
37 /****************************************************************************************/
38 void deleteNode(ListNode *pHead, ListNode * pToBeDeleted){
39     if(pHead == NULL || pToBeDeleted == NULL) return;
40     /*   not the last one  */
41     if(pToBeDeleted->next != NULL){  
42         ListNode *pNext = pToBeDeleted->next;
43         pToBeDeleted->v = pToBeDeleted->next->v;
44         pToBeDeleted->next = pToBeDeleted->next->next;
45         delete[] pNext;
46         pNext = NULL;
47     }else{
48         if(pToBeDeleted == pHead) {  // List only has one Node 
49             delete [] pHead;
50             pHead = NULL;
51             return; 
52         }else {                     // the last one && not the head 
53             ListNode *p = pHead;
54             while(p->next != pToBeDeleted && p->next != NULL) p = p->next; 
55             if(p->next == NULL) return;
56             else{
57                 ListNode *s = p->next;
58                 p->next = NULL;
59                 delete[] s;
60             }
61         }
62     }
63 }
64 
65 int main()
66 {
67     ListNode *pHead = creatList();
68     printList(pHead);
69     printf("\ndelete 3: \n");
70 
71     deleteNode(pHead, pHead->next->next); // delete 3 (among)
72     printList(pHead);
73     printf("\ndelete 1: \n");
74 
75     deleteNode(pHead, pHead);      // delete 1 (head)
76     printList(pHead);
77     printf("\ndelete 4: \n");
78 
79     deleteNode(pHead, pHead->next); // delete 4 (tail)
80     printList(pHead);
81     printf("\n");
82 /*
83     ListNode *s = new ListNode; // not in the list, if wanted more robust , need check before deleteNode 
84     deleteNode(pHead, s);
85     printList(pHead);
86     printf("\n");
87     delete[] s;
88 */
89     release(pHead);
90     return 0;
91 }
Code

 mamicode.com,码迷

14. 使整数数组中奇数位于前部分,偶数位于后部分。

 解耦合,使 Reorder函数可以复用。如代码:

mamicode.com,码迷
 1 #include <stdio.h>
 2 
 3 bool isEven(int v)
 4 {
 5     return (v & 1) == 1;
 6 }
 7 
 8 bool isPositive(int v)
 9 {
10     return v > 0;
11 }
12 
13 void Reorder(int data[], int length, bool (*fcn)(int))
14 {
15     if(length <= 1) return;
16     int low = 0, high = length - 1;
17     while(low < high)
18     {
19         while(low < high && fcn(data[low])) ++low;
20         while(low < high && !fcn(data[high])) --high;
21         if(low < high)
22         {
23             int tem = data[low];
24             data[low] = data[high];
25             data[high] = tem;
26         }
27     }
28 }
29 
30 void printf(int data[], int length)
31 {
32     for(int i = 0; i < length; ++i)
33     {
34         printf("%-3d", data[i]);
35     }
36     printf("\n");
37 }
38 int main()
39 {
40     printf("Test 1: odd element before even element. \n");
41     int test1[] = {1, 2, 3, 4, 5, 6, 7};
42     printf(test1, sizeof(test1)/sizeof(int));
43     Reorder(test1,sizeof(test1)/4, isEven);   /*  奇数放前面    */
44     printf(test1, sizeof(test1)/4);
45 
46     printf("Test 2: positive before Non-positive. \n");
47     int test2[] = {-3, 3, -2, 2, 0, -1, 1};
48     printf(test2, sizeof(test2)/sizeof(int));
49     Reorder(test2,sizeof(test2)/4, isPositive); /*  正数放前面  */
50     printf(test2, sizeof(test2)/4);
51 
52     return 0;
53 }
Code

mamicode.com,码迷

 15. 链表中倒数第 k 个结点

 note: k = 0 或 k > LinkList.length(). 

 mamicode.com,码迷

16. 反转链表

 a. mamicode.com,码迷   b.mamicode.com,码迷   c. mamicode.com,码迷

17.合并两个排序的链表(递归) 

mamicode.com,码迷
 1 #include <stdio.h> 
 2 struct ListNode{ 
 3     int v; 
 4     ListNode * next; 
 5 }; 
 6 /*************************************************************************/
 7 /********  Basic functions which were needed ******************************/
 8 /*  creat a List with the value of Node is 1, 2, …,N.  */
 9 ListNode* creatList(int begin, int N){   
10     ListNode * pHead = NULL, *p;
11     for(int i = begin; i <= N; ++i){ 
12         ListNode *s = new ListNode; 
13         s->v = i; 
14         s->next = NULL;
15         if(pHead != NULL){
16             p->next = s; 
17             p = p->next;
18         }else {
19             pHead = s;
20             p = pHead;
21         }
22     } 
23     return pHead; 
24 } 
25 void printList(ListNode *pHead){ 
26     if(pHead == NULL) { printf("NULL"); return; } 
27     printf("%d —> ", pHead->v); 
28     printList(pHead->next); 
29 } 
30 void release(ListNode *pHead) { 
31     if(pHead == NULL) return; 
32     release(pHead->next); 
33     delete[] pHead; 
34     pHead = NULL; 
35 } 
36 /********************************************************************************/
37 
38 ListNode* Merge(ListNode *pHead1, ListNode *pHead2) //not create any new Node again
39 {
40     if(pHead1 == NULL) return pHead2;
41     if(pHead2 == NULL) return pHead1;
42     ListNode *pMergedHead = NULL;
43     if(pHead1->v <= pHead2->v)
44     {
45         pMergedHead = pHead1;
46         pMergedHead->next = Merge(pHead1->next, pHead2);
47     }
48     else
49     {
50         pMergedHead = pHead2;
51         pMergedHead->next = Merge(pHead1, pHead2->next);
52     }
53     return pMergedHead; // key point
54 }
55 
56 int main() 
57 { 
58     ListNode *pHead1 = creatList(1, 5); 
59     printf("LinkList1: "); 
60     printList(pHead1); 
61     printf("\n");
62     ListNode *pHead2 = creatList(2, 6);
63     printf("LinkList1: "); 
64     printList(pHead2); 
65     printf("\n");
66 
67     ListNode *pHead3 = Merge(pHead1, pHead2);
68     printf("MergeList: "); 
69     printList(pHead3); 
70     printf("\n");
71 
72     release(pHead3); 
73     return 0; 
74 } 
Code

mamicode.com,码迷

18. 判断树 B 是否为树 A 的子结构(递归)

 mamicode.com,码迷mamicode.com,码迷mamicode.com,码迷

 

 

 

 

 

Chap3: question: 11 - 18,码迷,mamicode.com

Chap3: question: 11 - 18

标签:style   blog   http   color   os   strong   

原文地址:http://www.cnblogs.com/liyangguang1988/p/3700107.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!