├── Joseph_ring.c ├── README.md ├── arrange.c ├── array.c ├── char.c ├── charOprate.c ├── charOprate.h.gch ├── eightQueen.c ├── header.h ├── list.c ├── main.c ├── print1toN.c ├── print1toN.h.gch ├── quick_sort.c ├── quick_sort.h.gch ├── reOrderOddEven.c ├── test.cbp ├── test.depend ├── test.h ├── test.txt ├── tree.c └── 剑指offer ├── README ├── 面试题03——二维数组中的查找 ├── README └── main.c ├── 面试题04——替换空格 ├── README └── main.c ├── 面试题05——从头到尾打印链表 ├── README └── main.c ├── 面试题06——重建二叉树 ├── README └── main.c ├── 面试题07——用两个栈实现队列 ├── README └── main.c ├── 面试题08——旋转数组的最小数字 ├── README └── main.c ├── 面试题09——斐波那契数列 ├── README └── main.c ├── 面试题09(变形)——变态跳台阶 ├── README └── main.c ├── 面试题09(变形)——矩形覆盖 ├── README └── main.c ├── 面试题09(变形)——跳台阶 ├── README └── main.c ├── 面试题10——二进制中1的个数 ├── README └── main.c ├── 面试题12——打印1到最大的N位数 ├── README └── main.c ├── 面试题14——调整数组顺序使奇数位于偶数前面 ├── README └── main.c ├── 面试题15——链表中倒数第k个结点 ├── README └── main.c ├── 面试题16——反转链表 ├── README └── main.c ├── 面试题17——合并两个排序的链表 ├── README └── main.c ├── 面试题19——二叉树的镜像 ├── README └── main.c ├── 面试题20——顺时针打印矩阵 ├── README └── main.c ├── 面试题21——包含min函数的栈 ├── README └── main.c ├── 面试题22——栈的压入、弹出序列 ├── README └── main.c ├── 面试题23——从上往下打印二叉树 ├── README ├── a.out └── main.c ├── 面试题24——二叉搜索树的后序遍历序列 ├── README └── main.c ├── 面试题25——二叉树中和为某一值的路径 ├── README └── main.c ├── 面试题27——WA二叉搜索树与双向链表 ├── README └── main.c ├── 面试题28——字符串的排列 ├── README ├── main.c └── 二叉搜索树与双向链表.url ├── 面试题29——数组中出现次数超过一半的数字 ├── README └── main.c ├── 面试题30——最小的K个数 ├── README ├── main.c └── main1.c ├── 面试题31——WA连续子数组的最大和 ├── README ├── a.out └── main.c ├── 面试题32——PASS从1到n整数中1出现的次数 └── README ├── 面试题33——PASS把数组排成最小的数 └── README ├── 面试题33——把数组排成最小的数 ├── README ├── main └── main.c ├── 面试题34——PASS丑数 └── README ├── 面试题35——第一个只出现一次的字符 ├── README └── main.c ├── 面试题36——PASS数组中的逆序对 └── README ├── 面试题37——两个链表的第一个公共结点 ├── README └── main.c ├── 面试题38——WA数字在排序数组中出现的次数 ├── README └── main.c ├── 面试题39——PASS二叉树的深度 └── README ├── 面试题40——数组中只出现一次的数字 ├── README └── main.c ├── 面试题41——和为S的两个数字 ├── README └── main.c ├── 面试题41——和为S的连续正数序列 ├── README └── main.c ├── 面试题42——左旋转字符串 ├── README └── main.c ├── 面试题42——翻转单词顺序 ├── README └── main.c ├── 面试题43——PASSN个骰子的点数 └── README ├── 面试题44——WA扑克牌顺子 ├── README └── main.c ├── 面试题45——圆圈中最后剩下的数 ├── README └── main.c ├── 面试题46——求1+2+3+...+n ├── README └── main.c ├── 面试题47——不用加减乘除做加法 ├── README └── main.c ├── 面试题49——把字符串转换成整数 ├── README └── main.c └── 面试题50——树中两个结点的最低公共祖先 ├── README └── main.c /Joseph_ring.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/Joseph_ring.c -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | C-language 2 | ========== 3 | 4 | C语言练习代码 5 | -------------------------------------------------------------------------------- /arrange.c: -------------------------------------------------------------------------------- 1 | #include 2 | //排列的实现方法,和八皇后的实现方法是类似的 3 | 4 | int isOK(int *a, int index, int n) 5 | { 6 | int i; 7 | 8 | if (a == NULL || index < 0) 9 | return 0; 10 | for (i = 0; i < index; i++) 11 | if (a[i] == n) 12 | return 0; 13 | return 1; 14 | } 15 | void DoArrange(int *a, int n, int index) 16 | { 17 | int i; 18 | 19 | if (a == NULL || n < 1 || index < 0) 20 | return; 21 | if (index >= n) 22 | { 23 | for (i = 0; i < n; i++) 24 | printf("%d ", a[i]); 25 | printf("\n"); 26 | return; 27 | } 28 | for (i = 0; i < n; i++) 29 | { 30 | if (isOK(a, index, i)) 31 | { 32 | a[index] = i; 33 | DoArrange(a, n, index+1); 34 | } 35 | } 36 | } 37 | 38 | void arrange(int n) 39 | { 40 | int a[100]; 41 | 42 | DoArrange(a, n, 0); 43 | } 44 | -------------------------------------------------------------------------------- /array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /*求与数组有关的问题*/ 5 | 6 | 7 | /*求数组中和最大的字串,返回最大的和 8 | *这个解法的时间复杂度为O(n^2)*/ 9 | int MaxSumInArray(int *array, int len) 10 | { 11 | int Max, sum, i, j; 12 | 13 | Max = INT_MIN; 14 | for (i = 0; i < len; i++) 15 | { 16 | sum = 0; 17 | for ( j = i; j < len; j++) 18 | { 19 | sum += array[j]; 20 | if (sum > Max) 21 | Max = sum; 22 | } 23 | } 24 | 25 | return Max; 26 | } 27 | 28 | 29 | /*求数组中和最大的字串,返回最大的和 30 | *这个解法的时间复杂度为O(n) 31 | 这个解比上面的解来的好,只需扫描一次即可 32 | 这个算法同时指出循环数组*/ 33 | int MaxSumInArray01(int *array, int len) 34 | { 35 | int Max, sum, i, begin; 36 | 37 | Max = INT_MIN; 38 | for (i = sum = 0; i < len*2 && i < len + begin; i++) 39 | { 40 | sum += array[i%len]; 41 | if (sum > Max) 42 | Max = sum; 43 | if (sum <= 0) 44 | { 45 | sum = 0; 46 | begin = i; 47 | } 48 | } 49 | 50 | return Max; 51 | } 52 | -------------------------------------------------------------------------------- /char.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //字符和字符串有关的一些练习 4 | static int count = 0; 5 | 6 | void shooting_do(int *a, int index, int len, int score) 7 | { 8 | int i,j; 9 | int total = 0; 10 | if (index > len - 1) 11 | { 12 | for (i = 0; i < len; i++) 13 | { 14 | total += a[i]; 15 | //printf("%d ", a[i]); 16 | } 17 | //printf("\n"); 18 | if (total == score) 19 | count++; 20 | return; 21 | } 22 | for (i = 0; i <= len; i++) 23 | { 24 | a[index] = i; 25 | for (j = 0; j <= index; j++) 26 | { 27 | total += a[j]; 28 | } 29 | if ((total + (len-1-index)*len) < score) 30 | { 31 | total = 0; 32 | continue; 33 | } 34 | shooting_do(a, index+1, len, score); 35 | } 36 | } 37 | 38 | int shooting() //打靶10次,分数高于90的情况个数 39 | { 40 | int a[10]; 41 | int len = 10, score = 90; 42 | 43 | shooting_do(a, 0, len, score); 44 | return count; 45 | } 46 | 47 | void combine_do(char *in, char *out, int len, int start, int cur) 48 | { 49 | int i; 50 | 51 | if (cur == len) 52 | return; 53 | for (i = start; i < len; i++) 54 | { 55 | out[cur] = in[i]; 56 | out[cur+1] = '\0'; 57 | printf("%s\n", out); 58 | if (i < len - 1) 59 | combine_do(in, out, len, start+1, cur+1); 60 | } 61 | } 62 | 63 | int combine() //字符串字串问题,还没完成,后面再看看 64 | { 65 | char str[] = "hart"; 66 | char out[100]; 67 | int len = 4; 68 | combine_do(str, out, len, 0, 0); 69 | } 70 | int bag[100] = {0}; 71 | int mVal, nVal; 72 | void bag_01(int m, int n) //0 1背包问题 73 | { 74 | int i; 75 | 76 | if (n<1||m<1||(n==1&&m!=1)) 77 | return; 78 | if (n == m) 79 | { 80 | bag[n] = 1; 81 | for (i = 1; i <= nVal; i++) 82 | if (bag[i] == 1) 83 | printf("%d ", i); 84 | printf("\n"); 85 | bag[n] = 0; 86 | } 87 | 88 | bag[n] = 1; 89 | bag_01(m - n, n - 1); //n有加入的情况下 90 | bag[n] = 0; 91 | bag_01(m, n-1); //n没有加入的情况下 92 | } 93 | 94 | void call_bag01(int m, int n) //0 1背包调用函数 95 | { 96 | mVal = m; 97 | nVal = n; 98 | bag_01(mVal, nVal); 99 | } 100 | -------------------------------------------------------------------------------- /charOprate.c: -------------------------------------------------------------------------------- 1 | #include 2 | //字符串反转的问题 3 | void swap_char(char *a, char *b) 4 | { 5 | char tmp = *a; 6 | *a = *b; 7 | *b = tmp; 8 | } 9 | 10 | void reverse(char *a, int s, int d) 11 | { 12 | while (s < d) 13 | { 14 | swap_char(&a[s], &a[d]); 15 | s++; 16 | d--; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /charOprate.h.gch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/charOprate.h.gch -------------------------------------------------------------------------------- /eightQueen.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/eightQueen.c -------------------------------------------------------------------------------- /header.h: -------------------------------------------------------------------------------- 1 | #ifndef HEADER_H_INCLUDED 2 | #define HEADER_H_INCLUDED 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | typedef struct listNode 10 | { 11 | int data; 12 | struct listNode *next ; 13 | }listNode,*list; 14 | 15 | typedef struct btNode 16 | { 17 | int data; 18 | struct btNode *lchild, *rchild; 19 | }btNode, *BiTree; 20 | 21 | 22 | 23 | #endif // HEADER_H_INCLUDED 24 | -------------------------------------------------------------------------------- /list.c: -------------------------------------------------------------------------------- 1 | #include 2 | //单链表的一些练习 3 | typedef struct listNode 4 | { 5 | int data; 6 | struct listNode *next ; 7 | }listNode,*list; 8 | 9 | list CreateList(int *str, int len) 10 | { 11 | int i; 12 | listNode *head, *node, *tmp; 13 | 14 | if (str == NULL || len == 0) 15 | return NULL; 16 | head = (list)malloc(sizeof(listNode)); 17 | head->next = NULL; 18 | for (i = 0; i < len - 1; i++) 19 | { 20 | node = (list)malloc(sizeof(listNode)); 21 | node->data = str[i]; 22 | tmp = head->next; 23 | head->next = node; 24 | node->next = tmp; 25 | 26 | } 27 | head->data = str[i]; 28 | return head; 29 | } 30 | list reverseList(list l) 31 | { 32 | list head, tmp; 33 | 34 | head = tmp = NULL; 35 | while (l != NULL) 36 | { 37 | tmp = head; 38 | head = l; 39 | l = l->next; 40 | head->next = tmp; 41 | } 42 | return head; 43 | } 44 | 45 | void printList(list head) 46 | { 47 | if (head == NULL) 48 | return; 49 | 50 | while (head != NULL) 51 | { 52 | printf("%d ", head->data); 53 | head = head->next; 54 | } 55 | } 56 | 57 | list mergeList(list a, list b) 58 | { 59 | list head = NULL; 60 | 61 | if (a == NULL) 62 | return b; 63 | if (b == NULL) 64 | return a; 65 | if (a->data > b->data) 66 | { 67 | head = a; 68 | head->next = mergeList(a->next, b); 69 | } 70 | else 71 | { 72 | head = b; 73 | head->next = mergeList(a, b->next); 74 | } 75 | return head; 76 | } 77 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define N 10001 4 | 5 | typedef struct node 6 | { 7 | int data; 8 | struct node *lchild; 9 | struct node *rchild; 10 | }Node, *tree; 11 | 12 | tree Create() 13 | { 14 | int tmp; 15 | Node *tmpNode; 16 | 17 | scanf("%d", &tmp); 18 | if (tmp == 0) 19 | return NULL; 20 | tmpNode = malloc(sizeof(Node)); 21 | tmpNode->data = tmp; 22 | tmpNode->lchild = Create(); 23 | tmpNode->rchild = Create(); 24 | return tmpNode; 25 | } 26 | 27 | int getPath(tree root, int m, int *stack, int *top) 28 | { 29 | if (root == NULL) 30 | return 0; 31 | stack[++(*top)] = root->data; 32 | if (root->data != m) 33 | { 34 | if (getPath(root->lchild, m, stack, top) || getPath(root->rchild, m, stack, top)) 35 | return 1; 36 | else 37 | { 38 | (*top)--; 39 | return 0; 40 | } 41 | } else{ 42 | return 1; 43 | } 44 | } 45 | 46 | int stack1[N], top1, stack2[N], top2; 47 | 48 | int main() 49 | { 50 | freopen("test.txt", "r", stdin); 51 | 52 | int cnt, i, j, num1, num2; 53 | tree root; 54 | while (scanf("%d", &cnt) != EOF) 55 | { 56 | for (i = 0; i < cnt; i++) 57 | { 58 | root = Create(); 59 | scanf("%d%d", &num1, &num2); 60 | top1 = top2 = -1; 61 | if (getPath(root, num1, stack1, &top1) && getPath(root, num2, stack2, &top2)) 62 | { 63 | if (top1 > top2) 64 | top1 = top2; 65 | for (j = top1; j >= 0; j--) 66 | { 67 | if (stack1[j] == stack2[j]) 68 | { 69 | printf("%d\n", stack1[j]); 70 | break; 71 | } 72 | } 73 | } 74 | else 75 | printf("My God\n"); 76 | } 77 | } 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /print1toN.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/print1toN.c -------------------------------------------------------------------------------- /print1toN.h.gch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/print1toN.h.gch -------------------------------------------------------------------------------- /quick_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | //快速排序的随机化版本 5 | void quick_sort(int *a, int s, int d) 6 | { 7 | int i, j; 8 | 9 | if (s > d) 10 | return; 11 | srand(time(NULL)); 12 | i = rand()%(d+1-s) + s; 13 | swap(&a[i], &a[d]); 14 | for(i = s - 1, j = s; j < d; j++) 15 | { 16 | if (a[j] < a[d]) 17 | { 18 | swap(&a[++i], &a[j]); 19 | } 20 | } 21 | swap(&a[++i], &a[d]); 22 | quick_sort(a, s, i-1); 23 | quick_sort(a, i+1, d); 24 | } 25 | -------------------------------------------------------------------------------- /quick_sort.h.gch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/quick_sort.h.gch -------------------------------------------------------------------------------- /reOrderOddEven.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/reOrderOddEven.c -------------------------------------------------------------------------------- /test.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 76 | 77 | -------------------------------------------------------------------------------- /test.depend: -------------------------------------------------------------------------------- 1 | # depslib dependency file v1.0 2 | 1397544271 source:d:\learn_java\c_code\test\main.c 3 | 4 | 5 | 1397543635 source:d:\learn_java\c_code\test\print1ton.c 6 | 7 | 8 | 1397376978 d:\learn_java\c_code\test\header.h 9 | 10 | 11 | 12 | 13 | 14 | 1397543812 source:d:\learn_java\c_code\test\quick_sort.c 15 | 16 | 17 | 1397543635 source:d:\learn_java\c_code\test\charoprate.c 18 | 19 | 20 | 1397283793 source:d:\learn_java\c_code\test\print1ton.h 21 | "header.h" 22 | 23 | 1397280893 source:d:\learn_java\c_code\test\quick_sort.h 24 | "header.h" 25 | 26 | 1397280968 source:d:\learn_java\c_code\test\charoprate.h 27 | 28 | 1397283793 d:\learn_java\c_code\test\print1ton.h 29 | "header.h" 30 | 31 | 1397284249 d:\learn_java\c_code\test\test.h 32 | 33 | 1397543635 source:d:\learn_java\c_code\test\reorderoddeven.c 34 | 35 | 36 | 1397543687 source:d:\learn_java\c_code\test\list.c 37 | 38 | 39 | 1397544230 source:d:\learn_java\c_code\test\tree.c 40 | 41 | 42 | 1397543635 source:d:\learn_java\c_code\test\eightqueen.c 43 | 44 | 45 | 1397543634 source:d:\learn_java\c_code\test\joseph_ring.c 46 | 47 | 48 | 1397544267 source:d:\learn_java\c_code\test\arrange.c 49 | 50 | 51 | 1397546546 source:d:\github\c-language\eightqueen.c 52 | 53 | 54 | 1398142555 source:d:\github\c-language\list.c 55 | 56 | 57 | 1399796239 source:d:\github\c-language\main.c 58 | 59 | 60 | 61 | 1397543635 source:d:\github\c-language\print1ton.c 62 | 63 | 64 | 1397546428 source:d:\github\c-language\reorderoddeven.c 65 | 66 | 67 | 1397545753 source:d:\github\c-language\joseph_ring.c 68 | 69 | 70 | 1397546453 source:d:\github\c-language\charoprate.c 71 | 72 | 73 | 1398429620 source:d:\github\c-language\tree.c 74 | 75 | 76 | 1397546013 source:d:\github\c-language\quick_sort.c 77 | 78 | 79 | 80 | 81 | 1397546387 source:d:\github\c-language\arrange.c 82 | 83 | 84 | 1398499291 source:d:\github\c-language\char.c 85 | 86 | 87 | 1398742343 source:d:\github\c-language\array.c 88 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /test.h: -------------------------------------------------------------------------------- 1 | #ifndef TEST_H_INCLUDED 2 | #define TEST_H_INCLUDED 3 | 4 | void a() 5 | { 6 | printf("SDFFFFFFFFFFFFFF\n"); 7 | } 8 | 9 | #endif // TEST_H_INCLUDED 10 | -------------------------------------------------------------------------------- /test.txt: -------------------------------------------------------------------------------- 1 | zhe shi wei shen me ne 2 | -------------------------------------------------------------------------------- /tree.c: -------------------------------------------------------------------------------- 1 | #include 2 | //关于树的一些练习 3 | typedef struct btNode 4 | { 5 | int data; 6 | struct btNode *lchild, *rchild; 7 | } btNode, *BiTree; 8 | 9 | BiTree CreateBiTree() 10 | { 11 | char ch; 12 | BiTree T; 13 | scanf(" %c",&ch); 14 | if(ch=='#')T=NULL; 15 | else 16 | { 17 | T = (BiTree)malloc(sizeof(btNode)); 18 | T->data = ch; 19 | T->lchild = CreateBiTree(); 20 | T->rchild = CreateBiTree(); 21 | } 22 | return T; 23 | } 24 | void PreOrderTraverse(BiTree T) 25 | { 26 | if(T) 27 | { 28 | printf("%c",T->data); 29 | PreOrderTraverse(T->lchild); 30 | PreOrderTraverse(T->rchild); 31 | } 32 | } 33 | void TreeOfMirror(BiTree T) 34 | { 35 | BiTree stack[100], current, tmp; 36 | int top = -1; 37 | 38 | if (T != NULL) 39 | stack[++top] = T; 40 | while (top != -1) 41 | { 42 | current = stack[top--]; 43 | if (current->lchild != NULL) 44 | stack[++top] = current->lchild; 45 | if (current->rchild != NULL) 46 | stack[++top] = current->rchild; 47 | tmp = current->lchild; 48 | current->lchild = current->rchild; 49 | current->rchild = tmp; 50 | } 51 | } 52 | 53 | int TreeDeep(BiTree T) 54 | { 55 | int leftDeep, rightDeep; 56 | 57 | if (T == NULL) 58 | return 0; 59 | leftDeep = TreeDeep(T->lchild) + 1; 60 | rightDeep = TreeDeep(T->rchild) + 1; 61 | return leftDeep>rightDeep? leftDeep:rightDeep; 62 | } 63 | 64 | int TreeMaxRoad(BiTree T) 65 | { 66 | int leftDeep, rightDeep; 67 | static int Max = 0; 68 | 69 | if (T == NULL) 70 | return 0; 71 | leftDeep = TreeDeep(T->lchild) + 1; 72 | rightDeep = TreeDeep(T->rchild) + 1; 73 | if (leftDeep + rightDeep - 1 > Max) 74 | Max = leftDeep + rightDeep - 1; 75 | 76 | return Max; 77 | } 78 | 79 | int PrintNodeAtLevel(BiTree T, int level) 80 | { 81 | if (!T || level < 0) 82 | return 0; 83 | if (level == 0) 84 | { 85 | printf("%c\n", T->data); 86 | return 1; 87 | } 88 | return (PrintNodeAtLevel(T->lchild, level-1) + PrintNodeAtLevel(T->rchild, level-1)); 89 | } 90 | #define N 100 91 | int PrintAllLevel(BiTree root) 92 | { 93 | BiTree sequence[N]; 94 | int start, end, last; 95 | 96 | if (root == NULL) 97 | return 0; 98 | 99 | start = -1; 100 | end = last = 0; 101 | sequence[end++] = root; 102 | while (++start <= end - 1) 103 | { 104 | if (sequence[start]->lchild != NULL) 105 | sequence[end++] = sequence[start]->lchild; 106 | if (sequence[start]->rchild != NULL) 107 | sequence[end++] = sequence[start]->rchild; 108 | printf("%c ", sequence[start]->data); 109 | if (start == last) 110 | { 111 | printf("\n"); 112 | last = end - 1; 113 | } 114 | } 115 | } 116 | 117 | 118 | 119 | -------------------------------------------------------------------------------- /剑指offer/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/README -------------------------------------------------------------------------------- /剑指offer/面试题03——二维数组中的查找/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题03——二维数组中的查找/README -------------------------------------------------------------------------------- /剑指offer/面试题03——二维数组中的查找/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define N 1000 3 | int input[N][N]; 4 | int m, n, num; 5 | 6 | int IsExist(int arr, int cols) 7 | { 8 | while (arr < n && cols >=0) 9 | { 10 | if (input[arr][cols] == num) 11 | return 1; 12 | else if (input[arr][cols] > num) 13 | { 14 | cols--; 15 | } 16 | else 17 | { 18 | arr++; 19 | } 20 | } 21 | return 0; 22 | } 23 | int main() 24 | { 25 | //freopen("test.txt", "r", stdin); 26 | 27 | int i, j; 28 | 29 | while(scanf("%d%d", &n, &m) != EOF) 30 | { 31 | scanf("%d", &num); 32 | for (i = 0; i < n; i++) 33 | for (j = 0; j < m; j++) 34 | { 35 | scanf("%d", &input[i][j]); 36 | } 37 | if (IsExist(0, m-1)) 38 | printf("Yes\n"); 39 | else 40 | printf("No\n"); 41 | } 42 | return 0; 43 | } 44 | 45 | /************************************************************** 46 | Problem: 1384 47 | User: lintingbin2009 48 | Language: C 49 | Result: Accepted 50 | Time:690 ms 51 | Memory:4820 kb 52 | ****************************************************************/ 53 | -------------------------------------------------------------------------------- /剑指offer/面试题04——替换空格/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题04——替换空格/README -------------------------------------------------------------------------------- /剑指offer/面试题04——替换空格/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define N 1000000 4 | int main() 5 | { 6 | char input[N]; 7 | int len, i, count; 8 | 9 | while (gets(input) != NULL) 10 | { 11 | 12 | count = 0; 13 | len = strlen(input); 14 | for (i = 0; i < len; i++) 15 | if (input[i] == ' ') 16 | count += 1; 17 | count = count * 2; 18 | for (i = len; i >= 0; i--) 19 | { 20 | if (input[i] == ' ') 21 | { 22 | input[i+count] = '0'; 23 | input[i+--count] = '2'; 24 | input[i+--count] = '%'; 25 | } 26 | else 27 | { 28 | input[i+count] = input[i]; 29 | } 30 | } 31 | printf("%s\n", input); 32 | } 33 | 34 | return 0; 35 | } 36 | 37 | /************************************************************** 38 | Problem: 1510 39 | User: lintingbin2009 40 | Language: C 41 | Result: Accepted 42 | Time:10 ms 43 | Memory:1820 kb 44 | ****************************************************************/ 45 | -------------------------------------------------------------------------------- /剑指offer/面试题05——从头到尾打印链表/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题05——从头到尾打印链表/README -------------------------------------------------------------------------------- /剑指offer/面试题05——从头到尾打印链表/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct node 4 | { 5 | int data; 6 | struct node *next; 7 | }; 8 | 9 | int main() 10 | { 11 | //freopen("test.txt", "r", stdin); 12 | int input; 13 | struct node *head, *cur, *tmp; 14 | 15 | head = (struct node *)malloc(sizeof(struct node)); 16 | cur = head; 17 | scanf("%d", &input); 18 | while (input != -1) 19 | { 20 | tmp = (struct node *)malloc(sizeof(struct node)); 21 | tmp->data = input; 22 | tmp->next = NULL; 23 | cur->next = tmp; 24 | cur = cur->next; 25 | scanf("%d", &input); 26 | } 27 | cur = head->next; 28 | head->next = NULL; 29 | while (cur) 30 | { 31 | tmp = head->next; 32 | head->next = cur; 33 | cur = cur->next; 34 | head->next->next = tmp; 35 | } 36 | cur = head->next; 37 | while (cur) 38 | { 39 | printf("%d\n", cur->data); 40 | cur = cur->next; 41 | } 42 | return 0; 43 | } 44 | 45 | /************************************************************** 46 | Problem: 1511 47 | User: lintingbin2009 48 | Language: C 49 | Result: Accepted 50 | Time:90 ms 51 | Memory:3948 kb 52 | ****************************************************************/ 53 | -------------------------------------------------------------------------------- /剑指offer/面试题06——重建二叉树/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题06——重建二叉树/README -------------------------------------------------------------------------------- /剑指offer/面试题06——重建二叉树/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define N 1001 3 | int CanBiCreate; 4 | struct node 5 | { 6 | int data; 7 | struct node *lchild; 8 | struct node *rchild; 9 | }; 10 | 11 | struct node * reCreate(int *pre_order, int *mid_order, int count) 12 | { 13 | struct node *root; 14 | int subCount = 0; 15 | 16 | if (count < 1) 17 | return NULL; 18 | while (*pre_order != *(mid_order+subCount)) 19 | { 20 | subCount++; 21 | if (subCount >= count) 22 | { 23 | CanBiCreate = 0; 24 | return NULL; 25 | } 26 | } 27 | root = malloc(sizeof(struct node)); 28 | root->data = *pre_order; 29 | root->lchild = reCreate(pre_order+1, mid_order, subCount); 30 | root->rchild = reCreate(pre_order+subCount+1, mid_order+1+subCount, count-subCount-1); 31 | return root; 32 | } 33 | void succ_order(struct node *tree) 34 | { 35 | if (tree == NULL) 36 | return; 37 | succ_order(tree->lchild); 38 | succ_order(tree->rchild); 39 | printf("%d ", tree->data); 40 | } 41 | int main() 42 | { 43 | //freopen("test.txt", "r", stdin); 44 | int count, pre_order[N], mid_order[N]; 45 | int i; 46 | struct node *tree; 47 | 48 | while (scanf("%d", &count) != EOF) 49 | { 50 | for (i = 0; i < count; i++) 51 | { 52 | scanf("%d", &pre_order[i]); 53 | } 54 | for (i = 0; i < count; i++) 55 | { 56 | scanf("%d", &mid_order[i]); 57 | } 58 | CanBiCreate = 1; 59 | tree = reCreate(pre_order, mid_order, count); 60 | if (CanBiCreate) 61 | succ_order(tree); 62 | else 63 | printf("No"); 64 | printf("\n"); 65 | } 66 | return 0; 67 | } 68 | 69 | /************************************************************** 70 | Problem: 1385 71 | User: lintingbin2009 72 | Language: C 73 | Result: Accepted 74 | Time:0 ms 75 | Memory:912 kb 76 | ****************************************************************/ 77 | -------------------------------------------------------------------------------- /剑指offer/面试题07——用两个栈实现队列/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题07——用两个栈实现队列/README -------------------------------------------------------------------------------- /剑指offer/面试题07——用两个栈实现队列/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define N 100000 4 | 5 | int main() 6 | { 7 | //freopen("test.txt", "r", stdin); 8 | int count, i, stack0[N], stack1[N], top0, top1; 9 | char opt[10]; 10 | 11 | while (scanf("%d", &count) != EOF) 12 | { 13 | top0 = top1 = -1; 14 | for (i = 0; i < count; i++) 15 | { 16 | scanf("%s", opt); 17 | if (strcmp(opt, "PUSH") == 0) 18 | { 19 | scanf("%d", &stack0[++top0]); 20 | } 21 | else 22 | { 23 | if (top1 != -1) 24 | printf("%d\n", stack1[top1--]); 25 | else if (top0 == -1) 26 | printf("-1\n"); 27 | else 28 | { 29 | while(top0 != -1) 30 | stack1[++top1] = stack0[top0--]; 31 | printf("%d\n", stack1[top1--]); 32 | } 33 | } 34 | } 35 | } 36 | return 0; 37 | } 38 | 39 | /************************************************************** 40 | Problem: 1512 41 | User: lintingbin2009 42 | Language: C 43 | Result: Accepted 44 | Time:70 ms 45 | Memory:1620 kb 46 | ****************************************************************/ 47 | -------------------------------------------------------------------------------- /剑指offer/面试题08——旋转数组的最小数字/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题08——旋转数组的最小数字/README -------------------------------------------------------------------------------- /剑指offer/面试题08——旋转数组的最小数字/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define N 1000000 3 | 4 | int main() 5 | { 6 | //freopen("test.txt", "r", stdin); 7 | int count, i, input[N]; 8 | 9 | while (scanf("%d", &count) != EOF) 10 | { 11 | for (i = 0; i < count; i++) 12 | scanf("%d", &input[i]); 13 | for (i = count - 1; i > 0; i--) 14 | { 15 | if (input[i] >= input[i-1]) 16 | continue; 17 | if (input[i] < input[i-1]) 18 | { 19 | printf("%d\n", input[i]); 20 | break; 21 | } 22 | } 23 | if (i == 0) 24 | printf("%d\n", input[0]); 25 | } 26 | return 0; 27 | } 28 | 29 | /************************************************************** 30 | Problem: 1386 31 | User: lintingbin2009 32 | Language: C 33 | Result: Accepted 34 | Time:690 ms 35 | Memory:4744 kb 36 | ****************************************************************/ 37 | -------------------------------------------------------------------------------- /剑指offer/面试题09——斐波那契数列/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题09——斐波那契数列/README -------------------------------------------------------------------------------- /剑指offer/面试题09——斐波那契数列/main.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题09——斐波那契数列/main.c -------------------------------------------------------------------------------- /剑指offer/面试题09(变形)——变态跳台阶/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题09(变形)——变态跳台阶/README -------------------------------------------------------------------------------- /剑指offer/面试题09(变形)——变态跳台阶/main.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题09(变形)——变态跳台阶/main.c -------------------------------------------------------------------------------- /剑指offer/面试题09(变形)——矩形覆盖/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题09(变形)——矩形覆盖/README -------------------------------------------------------------------------------- /剑指offer/面试题09(变形)——矩形覆盖/main.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题09(变形)——矩形覆盖/main.c -------------------------------------------------------------------------------- /剑指offer/面试题09(变形)——跳台阶/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题09(变形)——跳台阶/README -------------------------------------------------------------------------------- /剑指offer/面试题09(变形)——跳台阶/main.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题09(变形)——跳台阶/main.c -------------------------------------------------------------------------------- /剑指offer/面试题10——二进制中1的个数/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题10——二进制中1的个数/README -------------------------------------------------------------------------------- /剑指offer/面试题10——二进制中1的个数/main.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题10——二进制中1的个数/main.c -------------------------------------------------------------------------------- /剑指offer/面试题12——打印1到最大的N位数/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题12——打印1到最大的N位数/README -------------------------------------------------------------------------------- /剑指offer/面试题12——打印1到最大的N位数/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | //freopen("test.txt", "r", stdin); 6 | int count; 7 | long long max, num; 8 | 9 | while (scanf("%d", &count) != EOF) 10 | { 11 | max = num = 1; 12 | while (count--) 13 | { 14 | max *=10; 15 | } 16 | while (--max) 17 | printf("%lld\n", num++); 18 | 19 | } 20 | return 0; 21 | } 22 | /************************************************************** 23 | Problem: 1515 24 | User: lintingbin2009 25 | Language: C 26 | Result: Accepted 27 | Time:40 ms 28 | Memory:912 kb 29 | ****************************************************************/ 30 | -------------------------------------------------------------------------------- /剑指offer/面试题14——调整数组顺序使奇数位于偶数前面/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题14——调整数组顺序使奇数位于偶数前面/README -------------------------------------------------------------------------------- /剑指offer/面试题14——调整数组顺序使奇数位于偶数前面/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define N 100000 3 | 4 | int main() 5 | { 6 | //freopen("test.txt", "r", stdin); 7 | int count, input[N], output[N], i, j; 8 | 9 | while (scanf("%d", &count) != EOF) 10 | { 11 | for (i = 0; i < count; i++) 12 | scanf("%d", &input[i]); 13 | for (i = j = 0; i < count; i++) 14 | if ( input[i]%2 != 0) 15 | output[j++] = input[i]; 16 | for (i = 0; i < count; i++) 17 | if ( input[i]%2 == 0) 18 | output[j++] = input[i]; 19 | for (i = 0; i < count-1; i++) 20 | printf("%d ", output[i]); 21 | printf("%d", output[count-1]); 22 | printf("\n"); 23 | } 24 | return 0; 25 | } 26 | 27 | /************************************************************** 28 | Problem: 1516 29 | User: lintingbin2009 30 | Language: C 31 | Result: Accepted 32 | Time:80 ms 33 | Memory:1624 kb 34 | ****************************************************************/ 35 | -------------------------------------------------------------------------------- /剑指offer/面试题15——链表中倒数第k个结点/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题15——链表中倒数第k个结点/README -------------------------------------------------------------------------------- /剑指offer/面试题15——链表中倒数第k个结点/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct node 4 | { 5 | int data; 6 | struct node *next; 7 | }; 8 | 9 | int main() 10 | { 11 | //freopen("test.txt", "r", stdin); 12 | int count, pos, i; 13 | struct node *head, *cur, *tmp; 14 | 15 | while (scanf("%d %d", &count, &pos) != EOF) 16 | { 17 | if (pos > count) 18 | pos = 0; 19 | head = malloc(sizeof(struct node)); 20 | scanf("%d", &(head->data)); 21 | cur = head; 22 | for (i = 0; i < count-1; i++) 23 | { 24 | cur->next = malloc(sizeof(struct node)); 25 | cur = cur->next; 26 | scanf("%d", &(cur->data)); 27 | } 28 | cur->next = NULL; 29 | tmp = cur = head; 30 | for (i = 0; i < pos && tmp != NULL; i++) 31 | { 32 | tmp = tmp->next; 33 | } 34 | while (tmp) 35 | { 36 | cur = cur->next; 37 | tmp = tmp->next; 38 | } 39 | if (cur) 40 | printf("%d\n", cur->data); 41 | else 42 | printf("NULL\n"); 43 | while (head) 44 | { 45 | cur = head; 46 | head = head->next; 47 | free(cur); 48 | } 49 | } 50 | return 0; 51 | } 52 | 53 | /************************************************************** 54 | Problem: 1517 55 | User: lintingbin2009 56 | Language: C 57 | Result: Accepted 58 | Time:100 ms 59 | Memory:912 kb 60 | ****************************************************************/ 61 | -------------------------------------------------------------------------------- /剑指offer/面试题16——反转链表/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题16——反转链表/README -------------------------------------------------------------------------------- /剑指offer/面试题16——反转链表/main.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题16——反转链表/main.c -------------------------------------------------------------------------------- /剑指offer/面试题17——合并两个排序的链表/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题17——合并两个排序的链表/README -------------------------------------------------------------------------------- /剑指offer/面试题17——合并两个排序的链表/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct node 4 | { 5 | int data; 6 | struct node *next; 7 | }; 8 | 9 | int main() 10 | { 11 | //freopen("test.txt", "r", stdin); 12 | int n, m, i; 13 | struct node *head0, *head1, *head, *cur, *tmp; 14 | 15 | while (scanf("%d %d", &n, &m) != EOF) 16 | { 17 | if (n <= 0) 18 | head0 = NULL; 19 | else 20 | { 21 | head0 = malloc(sizeof(struct node)); 22 | scanf("%d", &(head0->data)); 23 | cur = head0; 24 | for (i = 0; i < n-1; i++) 25 | { 26 | cur->next = malloc(sizeof(struct node)); 27 | cur = cur->next; 28 | scanf("%d", &(cur->data)); 29 | } 30 | cur->next = NULL; 31 | } 32 | if (m <= 0) 33 | head1 = NULL; 34 | else 35 | { 36 | head1 = malloc(sizeof(struct node)); 37 | scanf("%d", &(head1->data)); 38 | cur = head1; 39 | for (i = 0; i < m-1; i++) 40 | { 41 | cur->next = malloc(sizeof(struct node)); 42 | cur = cur->next; 43 | scanf("%d", &(cur->data)); 44 | } 45 | cur->next = NULL; 46 | } 47 | 48 | head = malloc(sizeof(struct node)); 49 | head->next = NULL; 50 | cur = head; 51 | while (head0&&head1) 52 | { 53 | if (head0->data < head1->data) 54 | { 55 | cur->next = head0; 56 | head0 = head0->next; 57 | } 58 | else 59 | { 60 | cur->next = head1; 61 | head1 = head1->next; 62 | } 63 | cur = cur->next; 64 | } 65 | if (!head0) 66 | cur->next = head1; 67 | if (!head1) 68 | cur->next = head0; 69 | cur = head->next; 70 | 71 | if (cur) 72 | { 73 | while (cur->next) 74 | { 75 | printf("%d ", cur->data); 76 | cur = cur->next; 77 | } 78 | printf("%d", cur->data); 79 | } 80 | else 81 | printf("NULL"); 82 | printf("\n"); 83 | while (head) 84 | { 85 | cur = head; 86 | head = head->next; 87 | free(cur); 88 | } 89 | } 90 | return 0; 91 | } 92 | 93 | /************************************************************** 94 | Problem: 1519 95 | User: lintingbin2009 96 | Language: C 97 | Result: Accepted 98 | Time:230 ms 99 | Memory:912 kb 100 | ****************************************************************/ 101 | -------------------------------------------------------------------------------- /剑指offer/面试题19——二叉树的镜像/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题19——二叉树的镜像/README -------------------------------------------------------------------------------- /剑指offer/面试题19——二叉树的镜像/main.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题19——二叉树的镜像/main.c -------------------------------------------------------------------------------- /剑指offer/面试题20——顺时针打印矩阵/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题20——顺时针打印矩阵/README -------------------------------------------------------------------------------- /剑指offer/面试题20——顺时针打印矩阵/main.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题20——顺时针打印矩阵/main.c -------------------------------------------------------------------------------- /剑指offer/面试题21——包含min函数的栈/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题21——包含min函数的栈/README -------------------------------------------------------------------------------- /剑指offer/面试题21——包含min函数的栈/main.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题21——包含min函数的栈/main.c -------------------------------------------------------------------------------- /剑指offer/面试题22——栈的压入、弹出序列/README: -------------------------------------------------------------------------------- 1 | 题目描述: 2 | 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。 3 | 输入: 4 | 每个测试案例包括3行: 5 | 第一行为1个整数n(1<=n<=100000),表示序列的长度。 6 | 第二行包含n个整数,表示栈的压入顺序。 7 | 第三行包含n个整数,表示栈的弹出顺序。 8 | 输出: 9 | 对应每个测试案例,如果第二个序列是第一个序列的弹出序列输出Yes,否则输出No。 10 | 样例输入: 11 | 5 12 | 1 2 3 4 5 13 | 4 5 3 2 1 14 | 5 15 | 1 2 3 4 5 16 | 4 3 5 1 2 17 | 样例输出: 18 | Yes 19 | No 20 | -------------------------------------------------------------------------------- /剑指offer/面试题22——栈的压入、弹出序列/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define N 100001 3 | int stack[N], input[N]; 4 | 5 | int main() 6 | { 7 | //freopen("/home/lintingbin/test.txt", "r", stdin); 8 | int n, i, j, top, tmp, Yes; 9 | 10 | while (scanf("%d", &n) != EOF) 11 | { 12 | Yes = 1; 13 | top = -1; 14 | for (i = 0; i < n; i++) 15 | scanf("%d", &input[i]); 16 | for (i = j = 0; i < n; i++) 17 | { 18 | scanf("%d", &tmp); 19 | if (tmp == input[j] && j < n) 20 | { 21 | j++; 22 | continue; 23 | } 24 | if (stack[top] == tmp && top > -1) 25 | { 26 | top--; 27 | continue; 28 | } 29 | while (stack[top] != tmp && j < n) 30 | stack[++top] = input[j++]; 31 | if (j >= n-1 && stack[top] != tmp) 32 | { 33 | Yes = 0;//这里不能加break,不然就不能吸收全部的数 34 | } 35 | if (stack[top] == tmp) 36 | top--; 37 | } 38 | if (Yes) 39 | printf("Yes\n"); 40 | else 41 | printf("No\n"); 42 | } 43 | return 0; 44 | } 45 | /************************************************************** 46 | Problem: 1366 47 | User: lintingbin2009 48 | Language: C++ 49 | Result: Accepted 50 | Time:190 ms 51 | Memory:1800 kb 52 | ****************************************************************/ 53 | -------------------------------------------------------------------------------- /剑指offer/面试题23——从上往下打印二叉树/README: -------------------------------------------------------------------------------- 1 | 题目描述: 2 | 从上往下打印出二叉树的每个节点,同层节点从左至右打印。 3 | 输入: 4 | 输入可能包含多个测试样例,输入以EOF结束。 5 | 对于每个测试案例,输入的第一行一个整数n(1<=n<=1000, :n代表将要输入的二叉树元素的个数(节点从1开始编号)。接下来一行有n个数字,代表第i个二叉树节点的元素的值。接下来有n行,每行有一个字母Ci。 6 | Ci=’d’表示第i个节点有两子孩子,紧接着是左孩子编号和右孩子编号。 7 | Ci=’l’表示第i个节点有一个左孩子,紧接着是左孩子的编号。 8 | Ci=’r’表示第i个节点有一个右孩子,紧接着是右孩子的编号。 9 | Ci=’z’表示第i个节点没有子孩子。 10 | 输出: 11 | 对应每个测试案例, 12 | 按照从上之下,从左至右打印出二叉树节点的值。 13 | 样例输入: 14 | 7 15 | 8 6 5 7 10 9 11 16 | d 2 5 17 | d 3 4 18 | z 19 | z 20 | d 6 7 21 | z 22 | z 23 | 样例输出: 24 | 8 6 10 5 7 9 11 25 | -------------------------------------------------------------------------------- /剑指offer/面试题23——从上往下打印二叉树/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题23——从上往下打印二叉树/a.out -------------------------------------------------------------------------------- /剑指offer/面试题23——从上往下打印二叉树/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define N 1001 3 | 4 | struct node 5 | { 6 | int data; 7 | struct node *lchild; 8 | struct node *rchild; 9 | }; 10 | void level_order(struct node* tree, int n) 11 | { 12 | struct node *quence[N], *tmp; 13 | int head, tail; 14 | 15 | tail = -1; 16 | head = 0; 17 | if (tree == NULL) 18 | return; 19 | quence[head++] = tree; 20 | while (tail < head - 1) 21 | { 22 | n--; 23 | tmp = quence[++tail]; 24 | if (tmp->lchild != NULL) 25 | quence[head++] = tmp->lchild; 26 | if (tmp->rchild != NULL) 27 | quence[head++] = tmp->rchild; 28 | if(n) 29 | printf("%d ", tmp->data); 30 | else 31 | printf("%d", tmp->data); 32 | } 33 | } 34 | 35 | int main() 36 | { 37 | //freopen("/home/lintingbin/test.txt", "r", stdin); 38 | int n, i, tmp; 39 | char opt; 40 | struct node *input[N]; 41 | 42 | while (scanf("%d", &n) != EOF) 43 | { 44 | if (n < 1) 45 | { 46 | printf("NULL\n"); 47 | continue; 48 | } 49 | for (i = 0; i < n; i++) 50 | { 51 | input[i] = malloc(sizeof(struct node)); 52 | scanf("%d", &(input[i]->data)); 53 | } 54 | for (i = 0; i < n; i++) 55 | { 56 | scanf(" %c", &opt); 57 | switch (opt) 58 | { 59 | case 'd': 60 | scanf("%d", &tmp); 61 | input[i]->lchild = input[tmp-1]; 62 | scanf("%d", &tmp); 63 | input[i]->rchild = input[tmp-1]; 64 | break; 65 | case 'l': 66 | scanf("%d", &tmp); 67 | input[i]->lchild = input[tmp-1]; 68 | input[i]->rchild = NULL; 69 | break; 70 | case 'r': 71 | scanf("%d", &tmp); 72 | input[i]->rchild = input[tmp-1]; 73 | input[i]->lchild = NULL; 74 | break; 75 | case 'z': 76 | input[i]->rchild = NULL; 77 | input[i]->lchild = NULL; 78 | break; 79 | } 80 | } 81 | level_order(input[0], n); 82 | printf("\n"); 83 | } 84 | return 0; 85 | } 86 | /************************************************************** 87 | Problem: 1523 88 | User: lintingbin2009 89 | Language: C 90 | Result: Accepted 91 | Time:0 ms 92 | Memory:912 kb 93 | ****************************************************************/ 94 | -------------------------------------------------------------------------------- /剑指offer/面试题24——二叉搜索树的后序遍历序列/README: -------------------------------------------------------------------------------- 1 | 题目描述: 2 | 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。 3 | 输入: 4 | 每个测试案例包括2行: 5 | 第一行为1个整数n(1<=n<=10000),表示数组的长度。 6 | 第二行包含n个整数,表示这个数组,数组中的数的范围是[0,100000000]。 7 | 输出: 8 | 对应每个测试案例,如果输入数组是某二叉搜索树的后序遍历的结果输出Yes,否则输出No。 9 | 样例输入: 10 | 7 11 | 5 7 6 9 11 10 8 12 | 4 13 | 7 4 6 5 14 | 样例输出: 15 | Yes 16 | No 17 | -------------------------------------------------------------------------------- /剑指offer/面试题24——二叉搜索树的后序遍历序列/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define N 10001 3 | 4 | int input[N]; 5 | 6 | int main() 7 | { 8 | //freopen("/home/lintingbin/test.txt", "r", stdin); 9 | int n, i, j, k, Yes; 10 | 11 | while (scanf("%d", &n) != EOF) 12 | { 13 | for (i = 0; i < n; i++) 14 | scanf("%d", &input[i]); 15 | Yes = 1; 16 | for (i = n-1; i >=0; i--) 17 | { 18 | k = -1; 19 | for (j = i - 1; i >= 0 && j >= 0; j--) 20 | { 21 | if (input[j] < input[i]) 22 | { 23 | k = j; 24 | break; 25 | } 26 | } 27 | for (j = 0; j <= k; j++) 28 | if (input[j] > input[i]) 29 | Yes = 0; 30 | for (j = k+1; j < i; j++) 31 | if (input[j] < input[i]) 32 | Yes = 0; 33 | } 34 | if (Yes) 35 | printf("Yes\n"); 36 | else 37 | printf("No\n"); 38 | } 39 | return 0; 40 | } 41 | 42 | /************************************************************** 43 | Problem: 1367 44 | User: lintingbin2009 45 | Language: C 46 | Result: Accepted 47 | Time:70 ms 48 | Memory:952 kb 49 | ****************************************************************/ 50 | -------------------------------------------------------------------------------- /剑指offer/面试题25——二叉树中和为某一值的路径/README: -------------------------------------------------------------------------------- 1 | 题目描述: 2 | 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。 3 | 输入: 4 | 每个测试案例包括n+1行: 5 | 第一行为2个整数n,k(1<=n<=10000),n表示结点的个数,k表示要求的路径和,结点编号从1到n。 6 | 接下来有n行。这n行中每行为3个整数vi,leftnode,rightnode,vi表示第i个结点的值,leftnode表示第i个结点的左孩子结点编号,rightnode表示第i个结点的右孩子结点编号,若无结点值为-1。编号为1的结点为根结点。 7 | 输出: 8 | 对应每个测试案例,先输出“result:”占一行,接下来按字典顺序输出满足条件的所有路径,这些路径由结点编号组成,输出格式参照输出样例。 9 | 样例输入: 10 | 5 22 11 | 10 2 3 12 | 5 4 5 13 | 12 -1 -1 14 | 4 -1 -1 15 | 7 -1 -1 16 | 1 5 17 | 1 -1 -1 18 | 样例输出: 19 | result: 20 | A path is found: 1 2 5 21 | A path is found: 1 3 22 | result: 23 | 24 | 遇到的问题: 25 | 这道题目主要是测试用例一一直通过不了,花了快两个小时的时间来做了。 26 | 上面的要求为按字典顺序输出,所以要把小的先输出,然后再输出大的,题目的要求一定要先弄清楚。 27 | 28 | 还有树的索引,这道题目不适合用指针来当索引,要用数值的下标来当孩子节点的索引。 29 | -------------------------------------------------------------------------------- /剑指offer/面试题25——二叉树中和为某一值的路径/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define N 10001 3 | 4 | struct node { 5 | int data; 6 | int lchild; 7 | int rchild; 8 | }; 9 | 10 | struct node input[N]; 11 | int path[N]; 12 | 13 | void pre_order(int n, int k, int count) 14 | { 15 | int i, tmp; 16 | if (n == 1) 17 | path[count] = 1; 18 | k -= input[n].data; 19 | if (k < 0) 20 | return; 21 | if ((input[n].lchild) == -1 && (input[n].rchild) == -1 && 0 == k) 22 | { 23 | printf("A path is found:"); 24 | for (i = 1; i <= count; i++) 25 | printf(" %d", path[i]); 26 | printf("\n"); 27 | } 28 | if (input[n].lchild > input[n].rchild)//小的要先输出 29 | { 30 | tmp = input[n].lchild; 31 | input[n].lchild = input[n].rchild; 32 | input[n].rchild = tmp; 33 | } 34 | 35 | if (input[n].lchild != -1) 36 | { 37 | path[count+1] = input[n].lchild; 38 | pre_order(input[n].lchild, k, count+1); 39 | } 40 | if (input[n].rchild != -1) 41 | { 42 | path[count+1] = input[n].rchild; 43 | pre_order(input[n].rchild, k, count+1); 44 | } 45 | } 46 | 47 | int main() 48 | { 49 | //freopen("/home/lintingbin/test.txt", "r", stdin); 50 | int n, k, i; 51 | 52 | while (scanf("%d%d", &n, &k) != EOF) 53 | { 54 | for (i = 1; i <= n; i++) 55 | scanf("%d%d%d", &(input[i].data), &(input[i].lchild), &(input[i].rchild)); 56 | printf("result:\n"); 57 | if (n > 0) 58 | pre_order(1, k, 1); 59 | } 60 | return 0; 61 | } 62 | 63 | /************************************************************** 64 | Problem: 1368 65 | User: lintingbin2009 66 | Language: C 67 | Result: Accepted 68 | Time:30 ms 69 | Memory:1068 kb 70 | ****************************************************************/ 71 | -------------------------------------------------------------------------------- /剑指offer/面试题27——WA二叉搜索树与双向链表/README: -------------------------------------------------------------------------------- 1 | 题目描述: 2 | 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。 3 | 输入: 4 | 输入可能包含多个测试样例。 5 | 对于每个测试案例,输入的第一行为一个数n(0 2 | #include 3 | 4 | struct node { 5 | int data; 6 | struct node *lchild; 7 | struct node *rchild; 8 | }; 9 | 10 | struct node *CreatTree() 11 | { 12 | struct node *root = NULL; 13 | int tmp; 14 | 15 | scanf("%d", &tmp); 16 | if (tmp != 0) 17 | { 18 | root = malloc(sizeof(struct node)); 19 | root->data = tmp; 20 | root->lchild = CreatTree(); 21 | root->rchild = CreatTree(); 22 | } 23 | return root; 24 | } 25 | void tree_to_link(struct node *root) 26 | { 27 | struct node *p, *q; 28 | 29 | if (root != NULL) 30 | { 31 | p = root->lchild; 32 | if (p != NULL && p->rchild != NULL) 33 | p = p->rchild; 34 | q = root->rchild; 35 | if (q != NULL && q->lchild != NULL) 36 | q = q->lchild; 37 | tree_to_link(root->lchild); 38 | tree_to_link(root->rchild); 39 | if (p != NULL) 40 | { 41 | p->rchild = root; 42 | root->lchild = p; 43 | } 44 | if (q != NULL) 45 | { 46 | q->lchild = root; 47 | root->rchild = q; 48 | } 49 | } 50 | } 51 | 52 | int main() 53 | { 54 | //freopen("/home/lintingbin/test.txt", "r", stdin); 55 | int n, i, tmp; 56 | struct node *tree, *head; 57 | 58 | while (scanf("%d", &n) != EOF) 59 | { 60 | for (i = 0; i < n; i++) 61 | { 62 | head = tree = CreatTree(); 63 | tree_to_link(tree); 64 | while (head != NULL && head->lchild != NULL) 65 | head = head->lchild; 66 | while (head != NULL) 67 | { 68 | printf("%d ", head->data); 69 | head = head->rchild; 70 | } 71 | printf("\n"); 72 | } 73 | } 74 | return 0; 75 | } 76 | 77 | /************************************************************** 78 | Problem: 1503 79 | User: lintingbin2009 80 | Language: C 81 | Result: Wrong Answer 82 | ****************************************************************/ 83 | -------------------------------------------------------------------------------- /剑指offer/面试题28——字符串的排列/README: -------------------------------------------------------------------------------- 1 | 题目描述: 2 | 输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。 3 | 输入: 4 | 每个测试案例包括1行。 5 | 输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。 6 | 输出: 7 | 对应每组数据,按字典序输出所有排列。 8 | 样例输入: 9 | abc 10 | BCA 11 | 样例输出: 12 | abc 13 | acb 14 | bac 15 | bca 16 | cab 17 | cba 18 | ABC 19 | ACB 20 | BAC 21 | BCA 22 | CAB 23 | CBA 24 | 25 | 主要问题: 26 | 1、要按照字典的顺序,就不能用交换的方法来排列 27 | 2、处理重复的字符,比如122只有122、212、221三种排列,而不是六种 28 | -------------------------------------------------------------------------------- /剑指offer/面试题28——字符串的排列/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define N 11 4 | 5 | char result[N]; 6 | 7 | void swap(char *a, char *b) 8 | { 9 | char tmp = *a; 10 | *a = *b; 11 | *b = tmp; 12 | } 13 | 14 | void sort(char *str, int len) 15 | { 16 | int chart[200], i, j; 17 | 18 | memset(chart, 0, 200*sizeof(int)); 19 | for (i = 0; i < len; i++) 20 | chart[str[i]]++; 21 | for (i = j = 0; i < 200; i++) 22 | while (chart[i]-- != 0) 23 | str[j++] = i; 24 | } 25 | int used[N] = {0}; 26 | int isRepeat(int *num, int len, int rep) 27 | { 28 | int i; 29 | for (i = 0; i < len; i++) 30 | if (rep == num[i]) 31 | return 1; 32 | return 0; 33 | } 34 | void pailie(char *str, int len, int index) 35 | { 36 | int i, j, repeat[N] = {0}; 37 | 38 | if (index == len) 39 | printf("%s\n", result); 40 | for (i = 0; i < len; i++) 41 | { 42 | if (used[i] == 1) 43 | continue; 44 | result[index] = str[i]; 45 | if (isRepeat(repeat, i, str[i])) 46 | continue; 47 | repeat[i] = str[i]; 48 | used[i] = 1; 49 | pailie(str, len, index+1); 50 | used[i] = 0; 51 | } 52 | } 53 | 54 | int main() 55 | { 56 | //freopen("/home/lintingbin/test.txt", "r", stdin); 57 | char str[N]; 58 | int len; 59 | 60 | while (scanf("%s", str) != EOF) 61 | { 62 | memset(used, 0, N*sizeof(int)); 63 | len = strlen(str); 64 | result[len] = '\0'; 65 | sort(str, len); 66 | pailie(str, len, 0); 67 | } 68 | return 0; 69 | } 70 | 71 | -------------------------------------------------------------------------------- /剑指offer/面试题28——字符串的排列/二叉搜索树与双向链表.url: -------------------------------------------------------------------------------- 1 | [InternetShortcut] 2 | URL=http://ac.jobdu.com/problem.php?pid=1503 3 | -------------------------------------------------------------------------------- /剑指offer/面试题29——数组中出现次数超过一半的数字/README: -------------------------------------------------------------------------------- 1 | 题目描述: 2 | 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。 3 | 输入: 4 | 每个测试案例包括2行: 5 | 第一行输入一个整数n(1<=n<=100000),表示数组中元素的个数。 6 | 第二行输入n个整数,表示数组中的每个元素,这n个整数的范围是[1,1000000000]。 7 | 输出: 8 | 对应每个测试案例,输出出现的次数超过数组长度的一半的数,如果没有输出-1。 9 | 样例输入: 10 | 9 11 | 1 2 3 2 2 2 5 4 2 12 | 样例输出: 13 | 2 14 | -------------------------------------------------------------------------------- /剑指offer/面试题29——数组中出现次数超过一半的数字/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define N 100001 3 | 4 | 5 | int input[N]; 6 | int main() 7 | { 8 | //freopen("/home/lintingbin/test.txt", "r", stdin); 9 | int n, i, num, count; 10 | 11 | while (scanf("%d", &n) != EOF) 12 | { 13 | for (i = 0; i < n; i++) 14 | scanf("%d", &input[i]); 15 | count = 0; 16 | for (i = 0; i < n; i++) 17 | { 18 | if (count == 0) 19 | { 20 | num = input[i]; 21 | count = 1; 22 | } 23 | else 24 | if (num != input[i]) //这边为什么不是input[i] != input[i-1] 25 | count--; 26 | else 27 | count++; 28 | } 29 | count = 0; 30 | for (i = 0; i < n; i++) 31 | if (input[i] == num) 32 | count++; 33 | if (2*count > n) 34 | printf("%d\n", num); 35 | else 36 | printf("-1\n"); 37 | } 38 | return 0; 39 | } 40 | 41 | -------------------------------------------------------------------------------- /剑指offer/面试题30——最小的K个数/README: -------------------------------------------------------------------------------- 1 | 题目描述: 2 | 输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。 3 | 输入: 4 | 每个测试案例包括2行: 5 | 第一行为2个整数n,k(1<=n,k<=200000),表示数组的长度。 6 | 第二行包含n个整数,表示这n个数,数组中的数的范围是[0,1000 000 000]。 7 | 输出: 8 | 对应每个测试案例,输出最小的k个数,并按从小到大顺序打印。 9 | 样例输入: 10 | 8 4 11 | 4 5 1 6 2 7 3 8 12 | 样例输出: 13 | 1 2 3 4 14 | 15 | 备注: 16 | 这道题目主要是要有运行时间的限制,如果先排序后再找k个小的数的话会运行不通过。 17 | main1.c主要的运行快速排序的思路来解决的,每次处理符合条件的那部分。本地测试可以通过,提交后只能通过一个测试。暂时不知道为什么。 18 | main.c用的是堆的思路来解决的,已经AC。 19 | -------------------------------------------------------------------------------- /剑指offer/面试题30——最小的K个数/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define N 200001 6 | 7 | int input[N]; 8 | int stack[N]; 9 | 10 | void swap(int *a, int *b) 11 | { 12 | int tmp = *a; 13 | *a = *b; 14 | *b = tmp; 15 | } 16 | 17 | void max_heapify(int *array, int index, int size) //保持大跟堆的性质 18 | { 19 | int l, r, largest; 20 | 21 | l = index*2; 22 | r = index*2 + 1; 23 | if (l <= size && array[l] > array[index]) 24 | largest = l; 25 | else 26 | largest = index; 27 | if (r <= size && array[r] > array[largest]) 28 | largest = r; 29 | if (largest != index) 30 | { 31 | swap(&array[largest], &array[index]); 32 | max_heapify(array, largest, size); 33 | } 34 | } 35 | 36 | void create_heap(int *array, int size) //建立大根堆 37 | { 38 | int i; 39 | 40 | for (i = size/2; i >= 1; i--) 41 | { 42 | max_heapify(array, i, size); 43 | } 44 | } 45 | 46 | int main() 47 | { 48 | //freopen("/home/lintingbin/test.txt", "r", stdin); 49 | int i, n, k, top; 50 | 51 | while (scanf("%d%d", &n, &k) != EOF) 52 | { 53 | for (i = 1; i <= n; i++) 54 | scanf("%d", &input[i]); 55 | create_heap(input, k); 56 | for (i = k + 1; i <= n; i++) 57 | { 58 | if (input[i] < input[1]) 59 | { 60 | input[1] = input[i]; 61 | max_heapify(input, 1, k); 62 | } 63 | } 64 | 65 | top = -1; 66 | for (i = k; i >= 1; i--) 67 | { 68 | stack[++top] = input[1]; 69 | input[1] = input[i]; 70 | max_heapify(input, 1, i-1); 71 | } 72 | 73 | while (top != 0) 74 | printf("%d ", stack[top--]); 75 | printf("%d", stack[top--]); 76 | 77 | printf("\n"); 78 | } 79 | 80 | return 0; 81 | } 82 | 83 | -------------------------------------------------------------------------------- /剑指offer/面试题30——最小的K个数/main1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define N 200001 3 | 4 | int input[N]; 5 | 6 | void swap(int *a, int *b) 7 | { 8 | int tmp = *a; 9 | *a = *b; 10 | *b = tmp; 11 | } 12 | 13 | void quick_sort(int *array, int start, int end) 14 | { 15 | int i, j; 16 | 17 | if (start > end) 18 | return; 19 | j = start - 1; 20 | for (i = start; i < end; i++) 21 | { 22 | if (array[i] < array[end]) 23 | { 24 | swap(&array[++j], &array[i]); 25 | } 26 | } 27 | swap(&array[++j], &array[end]); 28 | 29 | quick_sort(array, start, j-1); 30 | quick_sort(array, j+1, end); 31 | } 32 | void find_k(int *array, int start, int end, int k) //由上面的快速排序来改造 33 | { 34 | int i, j; 35 | 36 | if (start > end) 37 | return; 38 | j = start - 1; 39 | for (i = start; i < end; i++) 40 | { 41 | if (array[i] < array[end]) 42 | { 43 | swap(&array[++j], &array[i]); 44 | } 45 | } 46 | swap(&array[++j], &array[end]); 47 | if (j == k-1) 48 | return; 49 | else if (j > k-1) 50 | find_k(array, start, j-1, k); 51 | else 52 | find_k(array, j+1, end, k-j-1); 53 | } 54 | 55 | int main() 56 | { 57 | //freopen("/home/lintingbin/test.txt", "r", stdin); 58 | int i, n, k; 59 | 60 | while (scanf("%d%d", &n, &k) != EOF) 61 | { 62 | for (i = 0; i < n; i++) 63 | scanf("%d", &input[i]); 64 | find_k(input, 0, n-1, k+1); 65 | quick_sort(input, 0, k-1); 66 | for (i = 0; i < k - 1; i++) 67 | printf("%d ", input[i]); 68 | if (k > 0) 69 | printf("%d", input[k-1]); 70 | printf("\n"); 71 | } 72 | 73 | return 0; 74 | } 75 | 76 | -------------------------------------------------------------------------------- /剑指offer/面试题31——WA连续子数组的最大和/README: -------------------------------------------------------------------------------- 1 | 题目描述: 2 | HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学。今天JOBDU测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决。但是,如果向量中包含负数,是否应该包含某个负数,并期望旁边的正数会弥补它呢?例如:{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。你会不会被他忽悠住? 3 | 输入: 4 | 输入有多组数据,每组测试数据包括两行。 5 | 第一行为一个整数n(0<=n<=100000),当n=0时,输入结束。接下去的一行包含n个整数(我们保证所有整数属于[-1000,1000])。 6 | 输出: 7 | 对应每个测试案例,需要输出3个整数单独一行,分别表示连续子向量的最大和、该子向量的第一个元素的下标和最后一个元素的下标。若是存在多个子向量,则输出起始元素下标最小的那个。 8 | 样例输入: 9 | 3 10 | -1 -3 -2 11 | 5 12 | -8 3 2 0 5 13 | 8 14 | 6 -3 -2 7 -15 1 2 2 15 | 0 16 | 样例输出: 17 | -1 0 0 18 | 10 1 4 19 | 8 0 3 20 | 21 | 遇到问题: 22 | 首先自己要先模拟一下,然后再写代码。 23 | case2和case4过不了,不知道什么原因。 24 | -------------------------------------------------------------------------------- /剑指offer/面试题31——WA连续子数组的最大和/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题31——WA连续子数组的最大和/a.out -------------------------------------------------------------------------------- /剑指offer/面试题31——WA连续子数组的最大和/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define N 100001 3 | 4 | int input[N]; 5 | 6 | int main() 7 | { 8 | //freopen("/home/lintingbin/test.txt", "r", stdin); 9 | int n, i, start, end, sum, start_test, end_test, sum_test; 10 | 11 | while (scanf("%d", &n) != EOF) 12 | { 13 | for (i = 0; i < n; i++) 14 | scanf("%d", &input[i]); 15 | start = end = start_test = end_test = 0; 16 | sum = sum_test = input[0]; 17 | for (i = 1; i < n; i++) 18 | { 19 | if (sum_test < 0) 20 | { 21 | start_test = end_test = i; 22 | sum_test = input[i]; 23 | } 24 | else 25 | { 26 | sum_test += input[i]; 27 | end_test = i; 28 | } 29 | if (sum_test > sum || (sum_test==sum && (end_test-start_test) > (end-start))) 30 | { 31 | sum = sum_test; 32 | start = start_test; 33 | end = end_test; 34 | } 35 | } 36 | if (n > 0) 37 | printf("%d %d %d\n", sum, start, end); 38 | else 39 | printf("\n"); 40 | } 41 | 42 | 43 | 44 | return 0; 45 | } 46 | 47 | -------------------------------------------------------------------------------- /剑指offer/面试题32——PASS从1到n整数中1出现的次数/README: -------------------------------------------------------------------------------- 1 | 题目描述: 2 | 亲们!!我们的外国友人YZ这几天总是睡不好,初中奥数里有一个题目一直困扰着他,特此他向JOBDU发来求助信,希望亲们能帮帮他。问题是:求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1、10、11、12、13因此共出现6次,但是对于后面问题他就没辙了。ACMer希望你们帮帮他,并把问题更加普遍化,可以很快的求出任意非负整数区间中1出现的次数。 3 | 输入: 4 | 输入有多组数据,每组测试数据为一行。 5 | 每一行有两个整数a,b(0<=a,b<=1,000,000,000)。 6 | 输出: 7 | 对应每个测试案例,输出a和b之间1出现的次数。 8 | 样例输入: 9 | 0 5 10 | 1 13 11 | 21 55 12 | 31 99 13 | 样例输出: 14 | 1 15 | 6 16 | 4 17 | 7 18 | -------------------------------------------------------------------------------- /剑指offer/面试题33——PASS把数组排成最小的数/README: -------------------------------------------------------------------------------- 1 | 题目描述: 2 | 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。 3 | 输入: 4 | 输入可能包含多个测试样例。 5 | 对于每个测试案例,输入的第一行为一个整数m (1<=m <=100)代表输入的正整数的个数。 6 | 输入的第二行包括m个正整数,其中每个正整数不超过10000000。 7 | 输出: 8 | 对应每个测试案例, 9 | 输出m个数字能排成的最小数字。 10 | 样例输入: 11 | 3 12 | 23 13 6 13 | 2 14 | 23456 56 15 | 样例输出: 16 | 13236 17 | 2345656 18 | -------------------------------------------------------------------------------- /剑指offer/面试题33——把数组排成最小的数/README: -------------------------------------------------------------------------------- 1 | 题目描述: 2 | 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。 3 | 输入: 4 | 输入可能包含多个测试样例。 5 | 对于每个测试案例,输入的第一行为一个整数m (1<=m <=100)代表输入的正整数的个数。 6 | 输入的第二行包括m个正整数,其中每个正整数不超过10000000。 7 | 输出: 8 | 对应每个测试案例, 9 | 输出m个数字能排成的最小数字。 10 | 样例输入: 11 | 3 12 | 23 13 6 13 | 2 14 | 23456 56 15 | 样例输出: 16 | 13236 17 | 2345656 18 | -------------------------------------------------------------------------------- /剑指offer/面试题33——把数组排成最小的数/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题33——把数组排成最小的数/main -------------------------------------------------------------------------------- /剑指offer/面试题33——把数组排成最小的数/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define N 101 6 | 7 | int input[N]; 8 | 9 | int cmp(const void *a, const void *b)//比较两个数,看哪个数放在前面数会比较小 10 | { 11 | char str1[20], str2[20], tmp[20]; 12 | 13 | sprintf(str1, "%d", *(int*)a); 14 | sprintf(str2, "%d", *(int*)b); 15 | strcpy(tmp, str1); 16 | strcat(str1, str2); 17 | strcat(str2, tmp); 18 | return strcmp(str1, str2); 19 | } 20 | 21 | int main() 22 | { 23 | //freopen("/home/lin/test.txt", "r", stdin); 24 | int n, i; 25 | 26 | while (scanf("%d", &n) != EOF) { 27 | for (i = 0; i < n; i++) 28 | scanf("%d", &input[i]); 29 | qsort(input, n, sizeof(input[0]), cmp); 30 | for (i = 0; i < n; i++) 31 | printf("%d", input[i]); 32 | printf("\n"); 33 | } 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /剑指offer/面试题34——PASS丑数/README: -------------------------------------------------------------------------------- 1 | 题目描述: 2 | 把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。 3 | 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。 4 | 输入: 5 | 输入包括一个整数N(1<=N<=1500)。 6 | 输出: 7 | 可能有多组测试数据,对于每组数据, 8 | 输出第N个丑数。 9 | 样例输入: 10 | 3 11 | 样例输出: 12 | 3 13 | -------------------------------------------------------------------------------- /剑指offer/面试题35——第一个只出现一次的字符/README: -------------------------------------------------------------------------------- 1 | 题目描述: 2 | 在一个字符串(1<=字符串长度<=10000,全部由大写字母组成)中找到第一个只出现一次的字符。 3 | 输入: 4 | 输入有多组数据 5 | 每一组输入一个字符串。 6 | 输出: 7 | 输出第一个只出现一次的字符下标,没有只出现一次的字符则输出-1。 8 | 样例输入: 9 | ABACCDEFF 10 | AA 11 | 样例输出: 12 | 1 13 | -1 14 | -------------------------------------------------------------------------------- /剑指offer/面试题35——第一个只出现一次的字符/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define N 100 4 | #define MAX 10000 5 | 6 | 7 | int main() 8 | { 9 | //freopen("/home/lin/test.txt", "r", stdin); 10 | char input[MAX], ascii[N]; 11 | int len, find, i; 12 | 13 | while (scanf("%s", input) != EOF) { 14 | find = 0; 15 | memset(ascii, 0, 100); 16 | len = strlen(input); 17 | for (i = 0; i < len; i++) 18 | ascii[input[i]]++; 19 | for (i = 0; i < len; i++) { 20 | if (ascii[input[i]] == 1) { 21 | printf("%d\n", i); 22 | find = 1; 23 | break; 24 | } 25 | } 26 | if (!find) 27 | printf("%d\n", -1); 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /剑指offer/面试题36——PASS数组中的逆序对/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题36——PASS数组中的逆序对/README -------------------------------------------------------------------------------- /剑指offer/面试题37——两个链表的第一个公共结点/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题37——两个链表的第一个公共结点/README -------------------------------------------------------------------------------- /剑指offer/面试题37——两个链表的第一个公共结点/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define N 1001 4 | int first[N]; 5 | int second[N]; 6 | 7 | 8 | int main() 9 | { 10 | //freopen("test.txt", "r", stdin); 11 | int n, m, i, distance, index, value; 12 | 13 | while (scanf("%d%d", &n, &m) != EOF) 14 | { 15 | index = -1; 16 | for (i = 0; i < n; i++) 17 | scanf("%d", &first[i]); 18 | for (i = 0; i < m; i++) 19 | scanf("%d", &second[i]); 20 | if (n > m) 21 | { 22 | distance = n - m; 23 | for (i = 0; i < m; i++) 24 | { 25 | if (first[i+distance] == second[i]) 26 | { 27 | index = i; 28 | value = second[i]; 29 | break; 30 | } 31 | } 32 | } 33 | else 34 | { 35 | distance = m - n; 36 | for (i = 0; i < n; i++) 37 | { 38 | if (first[i] == second[i+distance]) 39 | { 40 | index = i; 41 | value = first[i]; 42 | break; 43 | } 44 | } 45 | } 46 | if (index == -1) 47 | printf("My God\n"); 48 | else 49 | printf("%d\n", value); 50 | 51 | } 52 | return 0; 53 | } 54 | 55 | -------------------------------------------------------------------------------- /剑指offer/面试题38——WA数字在排序数组中出现的次数/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题38——WA数字在排序数组中出现的次数/README -------------------------------------------------------------------------------- /剑指offer/面试题38——WA数字在排序数组中出现的次数/main.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题38——WA数字在排序数组中出现的次数/main.c -------------------------------------------------------------------------------- /剑指offer/面试题39——PASS二叉树的深度/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题39——PASS二叉树的深度/README -------------------------------------------------------------------------------- /剑指offer/面试题40——数组中只出现一次的数字/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题40——数组中只出现一次的数字/README -------------------------------------------------------------------------------- /剑指offer/面试题40——数组中只出现一次的数字/main.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题40——数组中只出现一次的数字/main.c -------------------------------------------------------------------------------- /剑指offer/面试题41——和为S的两个数字/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题41——和为S的两个数字/README -------------------------------------------------------------------------------- /剑指offer/面试题41——和为S的两个数字/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define N 1000001 3 | 4 | int input[N]; 5 | 6 | int main() 7 | { 8 | freopen("test.txt", "r", stdin); 9 | int n, k, i, j; 10 | 11 | while (scanf("%d%d", &n, &k) != EOF) 12 | { 13 | for (i = 0; i < n; i++) 14 | scanf("%d", &input[i]); 15 | i = 0; 16 | j = n-1; 17 | while ((input[i]+input[j]) != k && i != j) 18 | { 19 | if ((input[i]+input[j]) > k) 20 | j--; 21 | else 22 | i++; 23 | } 24 | if ( i == j) 25 | printf("%d %d\n", -1, -1); 26 | else 27 | printf("%d %d\n", input[i], input[j]); 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /剑指offer/面试题41——和为S的连续正数序列/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题41——和为S的连续正数序列/README -------------------------------------------------------------------------------- /剑指offer/面试题41——和为S的连续正数序列/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void show(int a ,int N) 5 | { 6 | int i; 7 | for(i=0; i= 0) 21 | { 22 | flag = 0; 23 | for(i=sqrt(2*S); i>=2; --i) 24 | { 25 | if( (2*S) % i == 0 ) 26 | { 27 | b = 2*S / i - i + 1; 28 | 29 | if( (b&1) == 0 ) 30 | { 31 | show( b/2 , i ); 32 | flag = 1; 33 | } 34 | } 35 | } 36 | if(flag == 0) 37 | printf("Pity!\n"); 38 | printf("#\n"); 39 | } 40 | return 0; 41 | } 42 | 43 | /************************************************************** 44 | Problem: 1354 45 | User: lintingbin2009 46 | Language: C 47 | Result: Accepted 48 | Time:110 ms 49 | Memory:928 kb 50 | ****************************************************************/ 51 | -------------------------------------------------------------------------------- /剑指offer/面试题42——左旋转字符串/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题42——左旋转字符串/README -------------------------------------------------------------------------------- /剑指offer/面试题42——左旋转字符串/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define N 1001 5 | 6 | char str[N]; 7 | 8 | void swap_string(int start, int end) 9 | { 10 | char tmp; 11 | while (start < end) 12 | { 13 | tmp = str[end]; 14 | str[end] = str[start]; 15 | str[start] = tmp; 16 | start++; 17 | end--; 18 | } 19 | } 20 | int main() 21 | { 22 | //freopen("test.txt", "r", stdin); 23 | 24 | int len, cnt; 25 | while(scanf("%s", str) != EOF) 26 | { 27 | len = strlen(str); 28 | scanf("%d", &cnt); 29 | cnt = cnt % len; 30 | swap_string(0, cnt - 1); 31 | swap_string(cnt, len - 1); 32 | swap_string(0, len - 1); 33 | printf("%s\n", str); 34 | } 35 | return 0; 36 | } 37 | 38 | /************************************************************** 39 | Problem: 1362 40 | User: lintingbin2009 41 | Language: C 42 | Result: Accepted 43 | Time:70 ms 44 | Memory:916 kb 45 | ****************************************************************/ 46 | -------------------------------------------------------------------------------- /剑指offer/面试题42——翻转单词顺序/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题42——翻转单词顺序/README -------------------------------------------------------------------------------- /剑指offer/面试题42——翻转单词顺序/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define N 50001 5 | 6 | char str[N]; 7 | 8 | void swap_string(int start, int end) 9 | { 10 | char tmp; 11 | while (start < end) 12 | { 13 | tmp = str[end]; 14 | str[end] = str[start]; 15 | str[start] = tmp; 16 | start++; 17 | end--; 18 | } 19 | } 20 | int main() 21 | { 22 | //freopen("test.txt", "r", stdin); 23 | 24 | int i, j, len; 25 | while(gets(str) != NULL) 26 | { 27 | len = strlen(str); 28 | for (i = 0; i < len; i++) 29 | { 30 | if (str[i] != ' ') 31 | { 32 | j = i; 33 | while (str[j] != ' ' && j < len) 34 | { 35 | if (str[j+1] == ' ' || j+1 == len) 36 | break; 37 | j++; 38 | } 39 | swap_string(i, j); 40 | i = j + 1; 41 | } 42 | } 43 | swap_string(0, len-1); 44 | printf("%s\n", str); 45 | } 46 | return 0; 47 | } 48 | 49 | /************************************************************** 50 | Problem: 1361 51 | User: lintingbin2009 52 | Language: C 53 | Result: Accepted 54 | Time:50 ms 55 | Memory:964 kb 56 | ****************************************************************/ 57 | -------------------------------------------------------------------------------- /剑指offer/面试题43——PASSN个骰子的点数/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题43——PASSN个骰子的点数/README -------------------------------------------------------------------------------- /剑指offer/面试题44——WA扑克牌顺子/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题44——WA扑克牌顺子/README -------------------------------------------------------------------------------- /剑指offer/面试题44——WA扑克牌顺子/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define N 15 4 | 5 | char num[N]; 6 | 7 | int main() 8 | { 9 | //freopen("test.txt", "r", stdin); 10 | 11 | int cnt, i, j, tmp, flag; 12 | while(scanf("%d", &cnt) != EOF && cnt != 0) 13 | { 14 | memset(num, 0, N); 15 | for (i = 0; i < cnt; i++) 16 | { 17 | scanf("%d", &tmp); 18 | num[tmp]++; 19 | } 20 | for (i = 1; i < N; i++) 21 | if (num[i] != 0) 22 | break; 23 | flag = 1; 24 | for (j = 0; j < cnt; j++, i++) 25 | { 26 | if (num[i] > 1 || (num[i] == 0 && num[0] == 0)) 27 | flag = 0; 28 | if (num[i] == 0 && num[0] > 0) 29 | { 30 | num[i]++; 31 | num[0]--; 32 | } 33 | } 34 | if (flag == 1) 35 | printf("So Lucky!\n"); 36 | else 37 | printf("Oh My God!\n"); 38 | } 39 | return 0; 40 | } 41 | 42 | /************************************************************** 43 | Problem: 1355 44 | User: lintingbin2009 45 | Language: C 46 | Result: Wrong Answer 47 | ****************************************************************/ 48 | -------------------------------------------------------------------------------- /剑指offer/面试题45——圆圈中最后剩下的数/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题45——圆圈中最后剩下的数/README -------------------------------------------------------------------------------- /剑指offer/面试题45——圆圈中最后剩下的数/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int lastRemaining(unsigned int n, unsigned int m) 4 | { 5 | if (n < 1 || m < 1) 6 | return -1; 7 | int last = 0, i; 8 | for (i = 2; i <= n; i++) 9 | last = (last + m) % i; 10 | return last + 1; 11 | } 12 | 13 | int main() 14 | { 15 | //freopen("test.txt", "r", stdin); 16 | 17 | unsigned int n, m; 18 | while(scanf("%d", &n) != EOF && n != 0) 19 | { 20 | scanf("%d", &m); 21 | if (lastRemaining(n, m) != -1) 22 | printf("%u\n", lastRemaining(n, m)); 23 | } 24 | return 0; 25 | } 26 | 27 | /************************************************************** 28 | Problem: 1356 29 | User: lintingbin2009 30 | Language: C 31 | Result: Accepted 32 | Time:550 ms 33 | Memory:912 kb 34 | ****************************************************************/ 35 | -------------------------------------------------------------------------------- /剑指offer/面试题46——求1+2+3+...+n/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题46——求1+2+3+...+n/README -------------------------------------------------------------------------------- /剑指offer/面试题46——求1+2+3+...+n/main.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题46——求1+2+3+...+n/main.c -------------------------------------------------------------------------------- /剑指offer/面试题47——不用加减乘除做加法/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题47——不用加减乘除做加法/README -------------------------------------------------------------------------------- /剑指offer/面试题47——不用加减乘除做加法/main.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题47——不用加减乘除做加法/main.c -------------------------------------------------------------------------------- /剑指offer/面试题49——把字符串转换成整数/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题49——把字符串转换成整数/README -------------------------------------------------------------------------------- /剑指offer/面试题49——把字符串转换成整数/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define N 10 5 | 6 | char num[N]; 7 | int error; 8 | 9 | int antoi(char *num) 10 | { 11 | int len = strlen(num), i = 0, flag = 1, result = 0; 12 | 13 | error = 0; 14 | if(num == NULL) 15 | { 16 | error = 1; 17 | return -1; 18 | } 19 | if (num[i] == '+' || num[i] == '-') 20 | { 21 | flag = (num[i]=='+')? 1:-1; 22 | i++; 23 | } 24 | 25 | for (; i < len; i++) 26 | { 27 | if (num[i]>='0' && num[i]<='9') 28 | result = result*10 + num[i] - '0'; 29 | else 30 | { 31 | error = 1; 32 | break; 33 | } 34 | } 35 | return result * flag; 36 | } 37 | int main() 38 | { 39 | //freopen("test.txt", "r", stdin); 40 | 41 | int result; 42 | while (scanf("%s", num) != EOF) 43 | { 44 | result = antoi(num); 45 | if (!error) 46 | printf("%d\n", result); 47 | else 48 | printf("My God\n"); 49 | } 50 | return 0; 51 | } 52 | 53 | /************************************************************** 54 | Problem: 1508 55 | User: lintingbin2009 56 | Language: C 57 | Result: Accepted 58 | Time:0 ms 59 | Memory:912 kb 60 | ****************************************************************/ 61 | -------------------------------------------------------------------------------- /剑指offer/面试题50——树中两个结点的最低公共祖先/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lintingbin/C-language/f88b639afd654e291062c612c0a19c89e1846b89/剑指offer/面试题50——树中两个结点的最低公共祖先/README -------------------------------------------------------------------------------- /剑指offer/面试题50——树中两个结点的最低公共祖先/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define N 10001 4 | 5 | typedef struct node 6 | { 7 | int data; 8 | struct node *lchild; 9 | struct node *rchild; 10 | }Node, *tree; 11 | 12 | tree Create() 13 | { 14 | int tmp; 15 | Node *tmpNode; 16 | 17 | scanf("%d", &tmp); 18 | if (tmp == 0) 19 | return NULL; 20 | tmpNode = malloc(sizeof(Node)); 21 | tmpNode->data = tmp; 22 | tmpNode->lchild = Create(); 23 | tmpNode->rchild = Create(); 24 | return tmpNode; 25 | } 26 | 27 | int getPath(tree root, int m, int *stack, int *top) 28 | { 29 | if (root == NULL) 30 | return 0; 31 | stack[++(*top)] = root->data; 32 | if (root->data != m) 33 | { 34 | if (getPath(root->lchild, m, stack, top) || getPath(root->rchild, m, stack, top)) 35 | return 1; 36 | else 37 | { 38 | (*top)--; 39 | return 0; 40 | } 41 | } else{ 42 | return 1; 43 | } 44 | } 45 | 46 | int stack1[N], top1, stack2[N], top2; 47 | 48 | int main() 49 | { 50 | //freopen("test.txt", "r", stdin); 51 | 52 | int cnt, i, j, num1, num2; 53 | tree root; 54 | while (scanf("%d", &cnt) != EOF) 55 | { 56 | for (i = 0; i < cnt; i++) 57 | { 58 | root = Create(); 59 | scanf("%d%d", &num1, &num2); 60 | top1 = top2 = -1; 61 | if (getPath(root, num1, stack1, &top1) && getPath(root, num2, stack2, &top2)) 62 | { 63 | if (top1 > top2) 64 | top1 = top2; 65 | for (j = top1; j >= 0; j--) 66 | { 67 | if (stack1[j] == stack2[j]) 68 | { 69 | printf("%d\n", stack1[j]); 70 | break; 71 | } 72 | } 73 | } 74 | else 75 | printf("My God\n"); 76 | } 77 | } 78 | return 0; 79 | } 80 | 81 | /************************************************************** 82 | Problem: 1509 83 | User: lintingbin2009 84 | Language: C 85 | Result: Accepted 86 | Time:140 ms 87 | Memory:4820 kb 88 | ****************************************************************/ 89 | --------------------------------------------------------------------------------