## 选择结构 ### 选择结构-1 ![image-20250220225958223](https://yp.smallkun.cn/markdown/image-20250220225958223.png!compress) ```c #include /* if(条件){ 复合语句(条件为真时执行) }else{ 复合语句(条件为假时执行) } */ int main(void){ int x, y, z, t;//定义三个整型变量 //数据类型 变量名1, 变量名2; scanf("%d%d%d", &x, &y, &z); if(x > y){ t = x; x = y; y = t; }//实现x和y比较 大数放y位置 if(y > z){ t = y; y = z; z = t; }//实现y和z比较 大数放z位置 z为三个数字中最大数 if(x > y){ t = x; x = y; y = t; } printf("%d %d %d\n", x, y, z); return 0; } ``` ### 选择结构-2 ![image-20250220230052521](https://yp.smallkun.cn/markdown/image-20250220230052521.png!compress) ```c #include int main(void){ int a; scanf("%d", &a); printf("该数的位数为%d\n", (a/10==0?1:(a/100==0?2:(a/1000==0?3:(a/10000==0?4:5))))); printf("%d", a%10); if(a%100/10 != 0){ printf("%d", a%100/10); } if(a%1000/100 != 0){ printf("%d", a%1000/100); } if(a%10000/1000 != 0){ printf("%d", a%10000/1000); } if(a/10000 != 0){ printf("%d", a/10000); } printf("\n"); return 0; } ``` ### 选择结构-3 ![image-20250220230133563](https://yp.smallkun.cn/markdown/image-20250220230133563.png!compress) ```c #include int main(void){ float a, b, result; int flag; printf("请输入两个数字:"); scanf("%f %f", &a, &b); printf("请输入您要进行的操作(1.做加法 2.做乘法 3.做除法):"); scanf("%d", &flag); switch(flag){ case 1:result = a+b;break; case 2:result = a*b;break; case 3:result = a/b;break; } printf("结果为%f\n", result); return 0; } ``` ### 选择结构-4 ![image-20250220230626411](https://yp.smallkun.cn/markdown/image-20250220230626411.png!compress) ```c #include int main(void){ int year, month, day, days=0; scanf("%d-%d-%d", &year, &month, &day); days+=day; //1 3 5 7 8 10 12 //从前一个月开始依次 switch(month-1){ case 11:days+=30; case 10:days+=31; case 9:days+=30; case 8:days+=31; case 7:days+=31; case 6:days+=30; case 5:days+=31; case 4:days+=30; case 3:days+=31; case 2:days+=28; case 1:days+=31; } if(month>3 && (year%4==0&&year%100!=0 || year%400==0)){ days++; } printf("%d\n", days); return 0; } ``` --- ## 循环结构 ### 循环结构-1 ![image-20250220230645828](https://yp.smallkun.cn/markdown/image-20250220230645828.png!compress) ```c #include /* 1.解决字符的循环输入 while((ch=getchar())!='\n') 2.判断字符类型 ASCALL码范围 */ int main(){ char ch; int l, d, o;//l字母的个数 d为数字个数 o为其他字符个数 l=d=o=0; while((ch=getchar()) != '\n'){ if(ch >= 'a' && ch<='z' || ch >= 'A' && ch<='Z' ){ l++; }else if(ch >= '0' && ch <= '9'){ d++; }else{ o++; } } printf("字母个数:%d 数字个数:%d 其他个数%d\n", l, d, o); return 0; } ``` ### 循环结构-2 ![image-20250220230945704](https://yp.smallkun.cn/markdown/image-20250220230945704.png!compress) ```c /* 暴力求解+枚举每一个台阶 for(i=0;i<1000;i++){ } i=0; while(i<1000){ i++; } && || ! 表达式1&&表达式2 左右都为真才为真 短路运算 表达式1为假 表达式2直接不运算 直接为假 */ #include int main(){ int i; for(i=0;i<1000;i++){ if(i%2==1 && i%3==2 && i%5==4 && i%6==5 && i%7==0){ printf("台阶数为%d\n", i); break; } } return 0; } ``` ### 循环结构-3 ![image-20250220230952843](https://yp.smallkun.cn/markdown/image-20250220230952843.png!compress) ```c #include /* 大马:i 中马:j 小马:k */ int main(){ int i, j , k; for(i=0;i<=33;i++){ for(j=0;j<=50;j++){ for(k=0;k<=200;k+=2){ if(i+j+k==100 && i*3 + j*2 + k/2 == 100){ printf("大马:%-2d 中马:%-2d 小马:%-2d\n", i, j, k); } } } } return 0; } ``` ### 循环结构-4 ![image-20250220231000056](https://yp.smallkun.cn/markdown/image-20250220231000056.png!compress) ```c #include int main(){ int i, s, t;//i用来遍历所有数, s用来存储数的位数,t是截取掉最高位之后的数 for(i=1;i<9999;i++){ t = i; while(t > 0){ s = (t/10==0?1:(t/100==0?2:(t/1000==0?3:4)));//求出这个数字位数 t = i%(int)pow(10, s-1);//使用模除去掉最高位 if(t*t == i){ printf("%d:%d\n", t, i); } } } return 0; } ``` ### 循环结构-5 ![image-20250220231006104](https://yp.smallkun.cn/markdown/image-20250220231006104.png!compress) ```c #include int main(){ /* int x, n, t=1;//t为2当前需要加入的位置 scanf("%d", &x); n = x;//处理之后数 while(t < x){//判断位置是否超过的数的范围 n+=t*2; //根据当前输入位置判断前一位是否溢出如果溢出则减一 if(n/(t*10) != x/(t*10)){ n-=(t*10); } t*=10;//继续处理下一位 } printf("%d:%d\n", x, n); */ int x, s = 0; scanf("%d", &x); while(x){ s*=10; s += (x%10+2)%10; x /= 10; } for(;s;s/=10){ printf("%d", s%10); } return 0; } ``` ### 循环结构-6 ![image-20250220231012110](https://yp.smallkun.cn/markdown/image-20250220231012110.png!compress) ```c ``` ### 循环结构-7 ![image-20250220231022607](https://yp.smallkun.cn/markdown/image-20250220231022607.png!compress) ```c ``` ### 循环结构-8 ![image-20250220231027969](https://yp.smallkun.cn/markdown/image-20250220231027969.png!compress) ```c ``` ### 循环结构-9 ![image-20250220231034218](https://yp.smallkun.cn/markdown/image-20250220231034218.png!compress) ```c ``` ### 循环结构-10 ![image-20250220231039716](https://yp.smallkun.cn/markdown/image-20250220231039716.png!compress) ```c ``` --- ## 数组 ### 数组-1 ![image-20250220231426147](https://yp.smallkun.cn/markdown/image-20250220231426147.png!compress) ### 数组-2 ![image-20250220231434409](https://yp.smallkun.cn/markdown/image-20250220231434409.png!compress) ### 数组-3 ![image-20250220231441384](https://yp.smallkun.cn/markdown/image-20250220231441384.png!compress) ### 数组-4 ![image-20250220231447834](https://yp.smallkun.cn/markdown/image-20250220231447834.png!compress) ### 数组-5 ![image-20250220231704799](https://yp.smallkun.cn/markdown/image-20250220231704799.png!compress) --- ## 函数 ## 指针 ## 编译预处理 ## 结构体 ## 文件 ##