俊介三

一天更新一点,一天积累一点

导航

2013年3月28日

摘要: 输入两个整数 n 和 m,从数列1,2,3.......n 中 随意取几个数,使其和等于 m ,要求将其中所有的可能组合列出来.#include <stdio.h>#include <list>using namespace std;void find(int n, int m){ static list<int> mylist; if(n<0 || m<0) return; if(m>0){ mylist.push_front(n); find(n-1,m-n); mylist.pop_front(); find(n-... 阅读全文

posted @ 2013-03-28 16:45 俊介三在前进 阅读(156) 评论(0) 推荐(0)

摘要: 输入一颗二元查找树,将该树转换为它的镜像,即在转换后的二元查找树中,左子树的结点都大于右子树的结点。用递归和循环两种方法完成树的镜像转换。 例如输入: 8 / \ 6 10/\ /\5 7 9 11输出: 8 / \ 10 6 /\ /\11 9 7 5非递归版本采用栈来实现:#include <stdio.h>#include <stack>using namespace std;struct Node{ int data; Node* left; Node* right; Node(){} Node(int d){ data = d; ... 阅读全文

posted @ 2013-03-28 14:20 俊介三在前进 阅读(144) 评论(0) 推荐(0)

摘要: 题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果。如果是返回true,否则返回false。例如输入5、7、6、9、11、10、8,由于这一整数序列是如下树的后序遍历结果: 8 / \ 6 10 / \ / \ 5 7 9 11因此返回true。如果输入7、4、6、5,没有哪棵树的后序遍历的结果是这个序列,因此返回false。思路:后序遍历意味着数组最后一个元素一定是根。再根据binary search tree的特点,左子树一定比根小,右子树一定比根大来递归地检查左右子树。代码:#include <stdio.h>bool ispostorder(int* ar 阅读全文

posted @ 2013-03-28 13:06 俊介三在前进 阅读(158) 评论(0) 推荐(0)

摘要: 用一种算法来颠倒一个链接表的顺序。递归和不递归的方法:#include <stdio.h>#include <stdlib.h>struct List{ int data; List* next; List(){} List(int d){ data = d; next = NULL; } List(int d, List* list){ data =d; next = list; }};//append an element to tailList* appendToTail(int data,... 阅读全文

posted @ 2013-03-28 12:03 俊介三在前进 阅读(278) 评论(0) 推荐(0)

摘要: 给出两个单向链表的头指针,比如h1,h2,判断这俩个链表是否相交.(注意要考虑此链是否带环)#include <stdio.h>#include <stdlib.h>struct List{ int data; List* next; List(){} List(int d){ data = d; next = NULL; } List(int d, List* list){ data =d; next = list; }};//append an element to tailList* app... 阅读全文

posted @ 2013-03-28 09:52 俊介三在前进 阅读(133) 评论(0) 推荐(0)