分支和循环练习

将三个数字从小到大输出

算法实现:a中放最大值,b中间值,c最小值

1234567891011121314151617181920212223
#include <stdio.h>int main() {    int a,b,c,max;    printf("请输入三个数字:->");    scanf("%d,%d,%d",&a,&b,&c);    if (a<b) {        max = b;        b = a;        a = max;    }    if (a<c) {        max = c;        c = a;        a = max;    }    if (b<c) {        max = c;        c = b;        b = max;    }    printf("%d,%d,%d",a,b,c);    return 0;}

输出1-100内3的倍数

12345678910
#include <stdio.h>int main() {    int i;    for (i=1; i<=100; i++) {        if (i%3==0) {            printf("%d,",i);        }    }    return 0;}

求两个数的最大公约数

12345678910111213
#include <stdio.h>int main(){    int m = 12;    int n = 8;    int r;    while (m % n) {        r = m % n;        m = n;        n = r;    }    printf("%d",n);}

打印1000-2000之间的闰年

闰年的计算方法‌:每四年一个闰年,但整百年份必须是400的倍数才是闰年。例如,2000年是闰年,而1900年则是平年。

12345678910
#include <stdio.h>int main() {    int a,b,c;    for (a = 1000; a <= 2000; a++) {        if ((a % 4 == 0 && a % 100 != 0 )|| a % 400 == 0) {            printf("%d,",a);        }    }    return 0;}

打印100-200之间的素数

质数又称素数。指在一个大于1的自然数中,除了1和此整数自身外,没法被其他自然数整除的数。

123456789101112131415
#include <stdio.h>int main() {    int a,b,c,d;        for (a=100; a<=200; a++) {        for (b=2; b<a; b++) {                        if ( a%b == 0 ) {                break;                }            }        if (b == a) {            printf("%d ",a);        }        }    return 0;}

计算1-100之间出现数字9的个数

123456789101112131415161718192021
#include <stdio.h>int main() {    int num = 0;    int a,b,c;    for (a = 1; a<=100; a++) {        //求a的十位数字        b = a / 10;//        printf("%d,",b);        //求a的个位数字        c = a % 10;//        printf("%d**\n**",c);        if (b == 9) {            num++;        }        if (c == 9) {            num++;        }    }    printf("%d\n",num);    return 0;}

想求一个数字的十位数字就用这个数字/10,想求它的个位数字就%10。

分数求和

计算1/1 - 1/2 + 1/3 - 1/4 + 1/5 …. + 1/99 - 1/100

12345678910111213141516171819
#include <stdio.h>int main() {    float a,c;    float f = 0.0;    int b = 1;    int d = 1;    for (a = 1; a <= 100; a++) {        c = b/a;//      printf("%f",c);        d++;        if (d%2==0) {            f = f + c;        }else{            f = f - c;        }    }    printf("%f",f);    return 0;}

求10个整数中的最大值

12345678910111213
#include <stdio.h>int main() {    int i;    int arr[10] = {-12,-23,-43,-21,-34,-74,-24,-53,-4435,-33};//先定义十个整数    int max = arr[0];    for (i = 1; i<10; i++) {        if (arr[i] >= max) {            max = arr[i];        }    }    printf("%d\n",max);    return 0;}

乘法口诀表

在屏幕上打印9*9乘法口诀表
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

12345678910111213141516
#include <stdio.h>int main() {    int x,y,a;    for (y=1; y<=9; y++) {        for (x=1; x<=9; x++) {            if (y>=x) {                a = x * y;                printf("%d*%d=%-2d ",x,y,a);//%-2d代表打印2位,不够2位补空格,负号代表左对齐,不带负号就是右对齐。            }            if (y==x) {                printf("\n");            }        }    }    return 0;}

printf("%d*%d=%-2d ",x,y,a);-2d代表打印2位,不够2位补空格,负号代表左对齐,不带负号就是右对齐。

猜数字游戏

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
#include <stdio.h>#include <stdlib.h>#include <time.h>void menu(){    printf("************************\n");    printf("***  1.play  0.exit  ***\n");    printf("************************\n");} void game(){    //1、生成随机数    int ret;    int guess = 0;    ret = rand()%100+1;//生成1-100之间的随机数。//  printf("%d**\n**",ret);//打印生成的随机数    //2、猜数字    while (1) {        printf("请猜数字:->");        scanf("%d",&guess);        if (guess > ret) {            printf("猜大了\n");        }else if(guess < ret){            printf("猜小了\n");        }else{            printf("猜对了\n");            break;        }    }} int main() {    int input = 0;    srand((unsigned int)time(NULL));    //用时间戳来设置随机数生成起点,一直在变化    //time函数    do {        menu();        printf("请选择:->");        scanf("%d",&input);        switch (input) {            case 1:                game();                break;            case 0:                printf("退出游戏\n");                break;            default:                printf("选择错误\n");                break;        }    } while (input);    return 0;}

srand((unsigned int)time(NULL));生成随机数。
使用time函数,用时间戳来设置随机数生成起点,这可以让数字一直在变化。
ret = rand()%100+1;使生成的随机数在1-100之间。

🔍 ×