Chinaunix首页 | 论坛 | 博客
  • 博客访问: 707978
  • 博文数量: 129
  • 博客积分: 2221
  • 博客等级: 大尉
  • 技术积分: 1620
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-09 11:27
个人简介

do{goodgoodstudy();daydayup();}while(!died)

文章分类

全部博文(129)

文章存档

2012年(26)

2011年(103)

分类:

2012-04-19 14:16:37

这段时间有点困惑,在底层待了一段时间,太没有自由和个性了,重新找一下上层C语言无拘无束的感觉

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>

  3. void bubble_sort(int a[], int n)
  4. {
  5. /*冒泡排序*/
  6.     int i, j, t;
  7.     int flag = 1;

  8.     for (i = 0; i < n && flag; i ) {
  9.         flag = 0;
  10.         for (j = 0; j < n - i - 1; j ) {
  11.             if (a[j] < a[j 1]) {
  12.                 t = a[j];
  13.                 a[j] = a[j 1];
  14.                 a[j 1] = t;
  15.                 flag = 1;
  16.             }
  17.         }
  18.     }
  19. }

  20. void select_sort(int a[], int n)
  21. {
  22. /*选择排序*/
  23.     int i, j, k, t;

  24.     for (i = 0; i < n - 1; i ) {
  25.         k = i;
  26.         for (j = i 1; j < n; j ) {
  27.             if (a[k] < a[j])
  28.                 k = j;
  29.         }
  30.         if (k != i) {
  31.             t = a[i];
  32.             a[i] = a[k];
  33.             a[k] = t;
  34.         }
  35.     }
  36. }

  37. void insert_sort(int a[], int n)
  38. {
  39. /*插入排序*/
  40.     int i, j, t;

  41.     for (i = 1; i < n; i ) {
  42.         t = a[i];
  43.         j = i - 1;
  44.         while (j >= 0 && t > a[j]) {
  45.             a[j 1] = a[j];
  46.             j--;
  47.         }
  48.         a[j 1] = t;
  49.     }
  50. }

  51. int half_find(int a[], int n, int data)
  52. {
  53. /*二分查找*/
  54.     int mid, top = n - 1, bot = 0;
  55.     mid = (top bot) / 2;

  56.     while (top >= bot) {
  57.         if (a[mid] < data)
  58.             bot = mid 1;
  59.         else if (a[mid] > data)
  60.             top = mid - 1;
  61.         else
  62.             return mid;

  63.         mid = (top bot) / 2;
  64.     }
  65. }

  66. void func()
  67. {
  68.     printf("int= %d \n",sizeof(int));
  69.     printf("float= %d \n",sizeof(float));
  70.     printf("double= %d \n",sizeof(double));
  71. }

  72. /*
  73.  * 下面这种写法是有错的
  74. GetMemory(char * p,int num)
  75. {
  76.    p=(char *)malloc(sizeof(char)*num);
  77. }
  78.  */

  79. char * GetMemory(int num)
  80. {
  81.    return (char *)malloc(sizeof(char)*num);
  82. }

  83. void func1()
  84. {
  85.     char *str =NULL;
  86.     char *str1 =NULL;
  87.     char *a="Hello";
  88.     str=GetMemory(100);
  89.     /*str=GetMemory1(&str1,100);*/

  90.     strcpy(str,a);
  91.     printf("%s \n",str);
  92.     strcpy(str,"hello");
  93.     printf("%s \n",str);

  94. /* strcpy(str1,a);*/
  95.     /*printf("%s \n",str1);*/
  96. }

  97. void func2(int a)
  98. {
  99.     int n;
  100.     while(a>0)
  101.     {
  102.         if(a&0x01)
  103.         {
  104.              n;
  105.             a=a>>1;
  106.         }
  107.     }

  108.     printf("n=%d \n",n);
  109. }

  110. int main(void)
  111. {
  112.     int a[5] = {1, 2, 3, 4, 5};    
  113.     int i;

  114.     printf("%d\n", half_find(a, 5, 5));
  115.     func();
  116.         func1();
  117.         func2(0x0fffff);
  118. /*    select_sort(a, 5);
  119.     for (i = 0; i < 5; i )
  120.         printf("%d ", a[i]);
  121.         */
  122.     printf("\n");

  123.     return 0;
  124. }

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