int main(int argc, const char * argv[]) { int a,b,c; printf("请输入a,b,c:\n"); scanf("%d,%d,%d",&a,&b,&c); if (a>b){ swap(&a, &b); } if (a>c){ swap(&a, &c); } if (b>c){ swap(&b, &c); } printf("升序排序结果:\n"); printf("%d,%d,%d\n",a,b,c); return 0; }
九九乘法表
1 2 3 4 5 6 7 8 9 10 11
int main(int argc, const char * argv[]) { int i = 0; int j = 0; for (i = 1; i <= 9; i++) { for (j = 1; j <= 9; j++) { printf("%d*%d=%2d\t",i,j,i*j); } printf("\n"); } return 0; }
使用goto语句模拟循环,求10以内整数之和。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <stdio.h>
int main(int argc, const char * argv[]) { int sum = 0; int i = 0; LOOP: if (++i <= 10) { sum += i; goto LOOP; } printf("10以内的正整数之和为:%d\n",sum); return 0; }
int main(int argc, const char * argv[]) { int a,b,m,c; printf("请输入两个数:"); scanf("%d,%d",&a,&b); // 给两个整数排序 if (a<b){ int temp = a; a = b; b = temp; } m = a*b; c = a%b; while (c != 0) { a = b; // 最大公约数 b = c; c = a%b; } printf("最大公约数是:%d\n",b); printf("最小公倍数是:%d\n",m/b);
return 0; }
Post title:C语言学习04:语句和控制块
Post author:张建
Create time:2023-02-13 12:58:15
Post link:https://redefine.ohevan.com/2023/02/13/C学习/C语言学习04:语句和控制块/
Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.