Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2566484
  • 博文数量: 315
  • 博客积分: 3901
  • 博客等级: 少校
  • 技术积分: 3640
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-08 15:32
个人简介

知乎:https://www.zhihu.com/people/monkey.d.luffy Android高级开发交流群2: 752871516

文章分类

全部博文(315)

文章存档

2019年(2)

2018年(1)

2016年(7)

2015年(32)

2014年(39)

2013年(109)

2012年(81)

2011年(44)

分类: C/C++

2012-07-13 16:24:30

   发现自己真的很笨,理解一个概念都要半天!以前数学还行,但是读大学后就变笨了,不愿意去思考,玩魔兽也不动脑筋、讲究技巧。所以脑袋生锈了,是时候需要训练大脑了,整天看一些无聊的视频会懒惰的,而且整天不锻炼身体也会懒惰的,不仅身体懒惰,而且大脑越来越不行,我就是一个典型!不管怎么样,能理解多少算多少,多练习就好了,多开拓思维,过两天找同学给我补习数学去....
  1、并查集:

点击(此处)折叠或打开

  1. //============================================================================
  2. // Name : Union-find.cpp
  3. // Author : hl
  4. // Version :
  5. // Copyright : Your copyright notice
  6. // Description : Union_find in C++, Ansi-style
  7. //============================================================================

  8. #include <iostream>
  9. #include<cstring>
  10. #include<cstdio>
  11. #include<cstdlib>
  12. using namespace std;

  13. int father[50002],a,b,m,n,p;
  14. /*
  15.  * 并查集
  16.  * 理解(simple)
  17.  *    亲戚(Relations)
  18.  *    题目描述: 若某个家族人员过于庞大,要判断两个是否是亲戚,确实还很不容易,现在给出某个亲戚关系图,求任意给出的两个人是否具有亲戚关系。
  19. 规定:x和y是亲戚,y和z是亲戚,那么x和z也是亲戚。如果x,y是亲戚,那么x的亲戚都是y的亲戚,y的亲戚也都是x的亲戚。
  20.  *    输入格式 Input Format
  21.  *        第一行:三个整数n,m,p,(n<=5000,m<=5000,p<=5000),分别表示有n个人,m个亲戚关系,询问p对亲戚关系。以下m行:每行两个数Mi,Mj,1<=Mi,Mj<=N,表示Ai和Bi具有亲戚关系。
  22.  *        接下来p行:每行两个数Pi,Pj,询问Pi和Pj是否具有亲戚关系。
  23.  *    输出格式 Output Format
  24.  *        P行,每行一个’Yes’或’No’。表示第i个询问的答案为“具有”或“不具有”亲戚关系。
  25.  * 结果(简单理解)---经过以下分析/最终我们需要找到Pi和Pj的根为同一个就OK了!!!
  26.  * Please input some information:
  27. 1000,3,1
  28. 1,2 ----1, 2直接找到返回;a[1] = 2,a[2] = 2;
  29. -----x1
  30. -----father[x]1
  31. -----x2
  32. -----father[x]2
  33. 2,3 ----2, 3直接找到返回;a[1] = 2, a[2] = 3, a[3] = 3;
  34. -----x2
  35. -----father[x]2
  36. -----x3
  37. -----father[x]3
  38. 1,4 ----1不能直接找到,a[1] = 2, 递归a[2] = 3, a[3] = 3,到3找到返回a[3];
  39. -----x3 ----4直接找到返回;最后,a[1] = 2, a[2] = 3, a[3] = 4, a[4] = 4;
  40. -----father[x]3
  41. -----x2
  42. -----father[x]3
  43. -----x1
  44. -----father[x]3
  45. -----x4
  46. -----father[x]4
  47. 3,4 ******当我们寻找3,4和亲戚关系的时候;可以通过a[3]找到4,因此3的父亲是4,and 4就是它本身;到此找到亲戚关系!!!
  48. -----x4 ******比如是1,5;通过1,我们找到root为4,而通过5找到的root是5;
  49. -----father[x]4
  50. -----x3
  51. -----father[x]4
  52. -----x4
  53. -----father[x]4
  54. Yes
  55.  */
  56. //查找(find):寻找某一个元素所在集合的根。使用路径压缩来优化该函数。
  57. int find(int x)
  58. {
  59.     if(father[x]!=x) father[x]=find(father[x]);
  60.     cout << "-----x" << x << endl;
  61.     cout << "-----father[x]" << father[x] << endl;
  62.     return father[x];
  63. }

  64. int main() {
  65.     cout << "Please input some information: " << endl;
  66. #if 1
  67.     scanf("%d,%d,%d",&n,&m,&p);
  68.     //getchar();
  69.     for(int i=1;i<=n;i++) father[i]=i;
  70.     for(int i=1;i<=m;i++)
  71.     {
  72.         scanf("%d,%d",&a,&b);
  73.         //getchar();
  74.         a=find(a),b=find(b);father[a]=b;
  75.     }
  76.     for(int i=1;i<=p;i++)
  77.     {
  78.         scanf("%d,%d",&a,&b);
  79.         //getchar();
  80.         a=find(a);b=find(b);
  81.         if(a==b)printf("Yes\n");
  82.         else printf("No\n"); //Result is YESYESNO or like this struct? about buffer,
  83.     }
  84. #endif
  85.     return 0;
  86. }
 2、二分查找:

点击(此处)折叠或打开

  1. /*
  2.  ============================================================================
  3.  Name : search.c
  4.  Author : hl
  5.  Version :
  6.  Copyright : Your copyright notice
  7.  Description : Search in C, Ansi-style
  8.  ============================================================================
  9.  */

  10. #include <stdio.h>
  11. #include <stdlib.h>

  12. /*
  13.  * 打印整型数组 can be used for test
  14.  */
  15. void print_array(int a[], int len)
  16. {
  17.     int i;
  18.     for (i = 0; i < len; i++)
  19.         printf("%d ", a[i]);
  20.     putchar('\n');
  21. }

  22. /* 时间复杂度()
  23.  * 说明:
  24.  * 二分查找又称折半查找,优点是比较次数少,查找速度快,平均性能好;
  25.  * 其缺点是要求待查表为有序表,且插入删除困难;
  26.  * So,折半查找方法适用于不经常变动而查找频繁的有序列表--百度百科
  27.  */
  28. int binarysearch(int a[], int len, int number)
  29. {
  30.     int mid, start = 0, end = len - 1;

  31.     while (start <= end)
  32.     {
  33.         mid = (start + end) / 2; //record middle position
  34.         if (a[mid] < number)
  35.             start = mid + 1; //注意mid+1哦!否则可能会死循环(没+1,sometime 怎么结束?能找到数组最后一位吗?
  36.         else if (a[mid] > number)
  37.             end = mid - 1; //注意mid-1哦!否则可能会死循环(没-1,sometime 怎么结束?能找到数组的第一位吗?
  38.         else
  39.             return mid;
  40.     }
  41.     return -1;
  42. }

  43. int main(void) {
  44.     int a1[7] = {3, 4, 5, 7, 9, 10, 12};
  45.     int key = 12;
  46.     printf("Search number %d, got position a[%d]\n", key, binarysearch(a1, 7, key));

  47.     return EXIT_SUCCESS;
  48. }

阅读(1295) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~