1.按顺序打印三个数
//按大小顺序打印三个数字
void Swap(int* px, int* py)
{int tmp = *px;*px = *py;*py = tmp;
}
int main()
{int a = 0;int b = 0;int c = 0;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("%d %d %d\n", a, b, c);return 0;
}
2.打印1-100之间3的倍数
方法一:
//打印1-100之间3的倍数
int main()
{int i = 0;for (i = 1; i <= 100; i++){if (i % 3 == 0){printf("%d ", i);}}return 0;}
方法二:
//打印1-100之间3的倍数
int main()
{int i = 0;for (i = 3; i <= 100; i+=3){printf("%d ", i);}return 0;}
3.求两个数的最大公约数
方法一:
//求两个数的最大公约数
//暴力求解
int main()
{int a = 0;int b = 0;scanf("%d %d", &a, &b);int min = (a < b) ? a : b;int m = min;while (1){if (a % m == 0 && b % m == 0){break;}m--;}printf("%d\n", m);return 0;
}
方法二:
//辗转相除法
int main()
{int a = 0;int b = 0;int c = 0;scanf("%d %d", &a, &b);while (c=a%b){a = b;b = c;}printf("%d\n", b);return 0;
}
4.编写程序算出1-100之间有多少个数字9
//编写程序算出1-100之间有多少个数字9
//9 19 29 39 49 59 69 79 89 99
//90 91 92 93 94 95 96 97 98
int main()
{int count = 0;int i = 0;for (i = 1; i <= 100; i++){//计算个位是9if (i % 10 == 9)count++;//计算十位是9if (i / 10 == 9)count++;}printf("%d\n", count);return 0;
}
5.计算1/1-1/2+1/3-1/4+1/5......+1/99-1/100的值,打印出结果
//计算1/1-1/2+1/3-1/4+1/5......+1/99-1/100的值,打印出结果
//分子总是1
//分母是1-100
int main()
{int i = 0;double sum = 0;int flag = 1;for (i = 1; i <= 100; i++){sum = sum + flag * 1.0 / i;flag = -flag;}printf("%lf\n", sum);return 0;
}