C语言个人代码总结(上)

if条件句的使用

1、输入生日,判断星座

方法一(初级版):

    int y = 0, m = 0, d = 0;
    scanf("%d%d%d", &y, &m, &d);
    y = 2014 - y;
    if (m == 1) {
        if (d <= 21){
            printf("%d岁, 摩羯座\n", y);
        } else {
            printf("%d岁, 水瓶座\n", y);
        }
    } else if (m == 2) {
        if (d <= 19){
            printf("%d岁, 水瓶座\n", y);
        } else {
            printf("%d岁, 双鱼座\n", y);
        }
    } else if (m == 3) {
        if (d <= 20){
            printf("%d岁, 双鱼座\n", y);
        } else {
            printf("%d岁, 白羊座\n", y);
        }
    } else if (m == 4) {
        if (d <= 20){
            printf("%d岁, 白羊座\n", y);
        } else {
            printf("%d岁, 金牛座\n", y);
        }
    } else if (m == 5) {
        if (d <= 21){
            printf("%d岁, 金牛座\n", y);
        } else {
            printf("%d岁, 双子座\n", y);
        }
    } else if (m == 6) {
        if (d <= 21){
            printf("%d岁, 双子座\n", y);
        } else {
            printf("%d岁, 巨蟹座\n", y);
        }
    } else if (m == 7) {
        if (d <= 22){
            printf("%d岁, 巨蟹座\n", y);
        } else {
            printf("%d岁, 狮子座\n", y);
        }
    } else if (m == 8) {
        if (d <= 22){
            printf("%d岁, 狮子座\n", y);
        } else {
            printf("%d岁, 处女座\n", y);
        }
    } else if (m == 9) {
        if (d <= 23){
            printf("%d岁, 处女座\n", y);
        } else {
            printf("%d岁, 天秤座\n", y);
        }
    } else if (m == 10) {
        if (d <= 23){
            printf("%d岁, 天秤座\n", y);
        } else {
            printf("%d岁, 天蝎座\n", y);
        }
    } else if (m == 11) {
        if (d <= 22){
            printf("%d岁, 天蝎座\n", y);
        } else {
            printf("%d岁, 射手座\n", y);
        }
    } else if (m == 12) {
        if (d <= 21){
            printf("%d岁, 射手座\n", y);
        } else {
            printf("%d岁, 摩羯座\n", y);
        }
    } 


方法二(升级版):

    int y = 0, m = 0, d = 0;
    scanf("%d-%d-%d", &y, &m, &d);
    int days2 = 28;
    if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) {
        days2++;
    }
    int md = m * 100 + d;
    if ((md >= 321 && md <= 331) || (md >= 401 && md <= 419)) {
        printf("白羊座\n");
    } else if ((md >= 420 && md <= 430) || (md >= 501 && md <= 520)) {
        printf("金牛座\n");
    } else if ((md >= 521 && md <= 531) || (md >= 601 && md <= 621)) {
        printf("双子座\n");
    } else if ((md >= 622 && md <= 630) || (md >= 701 && md <= 722)) {
        printf("巨蟹座\n");
    } else if ((md >= 723 && md <= 731) || (md >= 801 && md <= 822)) {
        printf("狮子座\n");
    } else if ((md >= 823 && md <= 831) || (md >= 901 && md <= 922)) {
        printf("处女座\n");
    } else if ((md >= 923 && md <= 931) || (md >= 1001 && md <= 1023)) {
        printf("天秤座\n");
    } else if ((md >= 1024 && md <= 1031) || (md >= 1101 && md <= 1122)) {
        printf("天蝎座\n");
    } else if ((md >= 1123 && md >= 1130) || (md >= 1201 && md <= 1221)) {
        printf("射手座\n");
    } else if ((md >= 1222 && md <= 1231) || (md >= 101 && md <= 119)) {
        printf("摩羯座\n");
    } else if ((md >= 120 && md <= 131) || (md >= 201 && md <= 218)) {
        printf("水瓶座\n");
    } else if ((md >= 219 && md <= days2) || (md >= 301 && md <= 322)) {
        printf("双鱼座\n");
    } else {
        printf("欢迎来到地球\n");
    }



for 和 while 循环

2、求两个数的最大公约数和最小公倍数

    int a = 0, b = 0, m = 0;
    scanf("%d%d", &a, &b);
    for (int i = a; i >= 1; i--) {
        if (b % i == 0 && a % i == 0) {
            m = i;
            printf("最大公约数:%d\n", i);
            printf("最小公倍数:%d\n", a * b / i);
            break;
        }
    }


辗转相除法(求最大公约数):

    int a = 0, b = 0;
    scanf("%d%d", &a, &b);
    int a1 = a, b1 = b;
    while (a1 % b1 != 0) {
        int x = a1 % b1;
        a1 = b1;
        b1 = x;
    }
    printf("%d\n", b1);


3、实心菱形

方法一(初级版)

    int n = 0;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i; j++) {
            printf(" ");
        }
        for (int k = 1; k <= i * 2 - 1; k++) {
            printf("*");
        }
        printf("\n");
    }
    for (int i = n - 1; i >= 1; i--) {
        for (int j = n - 1; j >= i; j--) {
            printf(" ");
        }

        for (int k = 1; k <= i * 2 - 1; k++) {
            printf("*");
        }
        printf("\n");
    }

方法二(升级版):

    int n = 0;
    scanf("%d", &n);
    
    for (int i = 1; i <= 2 * n - 1; i++) {
        int x = (2 * n - 1) - (abs(i - n) * 2);    // abs()绝对值;
        for (int k = 1; k <= abs(i - n); k++) {
            printf(" ");
        }
        for (int j = 1; j <= x; j++) {
            printf("*");
        }
        printf("\n");
    }

4、空心菱形:

    int n = 0;
    scanf("%d", &n);
    for (int i = 1; i <= 2 * n - 1; i++) {
        int x1 = abs(i - n);//空格数
        int x2 = (2 * n - 1) - x1 * 2;
        for (int k = 1; k <= x1; k++) {
            printf(" ");
        }
        for (int j = 1; j <= x2; j++) {
            if (j == 1 || j == x2) {
                printf("*");
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }

5、99乘法表:

    for (int i = 1; i <= 9; i++) {
        for (int j = 1; j <= i; j++) {
            printf("%d * %d = %2d  ", j, i, i * j);
        }
        printf("\n");
    }

6、找到n个随机数里第二大的数:

方法一(初级版):

遍历随机数,如果比老大的大,就让老大把原来的给老二;如果没有老大的大,然后再去跟老二比,如果比老二的大跟老二交换;

    int i = 1, n = 0;
    unsigned int max = 0, sec = 0;
    scanf("%d", &n);
    while (i <= n) {
        unsigned int random = arc4random() % (60 - 20 + 1) + 20;
        printf("%u\n", random);
        if (i == 1) {
            max = sec = random;
        } else if (i == 2) {
            if (random > max) {
                max = random;
            } else {
                sec = random;
            }
        } else if (i >= 3) {
            if (random > max) {
                sec = max;
                max = random;
            } else if (random > sec) {
                sec = random;
            }
        }
        i++;
    }
    printf("sec = %u\n", sec);
    printf("max = %u\n", max);


方法二(升级版):

同方法一思想一样,只是代码简化了。

    int n = 0;
    scanf("%d", &n);
    unsigned int max = 0, max2 = 0;
    while (n > 0) {
        unsigned int random = arc4random() % (70 - 30 + 1) + 30;
        printf("%u\n", random);
        if (max < random) {
            max2 = max;
            max = random;
        } else if (max2 < random) {
            max2 = random;
        }
        n--;
    }
    printf("max = %u, max2 = %u\n", max, max2);

方法三:

遍历随机数,把大的先给老二,老二再跟老大比,大的给老大;

 int n = 0, temp = 0;
    scanf("%d", &n);
    unsigned int max = 0, max2 = 0;
    while (n > 0) {
        unsigned int random = arc4random() % (70 - 30 + 1) + 30;
        printf("%u\n", random);
        if (random > max2) {
            max2 = random;
            if (max2 > max) {
                temp = max;
                max = max2;
                max2 = temp;
            }
        }
        n--;
    }
    printf("max = %u, max2 = %u\n", max, max2);


数组

1、三种排序,一种查找

冒泡:
    int array[] = {3, 2, 6, 9, 8, 5, 7, 1, 4};
    int count = sizeof(array) / sizeof(array[0]);
    int temp;
    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]) {
                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, 5, 6, 7, 8, 9, 2, 1, 4};
    int count = sizeof(array) / sizeof(array[0]);
    for (int i = 0; i < count - 1; i++) {
        int minIndex = i;
        for (int j = minIndex + 1; j < count; j++) {
            if (array[minIndex] > array[j]) {
                minIndex = j;
            }
        }
        if (minIndex != i) {
            int temp;
            temp = array[i];
            array[i] = array[minIndex];
            array[minIndex] = 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 = 1; i < count; i++) {
        int temp = array[i];
        int j = i-1;
        while (j >= 0 && array[j] > temp) {
            array[j+1] = array[j];
            j--;
        }
        if (j != i-1) {
            array[j+1] = 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, mid = 0;
    int target = 0;
    scanf("%d", &target);
    while (start <= end) {
        mid = (start + end) / 2;
        if (array[mid] > target) {
            end = mid - 1;
        } else if (array[mid] < target) {
            start = mid + 1;
        } else {
            break;
        }
    }
    if (start <= end) {
        printf("[%2d] : %d\n", mid, array[mid]);
    } else {
        printf("no\n");
    }

2、输入日期,返回是这一年的第几天

方法一(初级版):
缺陷是没有考虑输入错误日期的情况。
    int y = 0, m = 0, d = 0, days = 28, sum = 0;
    scanf("%d-%d-%d", &y, &m, &d);
    if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) {
        days++;
    }
    for (int i = 1; i < m; i++) {
        sum = 31 * i;
    }
    if (m > 11) {
        sum -= (4 + 31 - days - d);
    } else if (m > 9) {
        sum -= (3 + 31 - days - d);
    } else if (m > 6) {
        sum -= (2 + 31 - days - d);
    } else if (m > 4) {
        sum -= (1 + 31 - days - d);
    } else if (m > 2) {
        sum -= (31 - days - d);
    } else {
        sum += d;
    }
    printf("sum = %d\n", sum);

方法二(升级版,函数调用):
bool checkDateValiable(unsigned year, unsigned month, unsigned day)
{
    bool result = true;
    unsigned days = 0;
    switch (month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            days = 31;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            days = 30;
            break;
        case 2:
            days = 28 + daysOfLeapyear(year);
        default:
            break;
    }
    if (0 == days || day > days || 0 == day) {
        result = false;
    }
    return result;
}

unsigned int daysOfLeapyear(unsigned int year)
{
    return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
unsigned dayOfYear(unsigned year, unsigned month, unsigned day)
{
    if (!checkDateValiable(year, month, day)) {
        return 0;
    }
    int daysOfMonth[12] = {
        0,
        31,
        31 + 28,
        31 + 28 + 31,
        31 + 28 + 31 + 30,
        31 + 28 + 31 + 30 + 31,
        31 + 28 + 31 + 30 + 31 + 30,
        31 + 28 + 31 + 30 + 31 + 30 + 31,
        31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
        31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
        31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
        31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
    };
    int days = day + daysOfMonth[month - 1];
    if (month > 2) {
        days += daysOfLeapyear(year);
    }
    return days;
}


3、打乱数组

数组一共9个元素,产生9个随机数,每次产生随机数的范围从9到1依次递减,产生的随机数作为下标,把下标对应的数跟数组最后一个元素交换。
    int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    int count = sizeof(array) / sizeof(array[0]);
    for (int i = count - 1; i > 0; i--) {
        unsigned int random = arc4random() % (i + 1);
        int temp;
    temp = array[i];
    array[i] = array[random];
    array[random] = temp;
    }
    for (int i = 0; i < count; i++) {
        printf("[%2d] : %d\n", i, array[i]);
    }

4、实现字符串函数

(1)字符串的长度strlen
    char string1[10] = "iphone";
    int i = 0;
    while (string1[i] != '\0') {
        printf("%c", string1[i]);
        i++;
    }
    
    printf("\n");
    printf("length = %d\n", i);

(2)字符串的复制strcpy
    char string1[10] = "iphone";
    char string3[] = "android";
    int i = 0;
    while ((string1[i] = string3[i]) != '\0') {
        i++;
    }
    printf("%s\n", string1);

(3)字符串的拼接strcat
    char string1[30] = "iPhone";
    char string2[] = "android";
    int i = 0, j = 0;
    while (string1[i] != '\0') {
        i++;
    }
    while ((string1[i] = string2[j])) {
        i++;
        j++;
    }
    printf("%s\n", string1);

(4)字符串的比较strcmp
    char string1[] = "abcd";
    char string2[] = "abed";
    int result = 0, i = 0;
    while (0 == (result = string1[i] - string2[i])) {
        if ('\0' == string1[i]) {
            break;
        }
        i++;
    }
    printf("%d\n", result);

5、二路归并

    int a1[] = {3, 6, 8, 9, 4}, a2[] = {1, 5, 2, 0, 7};
    int count1 = sizeof(a1) / sizeof(a1[0]);
    int count2 = sizeof(a2) / sizeof(a2[0]);
    int a3[count1 + count2];
    int flag = 1, temp = 0;
    for (int i = 0; i < 5 - 1; i++) {
        flag = 0;
        for (int j = 0; j < 5 - i - 1; j++) {
            if (a1[j] > a1[j + 1]) {
                temp = a1[j];
                a1[j] = a1[j + 1];
                a1[j + 1] = temp;
                flag = 1;
            }
            if (a2[j] > a2[j + 1]) {
                temp = a2[j];
                a2[j] = a2[j + 1];
                a2[j + 1] = temp;
            }
        }
    }
    int i = 0, j = 0, k = 0;
    while (i < 5 && j < 5) {
            if (a1[i] < a2[j]) {
                a3[k] = a1[i];
                i++;
                k++;
            } else if (a1[i] > a2[j]) {
                a3[k] = a2[j];
                j++;
                k++;
            }
        }
    
    while (i < count1) {
        a3[k++] = a1[i++];
   }
    while (j < count2) {
        a3[k++] = a2[j++];
    }
    for (int i = 0; i < 5; i++) {
        printf("a1[%2d] : %d\n", i, a1[i]);
    }
    for (int i = 0; i < 5; i++) {
        printf("a2[%2d] : %d\n", i, a2[i]);
    }
    for (int i = 0; i < 10; i++) {
        printf("a3[%2d] : %d\n", i, a3[i]);
    }

6、选举

    int array[5], n = 0;
    int count = sizeof(array) / sizeof(array[0]);
    int index[count];
    for (int i = 0; i < count; i++) {
        index[i] = i;
    }
    printf("请输入候选人数:");
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        unsigned int random = arc4random() % ('E' - 'A' + 1) + 'A';
        array[random - 'A']++;
    }
    for (int i = 0; i < count - 2; i++) {
        for (int j = 0; j < count - i - 2; j++) {
            if (array[index[j]] < array[index[j + 1]]) {
                int temp;
                temp = index[j];
                index[j] = index[j + 1];
                index[j + 1] = temp;
            }
        }
    }
    for (int i = 0; i < count - 1; i++) {
        printf("[%c] : %d\n", index[i] + 'A', array[index[i]]);
    }
    printf("无效票:%d\n", array[4]);

7、索引数组排序

冒泡法:
    int array[] = {3, 2, 6, 9, 8, 5, 7, 1, 4};
    int count = sizeof(array) / sizeof(array[0]);
    int index[count];
    for (int i = 0; i < count; i++) {
        index[i] = i;
    }
    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[index[j]] > array[index[j + 1]]) {
                int temp = index[j];
                index[j] = index[j + 1];
                index[j + 1] = temp;
                flag = 1;
            }
        }
    }
    for (int i = 0; i < count; i++) {
        printf("[%2d] : %d\n", index[i], array[index[i]]);
    }

选择法:
    int array[] = {3, 2, 6, 9, 8, 5, 7, 1, 4};
    int count = sizeof(array) / sizeof(array[0]);
    int index[count];
    for (int i = 0; i < count; i++) {
        index[i] = i;
    }
    for (int i = 0; i < count - 1; i++) {
        int minIndex = i;
        for (int j = minIndex + 1; j < count; j++) {
            if (array[index[minIndex]] > array[index[j]]) {
                minIndex = j;
            }
        }
        int temp = index[minIndex];
        index[minIndex] = index[i];
        index[i] = temp;
    }
    
    for (int i = 0; i < count; i++) {
        printf("[%2d] : %d\n", index[i], array[index[i]]);
    }

插入法:
    int array[] = {3, 2, 6, 9, 8, 5, 7, 1, 4};
    int count = sizeof(array) / sizeof(array[0]);
    int index[count];
    for (int i = 0; i < count; i++) {
        index[i] = i;
    }
    for (int i = 1; i < count; i++) {
        int j = i;
        int tempIndex = index[j];
        while (j > 0 && array[tempIndex] < array[index[j - 1]]) {
            index[j] = index[j - 1];
            j--;
        }
        index[j] = tempIndex;
    }
    for (int i = 0; i < count; i++) {
        printf("[%2d] : %d\n", index[i], array[index[i]]);
    }

8、找到字符串的中间位置(面试题)

    char string1[] = "a12b3c45def6g789";
    int i = 0, j = 0;
    while (1) {
        if ('\0' == string1[i]) {
            break;
        }
        i++;
        if ('\0' == string1[i]) {
            break;
        }
        i++;
        j++;
    }
    printf("%c\n", string1[j]);

9、找出一个二维数组中的“鞍点”,即该位置上的元素在该行中最大,在该列中最小,打印有关信息。(提示:注意没鞍点或多个鞍点)

    int array[3][4] = {4, 5, 23, 7, 8, 21, 56, 65, 27, 9, 25, 62};
    int a[3] = {0}, b[4] = {0};
    int count1 = sizeof(array) / sizeof(array[0]);
    int count2 = sizeof(array[0]) / sizeof(array[0][0]);
    int max = 0, min = 0, m = 0;
    for (int i = 0; i < count1; i++) {
        max = 0;
        for (int j = 0; j < count2; j++) {
            if (max < array[i][j]) {
                max = array[i][j];
            }
        }
        a[i] = max;
    }
    for (int i = 0; i < count2; i++) {
        min = 100;
        for (int j = 0; j < count1; j++) {
            if (min > array[j][i]) {
                min = array[j][i];
            }
        }
        b[i] = min;
    }
    for (int i = 0; i < count1; i++) {
        for (int j = 0; j < count2; j++) {
            if (a[i] == b[j]) {
                printf("鞍点[%2d][%2d] : %d\n", i, j, a[i]);
                m = 1;
            }
        }
    }
    if (m != 1) {
        printf("no!\n");
    }

10、一到一百万的随机数,如何马上知道重复了

    int a[1000001] = {0};
    while (1) {
        unsigned int random = arc4random() % (1000000 - 1 + 1) + 1;
        if (a[random] == 0) {
            a[random] = random;
            printf("[%2d] : %d\n", random, a[random]);
        } else {
            printf("重复\n");
            break;
        }
    }











你可能感兴趣的:(C代码区)