C语言Lesson4

来源:互联网 发布:linux如何进入文件夹 编辑:程序博客网 时间:2024/06/05 03:17

//    1.

//    int array[20]= {0};

//    int count = sizeof(array) / sizeof(array[0]);

//    for (int i = 0; i < count; i++) {

//        array[i] = arc4random() % 100;

//        int sum = 0;

//        sum

//        }

//    }

    

//遍历数组+5

//    int array[5] = { 2 , 7, 4, 3, 6 };

//    array[1] = 213;

//    for (int i = 0; i < 5; i++) {

//        printf("%d\n",array[i] + 5);

//    }

    

//    int a = 3;

//    printf("%lu\n", sizeof(a));

//    printf("int : %lu\n", sizeof(int));

//    printf("float : %lu\n", sizeof(float));

//    printf("char : %lu\n", sizeof(char));

//    printf("double : %lu\n", sizeof(double));

//    printf("long : %lu\n", sizeof(long));

//    printf("short : %lu\n", sizeof(short));

//    printf("array : %lu\n", sizeof(array));

//    

//    int count = sizeof(array) / sizeof(array[0]);

//    printf("count = %d\n", count);

//    for (int i = 0; i < count;  i++) {

//        printf("[%2d] : %d\n", i , array[i]);

//    }

    

//    数组求和

//    int array[2] = {0};

//    int count = sizeof(array) / sizeof(array[0]);

//    for (int i = 0; i < count; i++) {

//        array[i] = arc4random() % 100;

//        printf("[%2d] : %d\n", i, array[i]);

//    }

//    int sum = 0;

//    for (int i = 0; i < count; i++) {

//        sum +=array[i];

//    }

//    printf("sum = %d\n", sum);

    

//    int array[count] ;// c99标准

    

//    int array [3] = { 5 , 6 , 7};

//    int array2[3] = { 2 , 3 , 4};

//    for (int i = 0; i < 3; i++) {

//        array2[i] = array[i];

//        printf("[%2d] : %d\n", i ,array2[i] );

//    }

    

//    int array [10] = {0};

//    int array2 [10] = {0};

//    int count = sizeof(array) / sizeof(array[0]);

//    for (int i = 0; i < count; i++) {

//        array[i] = arc4random() % (40 - 20 + 1) + 20;

//        array2[i] = arc4random() % (40 - 20 + 1) + 20;

//        int array3[10] = {0};

//        array3[i] = array[i] + array2[i];

//        printf("%d + %d = %d\n", array[i], array2[i], array3[i]);

//    }

    

//数组交换

//    int array[10] = {0};

//    int array2[10] = {0};

//    int count = sizeof(array) / sizeof(array[0]);

//    for (int i = 0; i < count; i++) {

//        array[i] = arc4random() % (50 - 10 + 1) + 40;

//        array2[i] = arc4random() % (50 - 10 + 1) + 40;

//        int array3[10] = {0};

//        array3[i] = array[i];

//        array[i] = array2[i];

//        array2[i] = array3[i];

//        printf("[%2d] [%2d]\n", array[i] , array2[i]);

//    }

    

//冒泡排序

    

    

//    int array[] = {7,8,4,9,2,4,6,0};

//    int count = sizeof(array) / sizeof(array[0]);

//    for (int i = 0; i < count; i++) {

//        for (int j = 0; j < count - i - 1; j++) {

//            if (array[j] > array[j + 1]) {

//                int temp = 0;

//                temp = array[j];

//                array[j] = array[j + 1];

//                array[j + 1] =  temp;

//            }

//        }

//    }

//    for (int i = 0; i < count; i++) {

//        printf("[%2d] : %d\n", i ,array[i]);

//    }

    

//    int array[] = {3,1};

//    int count = sizeof(array) / sizeof(array[0]);

//    for (int i = 0; i < count; i++) {

//        for (int j = 0; j < count - 1 - i; j++) {

//            if (array[j] > array[j + 1]) {

//                int temp = 0;

//                temp = array[j];

//                array[j] = array[j + 1];

//                array[j + 1] = temp;

//                

//            }

//        }

//    }

//    for (int i = 0; i < count; i++) {

//        printf("[%2d] : %d\n", i ,array[i]);

//    }

    

    

//    int array[] = {4, 6, 7, 1, 4, 2, 3, 9, 8, 5};

//    int count = sizeof(array) / sizeof(array[0]);

//    for (int i = 0; i < count; i++) {

//        for (int j = 0; j < count - i - 1; j++) {

//            if (array[j] > array[j + 1]) {

//                int temp = 0;

//                temp = array[j];

//                array[j] = array[j + 1];

//                array[j + 1] = temp;

//            }

//        }

//    }

//    for (int i = 0; i < count; i++) {

//        printf("[%2d] : %d\n", i, array[i]);

//    }

//    


    

//           /

//  选择排序\/

//    int array[] = {3,2,5,1,7,8,6,9,4};

//    int count = sizeof(array) / sizeof(array[0]);

//    for (int i = 0; i < count; i++) {

//        if (array[i] < array[]) {

//

//        }

//    }

    

//    插入排序

    

//    int array[10] = {0};

//    for (int i = 0;i > 0; i++) {

//        array[i] = arc4random() % 20;

//        if (array[i] < array[i + 1]) {

//            array[i] = array[i + 1];

//        }

//        printf("[%2d] : %d" ,i , array[i]);

//    }

    

//    1.

//    int a[SIZE] = {0}, i = 0;

//    for (i = 0; i < SIZE; i++) {

//        a[i]= i + 10;

//        printf("a[%d] = %d\n", i, a[i]);

//    }

//    2.反向输出

//    int b[SIZE] = {0};

//    for ( int i = SIZE - 1; i > 0; i--) {

//        b[i]= i + 10;

//        printf("b[%d] = %d\n", SIZE - i, b[i]);

//

//    }

//    3.求和

//    int a[SIZE] ={0};

//    for ( int i = 0; i < SIZE; i++) {

//        a[i] = i + 10;

//        printf("a[%d] = %d\n", i, a[i]);

//        }

//    int sum = 0;

//    for (int i = 0; i < SIZE; i++) {

//        sum +=a[i];

//    }

//    printf("sum = %d\n", sum);

//    4.连减

//    int a[SIZE]= {0};

//    for (int i = 0; i < SIZE ; i++) {

//        a[i] = i + 10;

//        printf("a[%d] = %d\n", i, a[i]);

//    }

//    int xiangjian = 0;

//    for (int i = 0; i < SIZE; i++) {

//        xiangjian -= a[i];

//    }

//    printf("xiangjian = %d\n", xiangjian + 2 * a[0]);

 

    

//    5.sum

//    int array[20] = {0};

//    int count = sizeof(array) / sizeof(array[0]);

//    for (int i = 0; i < count; i++) {

//        array[i] = arc4random() % (50 - 10 + 1) + 40;

//        printf("[%2d] : %d\n", i,array[i]);

//    }

//    int sum = 0;

//    for (int i = 0; i < count; i++) {

//        sum += array[i];

//    }

//    printf("sum = %d\n", sum );

    

//    avg

//    int array[20] = {0};

//    int count = sizeof(array) / sizeof(array[0]);

//    for (int i = 0; i < count; i++) {

//        array[i] = arc4random() % (50 - 10 + 1) + 40;

//        printf("[%2d] : %d\n", i,array[i]);

//    }

//    int avg = 0;

//    for (int i = 0; i < count; i++) {

//        avg +=array[i];

//    }

//    printf("avg = %d\n", avg / 20 );

    

//    max

//    int array[20], i = 0,max = 0, min;

//    int count = sizeof(array) / sizeof(array[0]);

//    for ( i = 0; i < count; i++) {

//        array[i] = arc4random() % (50 - 10 + 1) + 40;

//        printf("[%2d] = %d\n", i , array[i]);

//        if (max < array[i]) {

//            max = array[i];

//        }

//        if (min > array[i]) {

//            min = array[i];

//        }

//    }

//    printf("max = %d, min = %d\n", max, min);

    

////    min

//    int array[20],i = 0,min;

//    int count =sizeof(array) / sizeof(array[0]);

//    for (i = 0; i < count; i++) {

//        array[i] = arc4random() % (50 - 10 + 1) + 40;

//        printf("[%2d] = %d\n", i, array[i]);

//        if (min > array[i]) {

//            min = array[i];

//        }

//    }

//    printf("min = %d\n", min);

    

//    6.

    

 

//    int array[5] = {0};

//    int c[10]={0};

//    int count = sizeof(array) / sizeof(array[0]);

//    for (int k = 1; k <= 2; k++) {

//        for (int i = 0; i < count; i++) {

//            scanf("%d", &array[i]);

//        }

//

//        if ( k == 1) {

//            for (int i = 0; i < 5; i++) {

//                c[i] = array[i];

//            }

//        }

//        else if (k ==2) {

//            for (int i = 5; i < 10; i++) {

//                c[i] = array[i - 5];

//            }

//        }

//        for (int i = 0; i < count; i++) {

//            for (int j = 0; j < count - i - 1; j++) {

//                if (array[j] > array[j + 1]) {

//                    int temp = 0;

//                    temp = array[j];

//                    array[j] = array[j + 1];

//                    array[j + 1] = temp;

//                }

//            }

//        }

//        for (int i = 0; i < count; i++) {

//            printf("[%2d] : %d\n", i, array[i]);

//        }

//    }

//    printf("\n");

//    for (int i = 0; i < 10; i++) {

//        for (int j = 0; j < 10 - i ; j++) {

//            if (c[j] > c[j + 1]) {

//                int temp = 0;

//                temp = c[j];

//                c[j] = c[j + 1];

//                c[j + 1] = temp;

//            }

//        }

//    }

//    for (int i = 0; i < 10; i++) {

//        printf("[%2d] : %d\n", i, c[i] );

//    }

    

    

//    int a1[]= {0, 5, 6, 2, 9};

//    int a2[] = {4, 8, 1, 9, 5};

//    int a3[10] = {0};

//    int count = sizeof(a1) / sizeof(a1[0]);

//    for (int i = 0; i < count; i++ ) {

//        a3[i] = a1[i];

//        printf("%d\n", a3[i]);

//    }

//    for (int j = 5; j < 10 ; j++) {

//        a3[j] = a2[j - 5];

//        printf("%d\n", a3[j]);

//    }for (<#initialization#>; <#condition#>; <#increment#>) {

//        <#statements#>

//    }

    

//    int array[] = { 10, 6, 3, 7, 9};

//    int count = sizeof(array) / sizeof(array[0]);

//    for (int i = 0; i < count; i++) {

//        for (int i = 0; i < count  ; i++) {

//            for (int j = 0; j < count - i - 1; j++) {

//                if (array[j] > array[j + 1]) {

//                    int temp = 0 ;

//                    temp = array[j];

//                    array[j] = array[j + 1];

//                    array[j + 1] = temp;

//                }

//            }

//        }

//        printf("[%2d] : %d\n", i, array[i]);

//    }

//    int array2[] = { 9, 4, 3, 8, 7};

//    for (int i = 0; i < count; i++) {

//        for (int i = 0; i < count; i++) {

//            for (int j = 0; j < count - i - 1; j++) {

//                if (array2[j] > array2[j + 1]) {

//                    int temp = 0;

//                    temp = array2[j];

//                    array2[j] = array2[j + 1];

//                    array2[j + 1] = temp;

//                }

//            }

//        }

//        printf("[%2d] : %d\n", i , array2[i]);

//    }

//    int array3[10] = {0};

//    array3[10] = array[5] + array2[5];

//        printf("%d\n", array3[10]);

    

//    7.

//    int yaer = 0, month = 0,day = 0;

//    scanf("%d-%d-%d", &yaer, &month, &day);

//    int m[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

//    if (yaer % 4 == 0 || (yaer % 400 == 0 && yaer % 100 != 0)) {

//        m[1] = 29;

//    }

//    int days = 0;

//    for (int i = 0; i < month - 1; i++) {

//        days += m[i];

//    }

//    days += day;

//    printf("NO.%d\n",days );

//  8.

//    int array[] = {3, -9, 32, 77, 63, -24, 14, 0, 21, 45};

//    int count = sizeof(array) /sizeof(array[0]);

//    int flag = 1;

//    for (int i = 0; i < count; i++) {

//        flag = 0;

//        for (int j = 0; j < count - i -1; j++) {

//            if (array[j] > array[j + 1]) {

//                int temp = 0;

//                temp = array[j];

//                array[j] = array[j + 1];

//                array[j + 1] = temp;

//                flag = 1;

//            }

//        }

//    }

//    for (int i = 0; i < count; i++) {

//        printf("[%2d] : %d\n", i, array[i]);

//    }


//    9.

//    int array[] = {5, -9, 32, 77, 64, -24, 14, 0, 21, 45}, max = 0, min = 0, maxid = 0, minid = 0;

//    int count = sizeof(array) / sizeof(array[0]);

//    for (int i = 0; i < count - 1; i++) {

//        if (array[i] > max) {

//            max = array[i];

//            maxid = i;

//        }

//        if (array[i] < min) {

//            min = array[i];

//            minid = i;

//    }

//    }

//    printf("max = %d, maxid = %d\n", max, maxid);

//    printf("min = %d, minid = %d\n", min, minid);

    

//    int array[] = {3, 2, 6, 9, 8, 5, 7, 1, 4};

//    int min = array[0],minid = 0;

//    int count = sizeof(array) / sizeof(array[0]);

//    for (int i = 0; i < count; i++) {

//        if (array[i] < array[i + 1]) {

//            min = array[i];

//            minid = i;

//        }

//    }

//    printf("min = %d, %d\n", min, minid);

//    随机冒泡排序

//    int a[5] ={0};

//    int count = sizeof(a) / sizeof(a[0]);

//    for (int i = 0; i < count; i++) {

//        a[i] = arc4random() % 100;

//        printf("%d\n", a[i]);

//    }

//    for (int i = 0; i < count - 1; i++) {

//        for (int j = 0; j < count -i -1; j++) {

//            if (a[j] > a[j + 1]) {

//                int temp = 0;

//                temp = a[j];

//                a[j] = a[j + 1];

//                a[j + 1] = temp;

//            }

//        }

//    }

//    for (int i = 0; i < count; i++) {

//        printf("[%2d] : %d\n", i, a[i]);

//    }

    

//    int a[5] = {0};

//    int count = sizeof(a)/ sizeof(a[0]);

//    for (int i = 0; i < count; i++) {

//        a[i] = arc4random() % 100;

//        printf("%d\n", a[i]);

//    }

//    for (int i = 0; i < count - 1; i++) {

//        for (int j = 0; j < count - 1 - i; j++) {

//            if (a[j] > a[ j + 1]) {

//                int t = 0;

//                t = a[j];

//                a[j] = a[j + 1];

//                a[j + 1] = t;

//            }

//        }

//    }

//    for (int i = 0; i < count; i++) {

//        printf("[%2d] : %d\n", i , a[i]);

//    }


//    int array[5] = {0};

//    int count = sizeof(array) / sizeof(array[0]);

//    for (int i = 0; i < count; i++){

//        array[i] = arc4random() % 100;

//        printf("%d\n", array[i]);

//        }

//    for (int i = 0; i < count - 1; i++) {

//        for (int j = 0; j < count - i -1; j++) {

//            if (array[j] > array[j + 1]) {

//                int t = 0;

//                t = array[j];

//                array[j] = array[j + 1];

//                array[j + 1] = t;

//            }

//        }

//    }

//    for (int i = 0; i < count; i++)

//    {

//        printf("%d\n", array[i]);

//    }

    

////  折半查找 错!!!!!

//    int a = 0, b = count, c = 0, mid = 0;

//    scanf("%d", &c);

//    while (a <= b) {

//        mid = (a + b) / 2;

//        if (c == array[mid]) {

//            printf("这个数是%d\n",array[mid]);

//            break;

//        }else if (c > array[mid])

//            a = mid + 1;

//        else b = mid - 1;

//    }

//    if(a > b){

//        printf("无此元素%d\n", array[mid]);

//    }

    

//    int i, max, a[4];

//    printf("input 4 numbers: \n");

//    for (i = 0; i < 4; i++) {

//        scanf("%d", &a[i]);

//    }

//    max = a[0];

//    for (i = 1; i < 4; i++) {

//        if (a[i] > max)

//            max = a[i];

//        }

//    printf("maxnum = %d\n", max);

    

//    int a[5] = {0};

//    int count = sizeof(a ) / sizeof(a[0]);

//    for (int i = 0; i < count; i++) {

//        a[i] = arc4random() % 100;

//        printf("%d\n", a[i]);

//    }

//    for (int i = 0; i < count - 1; i++) {

//        for ( int j = 0; j < count - i - 1; j++) {

//            if (a[j] > a[j + 1]) {

//                int t = 0;

//                t = a[j] ;

//                a[j] = a[j + 1];

//                a[j + 1] = t;

//            }

//        }

//    }

//    for (int i = 0; i < count; i++) {

//        printf("[%2d] : %d\n", i ,a[i]);

//    }

    

//    int a[5] = {0};

//    int count = sizeof(a)/ sizeof(a[0]);

//    for (int i = 0; i < count; i++) {

//        a[i] = arc4random() % 100;

//        printf("%d\n", a[i]);

//    }

//    for (int i = 0; i < count - 1; i++) {

//        for (int j = 0; j < count - i - 1; j++) {

//            if (a[j] > a[j + 1]) {

//                int t = 0;

//                t = a[j];

//                a[j] =a [j + 1];

//                a[j + 1] = t;

//            }

//        }

//    }for (int i = 0; i < count; i++) {

//        printf("[%2d : %d\n", i ,a[i]);

//    }

    

//    int a[5] = {0};

//    int count = sizeof(a) / sizeof(a[0]);

//    for (int i = 0; i < count; i++) {

//        a[i] = arc4random() % 100;

//        printf("%d", a[i]);

//    }

//    for (int i = 0; i< count; i++) {

//        for (int j = 0; j < count - i - 1; j++) {

//            if (a[j ] > a[j + 1]) {

//                int t = 0   ;

//                t = a[j];

//                a[j] = a[j + 1];

//                a[j + 1]= t;

//            }

//        }

//    }for (int i = 0; i< count; i++) {

//        printf("[%2d] : %d", i , a[i]);

//    }

    

//    int a[5] = {0};

//    int count = sizeof(a) / sizeof(a[0])    ;

//    for (int i = 0; i < count; i++) {

//        a[i] = arc4random() % 100;

//        printf("%d\n", a[i]);

//    }for (int i = 0; i < count - 1; i++) {

//        for (int j = 0; j < count - i - 1; j++) {

//            if (a[j] > a[j + 1]) {

//                int t = 0;

//                t = a[j];

//                a[j] = a [j + 1];

//                a[j + 1] = t;

//            }

//        }

//    }

//    for (int i = 0; i < count; i++) {

//        printf("[%2d] : %d\n",i ,a[i] );

//    }

//    

//    int a[5] ={0};

//    int count = sizeof(a) / sizeof(a[0]);

//    for (int i = 0; i < count; i++) {

//        a[i]= arc4random () % 100;

//        printf("%d\n", a[i]);

//    }

//    for (int i = 0; i < count - 1; i++) {

//        for (int j = 0; j < count - i - 1; j++) {

//            if (a[j] > a[j + 1]) {

//                int t = 0;

//                t = a[j];

//                a[j] = a[j + 1];

//                a[j + 1] = t;

//                

//            }

//        }

//    }for (int i = 0; i < count; i++) {

//        printf("[%2d} : %d\n", i, a[i]);

//    }

    

//    int a[7] ={0};

//    int count = sizeof(a) / sizeof(a[0]);

//    for (int i = 0; i < count; i++) {

//        a[i] = arc4random() % 100;

//        printf("%d\n", a[i]);

//    }for (int i = 0; i < count; i++) {

//        for (int j = 0; j < count - i -1; j++) {

//            if (a[j] > a[j + 1]) {

//                int t = 0;

//                t = a[j];

//                a[j] = a[j + 1];

//                a[j + 1] = t;

//            }

//        }

//    }for (int i = 0; i < count; i++) {

//        printf("[%2d] : %d\n", i , a[i]);

//    }

    

//    int a[5] = {0};

//    int count = sizeof(a) / sizeof(a[0]);

//    for (int i = 0; i < count; i++) {

//        a[i] = arc4random() % 100;

//        printf("%d\n", a[i]);

//    }for (int i = 0; i < count - 1; i++) {

//        for (int j = 0; j < count - i - 1; j++) {

//            if (a[j] > a[j + 1]) {

//                int t = 0;

//                t = a[j];

//                a[j] = a[j + 1];

//                a[j + 1] = t;

//            }

//        }

//    }for (int i = 0; i < count; i++) {

//        printf("[%2d] : %d\n", i,a[i]);

//    }

    

    

//    int a[9] = {0};

//    int count = sizeof(a) /sizeof(a[0]);

//    for (int i = 0; i < count; i++) {

//        a[i] = arc4random() % 100;

//    }for (int i = 0; i < count - 1; i++) {

//        for (int j = 0; j < count - i - 1; j++) {

//            if (a[j] > a[j+ 1]) {

//                int t = 0;

//                t = a[j];

//                a[j] = a[j + 1];

//                a[j + 1] = t;

//            }

//        }

//    }for (int i = 0; i < count; i++) {

//        printf("[%2d] : %d\n", i, a[i]);

//    }

    

//    int a[] = {8, 2, 5, 6, 4, 1, 9, 7, 4, 3}, min = 0, flag = 0;

//    int count = sizeof(a) / sizeof(a[0]);

//    min = a[0];

//    for (int i = 0; i < count ; i++ ) {

//        if (min > a[i]) {

//            min = a[flag];

//            

//        };

//    }

    

//    int a[] = {9,8,7,6,5,4,3,2,1};

//    int count  = sizeof(a ) / sizeof(a[0]);

//    for (int i = 0; i < count - 1; i++) {

//        for (int j = 0; j < count - i - 1; j++) {

//            if (a[j] > a[j + 1]) {

//                int t = 0;

//                t = a[j];

//                a[j] = a[j + 1];

//                a[j + 1] = t;

//            }

//        }

//    }for (int i = 0; i< count; i++) {

//        printf("[%2d] :%d\n", i, a[i]);

//    }

    

//    int a[] = {0,4,5,3,2,1};

//    int count = sizeof(a) / sizeof(a[0]);

//    for (int i =0; i < count; i++) {

//        for (int j = 0; j < count - i - 1; j++) {

//            if (a[j] > a[j + 1]) {

//                int t = 0;

//                t = a[j ] ;

//                a[j] = a[j + 1];

//                a[j + 1] = t;

//            }

//        }

//    }for (int i =0; i < count; i++) {

//        printf("[%2d] : %d\n", i , a[i]);

//    }

    



    



//    int array[] = {3, 2, 6, 9, 8, 5, 7, 1, 4};

//    int count = sizeof(array) / sizeof(array[0]);

//    for (int i = 0; i < count - 1; i++) {

//        int minid = i;

//        for (int j = minid + 1; j < count; j++) {

//            if (array[minid] > array[j]) {

//                minid = j;

//            }

//        }

//        int temp = 0;

//        temp = array[minid];

//        array[minid] = array[i];

//        array[i] = temp;

//    }

//    for (int i = 0; i < count; i++) {

//        printf("[%2d] : %d\n", i, array[i]);

//    }


//        int array[] = {3, 2, 6, 9, 8, 5, 7, 1, 4};

//    int count = sizeof(array) / sizeof(array[0]);

//    for (int i = 0 ; i < count; i++) {

//        int minid = i;

//        for (int i = minid + 1; i < count; i++) {

//            if (array[minid] > array[i]) {

//                minid = i;

//            }

//        }

//        int temp = 0;

//        temp = array[minid];

//        array[minid] = array[i];

//        array[i] = temp;

//        printf("[%2d] : %d\n", i, array[i]);

//    }

    


//    int array[] = {3, 2, 6, 9, 8, 5, 7, 1, 4};

//    int count = sizeof(array) / sizeof(array[0]);

//    for (int i = 0; i < count; i++) {

//        int minid = i;

//        for (int i = minid + 1; i < count; i++) {

//            if (array[minid] > array[i]) {

//            minid = i;

//            }

//        }

//        int temp = 0;

//        temp = array[minid];

//        array[minid] = array[i];

//        array[i] = temp;

//        printf("[%2d] : %d\n", i, array[i]);

//    }

    

//    int array[] = {3, 2, 6, 9, 8, 5, 7, 4, 1};

//    int count = sizeof(array) / sizeof(array[0]);

//    for (int i = 0; i < count; i++) {

//        int mid = i;

//        for (int i = mid + 1; i < count; i++) {

//            if (array[mid] > array[i]) {

//                mid = i;

//            }

//        }

//        int temp = 0;

//        temp = array[mid];

//        array[mid] = array[i];

//        array[i] = temp;

//        printf("[%2d] : %d\n", i, array[i]);

//    }

    

//    冒泡排序的优化

//    int array[] = {0 ,1, 2, 3, 4, 5, 6, 7, 8, 9};

//    

//    int count = sizeof(array) / sizeof(array[0]);

//    int flag = 1;//1表示无序,0表示有序

//    for (int i = 0; i < count - 1 && 1 == flag; i++) {

//        flag = 0;//冒泡之前假定有序

//        for (int j = 0; j < count - i - 1; j++) {

//            if (array[j] > array[j + 1]) {

//                int temp = 0;

//                temp = array[j];

//                array[j] = array[j + 1];

//                array[j + 1] = temp;

//                flag = 1;// 发生数据交换,是无序状态

//            }

//        }

//    }

//    for (int i = 0; i < count; i++) {

//        printf("[%2d] : %d\n", i, array[i]);

//    }

    

//    int count = sizeof(array) / sizeof(array[0]);

//    int flag = 1;

//    for (int i = 0; i < count - 1 && 1 == flag; i++) {

//        flag = 0;

//        for (int j = 0; j < count - i -1; j++) {

//            if (array[j] > array[j + 1] ) {

//                int temp = 0;

//                temp = array[j];

//                array[j] = array[j + 1];

//                array[j + 1] = temp;

//                flag = 1;

//            }

//        }

//    }for (int i = 0; i < count; i++) {

//        printf("[%2d] : %d\n", i ,array[i]);

//    }

    

//    选择排序

//    int array[] = {3, 2, 6, 9, 8, 5, 7, 1, 4};

//    int count = sizeof(array) / sizeof(array[0]);

//    for (int i = 0; i < count - 1; i++) {

//        int mid = i;

//        for (int i = mid + 1; i < count; i++) {

//            if (array[mid] > array[i]) {

//                mid = i;

//            }

//        }

//        int temp = 0;

//        temp = array[mid];

//        array[mid] = array[i];

//        array[i] = temp;

//        }

//    for (int i = 0 ; i < count; i++) {

//        printf("[%2d] : %d\n", i, array[i]);

//    }

    

//    插入排序

//    int array[] = {3 , 6, 2, 5, 4, 1, 7, 9, 8};

//    int count = sizeof(array) / sizeof(array[0]);

//    for (int i = 1; i < count; i++) {

//        int j = i;

//        int temp = array[j];

//        while (j > 0 && temp < array[j - 1]) {

//            array[j] = array[j - 1];

//            j--;

//        }

//        array[j] = temp;

//    }

//    for (int i = 0; i < count; i++) {

//        printf("[%2d] : %d\n", i, array[i]);

//    }


    


//    折半查找

//    int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};

//    int count = sizeof(array) / sizeof(array[0]);

//    int start = 0, end = count - 1, c = 0, mid = 0;

//    scanf("%d", &c);

//    while (start <= end) {

//        mid = (start + end) / 2;

//        if (c == array[mid]) {

//            printf("这个数是%d\n",array[mid]);

//            break;

//        }else if (c > array[mid])

//            start = mid + 1;

//        else end = mid - 1;

//    }

//    if(start > end){

//        printf("无此元素\n");

//    }

    

//    int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};

//    int count = sizeof(array) / sizeof(array[0]);

//    int start = 0, end = count - 1, mid = 0, c = 0;

//    scanf("%d", &c);

//    while (start <= end) {

//        mid = (start + end) / 2;

//        if (array[mid] > c ) {

//            end = mid - 1;

//        }else if (array [mid] < c){

//            start = mid + 1;

//        }else {

//            break;

//        }

//    }

//    if (start <= end) {//if(mid == c)

//        printf("[%2d] : %d\n", mid, array[mid]);

//    }else {

//        printf("not found\n");

//    }

    

//    随即打乱

//    int array[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};

//    int id = 0;

//    int count = sizeof(array) / sizeof(array[0]);

//    for (int i = 0; i < count; i++) {

//        id = arc4random() % 9;

//        printf("%d\n", id);

//        array[i] =array[id];

//    }

//    for (int i = 0; i < count; i++) {

//        printf("[%2d] : %d\n", id, array[i]);

//    }

    

//    字符串

//    char string[10] = {'i', 'p', 'h', 'o','n', 'e'};

//    int count  = sizeof(string) / sizeof(string[0]);

//    for (int i = 0; i < count; i++) {

//        printf("%c", string[i]);

//    }

//    printf("\n");

//    

//    char string1[10] = {"iphoneios8"};

//    int i = 0;

//    while (string1[i] != '\0') {

//        printf("%c", string1[i]);

//        i++;

//        

//    }

//    printf("\n");

//    printf("i = %d\n", i);

    

//    string1[3] = '\0';

//    printf("%s\n", string1);

    

//    char string3[] = "android";

//    i = 0;

//    while (string3[i] != 0) {

//        string1[i] = string3[i];

//        i++;

//    }

//    string1[i] = '\0';

//    printf("%s\n", string1);

// 

//    i = 0;

//    while ((string1[i] = string3[i]) != 0) {

//        i++;

//    }printf("%s\n", string1);

//

    

//    char string1[30] = "iphone";

//    char string2[] = "android";

//    int i = 0;

//    while (string1[i]) {

//        i++;

//    }

//    int j = 0;

//    while ((string1[i] = string2[j])) {

//        i++;

//        j++;

//    }

//    printf("%s\n", string1);

    

//    while ((string1[i + 6] = string2[i]) != 0) {

//        i++;

//    }

//    printf("%s\n", string1);

    

//    字符串比较

    char string1[] = "abdd";

    char string2[] = "abjd";

    int  result = 0, i = 0;

    while (0 == (result = string1[i] - string2[i]) && string1[i] !='\0') {

        i++;

    }

    printf("result = %d\n", result);

    

//    while (0 == (result = string1[i] - string2[i]) && string1[i]) {

//        i++;

//    }

//    printf("result = %d\n", result);

    

//    while (0 == (result = string1[i] - string2[i]) && string1[i++]) {

//    }

//    printf("result = %d\n", result);

    

//    char string[10] = "iphone";

//    puts(string);

//    printf("lenth = %lu\n",strlen(string));

//

//    char dest[30] = {'\0'};

//    strcpy(dest, string);

//    puts(string);

//

//    char string1[] = "android";

//    strcat(dest, string1);

//    puts(dest);

    

//    char string1[] = "abcd";

//    char string2[] = "abgd";

//    printf("result = %d\n", strcmp(string1, string2));

    

//    char string1[] = "I love iOS, i want an iPhone6";

//    int count = 0;

//    int i = 0;

//    while (string1[i] != '\0') {

//        count +=i;

//    }printf("count = %d\n", count);

    //    int count = sizeof(array) / sizeof(array[0]);

    //    for (int i = 0; i < count - 1; i++) {

    //        int mid = i;

    //        for (int i = mid + 1; i < count; i++) {

    //            if (array[mid] > array[i]) {

    //                mid = i;

    //            }

    //        }

    //        int temp = 0;

    //        temp = array[mid];

    //        array[mid] = array[i];

    //        array[i] = temp;

    //    }for (int i = 0; i < count; i++) {

    //        printf("[%2d] : %d\n", i, array[i]);

    //    }

    

    

    //    int count = sizeof(array) / sizeof(array[0]);

    //    for (int i = 0; i < count - 1; i++) {

    //        int minid = i;

    //        for (int i = minid + 1; i < count; i++) {

    //            if (array[minid] > array[i] ) {

    //                minid = i;

    //            }

    //        }

    //        int temp = 0;

    //        temp = array[minid];

    //        array[minid] = array[i];

    //        array[i] = temp;

    //    }for (int i = 0; i < count; i++) {

    //        printf("[%2d] : %d\n", i ,array[i]);

    //    }

    

    //    int array[] = {3, 2, 6, 9, 8, 5, 7, 1, 4};

    //    int count = sizeof(array) / sizeof(array[0]);

    //    for (int i = 0; i < count - 1; i++) {

    //        int mid = i;

    //        for (int i = mid + 1; i < count; i++) {

    //            if (array[mid] > array[i]) {

    //                mid = i;

    //            }

    //        }

    //    if (mid != i) {

    //        int temp = 0;

    //        temp = array[mid];

    //        array[mid] = array[i];

    //        array[i] = temp;

    //    }

    //    }for (int i = 0; i < count; i++) {

    //        printf("[%2d] : %d\n", i, array[i]);

    //    }


0 0
原创粉丝点击