• C语言学习3-1:成绩检测,使用while和do...while得到九九乘法,1-1/2+1/3-1/4 .......+1/10求和,输入10进制打印2进制,求素数,猜随机产生的数字,16进制转二进制,


    这个仅作为练习扩展使用,贴出代码,作为以后复习,不直接写结果。因为要自己推出

    #include <stdio.h>
    
    int main(void)
    {
        int score;
    
        printf("pls input a score: ");
        scanf("%d", &score);
    
        if (score < 0 || score > 100)
        {
            printf("wrong. score from 0 to 100.
    ");    
        }
        else if (60 < score && score <= 100)
        {
            if (score < 70)    
            {
                printf("rank C!
    ");    
            }
            else if (70 <= score && score < 85)
            {
                printf("rank B!
    ");    
            }
            else
                printf("rank A!
    ");
        }
        else
        {
            if (score < 30)    
                printf("rank E!
    ");
            else
                printf("rank D!
    ");
        }
    
        return 0;
    }

    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    #include <stdio.h>
    
    int main(void)
    {
        int i, j;
    
        i = 1;
        while (i <= 9)
        {
            j = 1;
            while (j <= i)
            {
                printf("%d x %d = %2d ", i, j, i * j);    
                j++;
            }
    
            i++;
            putchar('
    ');
        }
        printf("---------------------
    ");
    
        i = 1;
        do {
            j = 1;
            do {
                printf("%d x %d = %2d ", i, j, i * j);
                j++;    
            } while (j <= i);
    
            putchar('
    ');
            i++;
        } while (i <= 9);
        printf("---------------------
    ");
    
        i = 1;
    labeli:
    
            j = 1;
        labelj:
            printf("%d x %d = %2d ", i, j, i * j);
            j++;
            if (j <= i)
                goto labelj;
    
        putchar('
    ');
        i++;
        if (i <= 9)
            goto labeli;
        
        return 0;
    }

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    #include <stdio.h>
    
    int main(void)
    {
        int i;
        double sum = 0;
    
        int flag = 1;
        
        for (i = 1; i <= 10; i++)
        {
            /*
             *if (i % 2 != 0)    
             *    sum += (double)1 / i;
             *else
             *    sum -= 1 / (double)i;
             */
            sum += flag * 1 / (double)i;
            flag *= -1;
        }
    
        printf("sum = %lf
    ", sum);
    
        return 0;
    }

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    #include <stdio.h>
    
    int main(void)
    {
        unsigned n;
    
        printf("pls input n: ");
        scanf("%d", &n);
    
        int i;
        for (i = 31; i >= 0; i--)
        {
            if (n & (1 << i))    
                putchar('1');
            else
                putchar('0');
    
            if (i % 4 == 0)
                putchar(' ');
        }
        putchar('
    ');
    
        return 0;
    }

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    #include <stdio.h>
    
    int main(void)
    {
        int num;
        int divider;
        int flag;
        int count = 0;
    
        for (num = 2; num <= 1000; num++)
        {
            flag = 0;
            for (divider = 2; divider <= num / 2; divider++)    
            {
                if (num % divider == 0)    
                {
                    flag = 1;
                    break;    
                }
            }
    
            if (!flag)
            {
                count++;
                printf("%d 是素数.
    ", num);
            }
        }
    
        printf("2 - 1000 一共有%d个素数.
    ", count);
    
        return 0;
    }

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    #include <stdio.h>
    
    int main(void)
    {
        int r;
        int guess;
        int ret;
    
        srand(time(NULL));
        r = rand() % 100;
    
        while (1)
        {
        retry:
            printf("input a num, to guess the password: ");    
            ret = scanf("%d", &guess);
    
            while (getchar() != '
    ')
                ;
            if (ret == 0)
            {
                printf("姿势不对, 重新输入.
    ");
                goto retry;
            }
        
            if (guess > r)
                printf("你输入的数大了.
    ");
            else if (guess < r)
                printf("你输入的数小了.
    ");
            else
            {
                printf("恭喜你, 中奖了.
    ");
                break;
            }
        }
    
        return 0;
    }

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    #include <stdio.h>
    
    int main(void)
    {
        char ch8 = '0', ch7 = '0', ch6 = '0', ch5 = '0',
             ch4 = '0', ch3 = '0', ch2 = '0', ch1 = '0';
        unsigned n;
        unsigned q, r;
    
        printf("pls input n: ");
        scanf("%u", &n);
    
        q = n;
        char ch;
        int cnt = 0;
        while (q > 0)
        {
            r = q % 16;
            q /= 16;
    
            if (r > 9)
                ch = (r - 10) + 'a';
            else
                ch = r + '0';
    
            switch (cnt)
            {
                case 0:
                    ch1 = ch;
                    break;
                case 1:
                    ch2 = ch;
                    break;
                case 2:
                    ch3 = ch;
                    break;
                case 3:
                    ch4 = ch;
                    break;
                case 4:
                    ch5 = ch;
                    break;
                case 5:
                    ch6 = ch;
                    break;
                case 6:
                    ch7 = ch;
                    break;
                case 7:
                    ch8 = ch;
                    break;
                default:    
                    break;
            }
    
            cnt++;
        }
    
        printf("transformed: ");
        printf("0x%c%c%c%c %c%c%c%c
    ",
            ch8, ch7, ch6, ch5, ch4, ch3, ch2, ch1);
    
        return 0;
    }

    这个几个中就转换二进制有点意思,一个判断位为1还是0,一个一个的来共32次,转16进制就是不停的用除数除16得到每个阶段的余数,从低到高

  • 相关阅读:
    转发与重定向的区别
    Servlet开发详讲
    Servlet的常见错误
    HTTP请求方式之POST和GET的区别
    Spring各种类型数据的注入
    Spring容器的基本使用
    Python接口自动化-测试用例编写
    Python接口自动化-设计测试用例
    python简明教程之数据结构(列表、元组、字典、集合)
    python简明教程之函数
  • 原文地址:https://www.cnblogs.com/will-boot/p/3278777.html
Copyright © 2020-2023  润新知