2013年7月24日
摘要: 代码./* 因为字符共256种可能,记录每个字符出现的次数,然后找到第一次出现1次的就是 */#include#includeusing namespace std;void getfirst(string str){ unsigned int count[256]; int i; for(i=0;i>s; getfirst(s); return 0;} 阅读全文
posted @ 2013-07-24 15:49 紫金树下 阅读(190) 评论(0) 推荐(0) 编辑
摘要: 代码。/* 层次遍历 */#include#includeusing namespace std;typedef char Elemtype;typedef struct node{ Elemtype data; struct node* left; struct node* right;}Tree;//先序建立一个树,NULL指针用0代替Tree* create_tree(void){ Tree* root=NULL; char ch; cin>>ch; if(ch=='0') return NULL; else {... 阅读全文
posted @ 2013-07-24 15:24 紫金树下 阅读(201) 评论(0) 推荐(0) 编辑
摘要: 题目: 输入1个二叉查找树,将该树转换为它的镜像,即在转换后的二叉查找树中,左子树的节点都大于右子树的节点,用递归和循环两种方法完成。二叉查找树中序遍历是从小到大的,镜像树是从大到小的,可用来判断是否转换成功代码,递归的:/* 二叉查找树转换前中序遍历是从小到大,转换后是从大到小 递归: 若节点有左孩子或者右孩子的话,首先转换左子树,再转换右子树,然后交换节点左右孩子 */#includeusing namespace std;typedef int datatype;typedef struct node{ datatype value; struct node*... 阅读全文
posted @ 2013-07-24 15:14 紫金树下 阅读(370) 评论(0) 推荐(0) 编辑
摘要: 类似于算法导论2.3-7,它要求在一个无序集合中找出2个数和等于x。首先对它排序,快排O(nlgn),然后就是本题了。代码:/* 因为已经是有序的,设置两个指针指向开始和结束,然后相加它们,若和>给定的值,则尾指针向前,若和using namespace std;void getsum(int *a,int begin,int end,int sum){ bool flag=false; while(begin>sum; getsum(a,0,5,sum); return 0;}打印所有的,在a[begin]+a[end]==sum时继续下去就OK了。 阅读全文
posted @ 2013-07-24 14:28 紫金树下 阅读(214) 评论(0) 推荐(0) 编辑