分支和循环练习
程序员文章站
2022-03-02 12:08:48
...
三个数从小到大输出
# include<stdio.h>
int main() {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
if (a < b) {
int temp = a;
a = b;
b = temp;
}
if (a < c) {
int temp = a;
a = c;
c = temp;
}
if (b < c) {
int temp = b;
b = c;
c = temp;
}
printf("%d %d %d\n", a, b, c);
return 0;
}
一百以被三整除
# include<stdio.h>
int main() {
int i;
for (i = 0; i <= 100; i++) {
if (i % 3 == 0) {
printf("%d\n", i);
}
}
return 0;
}
最大公约数(给两个数,求最大公约数)
辗转相除法:以除数和余数反复做除法运算,当余数为 0 时,取当前算式除数为最大公约数,
# include<stdio.h>
int main() {
int m = 24, n = 18, r;
while (m % n!=0) {
r = m % n;
m = n;
n = r;
}
printf("%d\n", n);
}
打印闰年,1000年到2000年
1.能被4整除,不能被100整除
两者“或”的关系
2;能被400整除是闰年
# include<stdio.h>
int main() {
int year = 0;
int count = 0;
for (year = 1000; year <= 2000; year++) {
if (year % 4 == 0&&year%100!=0) {
printf("%d\n", year);
count++;
}
else if (year % 400 == 0) {
printf("%d\n", year);
count++;
}
}
printf("%d\n", count);
return 0;
}
# include<stdio.h>
int main() {
int year;
int count = 0;
for (year = 1000; year <= 2000; year++) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
printf("%d\n", year);
count++;
}
}
printf("%d\n", count);
}
100到200的素数
1:试除法 产生2到i-1
# include<stdio.h>
int main() {
int i;
for (i = 100; i <= 200; i++)
{
//判断i是否为素数
//1:试除法 产生2到i - 1
int j = 0;
for (j = 0; j < i; j++) {
if (i % j == 0) {
break;
}
}
if (j == i) {
printf("%d\n",i);
}
}
return 0;
}
2:
i=a*b
a,b中有一个小于i的开平方
# include<stdio.h>
#include<math.h>
# include<string.h>
int main() {int i;
for (i = 100; i <= 200; i++)
{
//判断i是否为素数
//1:试除法 产生2到i - 1
int j = 0;
for (j = 0; j < sqrt(i); j++) {
if (i % j == 0) {
break;
}
}
if (j >sqrt(i)) {
printf("%d\n", i);
}
}
return 0;
}
数9的个数
数一下1到100有多少个9
99两个9,不能用else
# include<stdio.h>
int main() {
int i;
int count = 0;
for (i = 1; i <= 100; i++) {
if (i % 10 == 9) {
count++;
}
if (i / 10 == 9) {
count++;
}
}
printf("%d\n", count);
}
分数求和
# include<stdio.h>
int main() {
int i;
double sum = 0;
int flag = 1;
for (i = 1; i <= 100; i++) {
sum = flag*1.0/ i + sum;
flag = -flag;
}
printf("%lf\n", sum);
return 0;
}
求十个数的最大值
注意max的赋值
# include<stdio.h>
int main() {
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int max = arr[1];
int i;
int se = sizeof(arr) / sizeof(arr[0]);
for (i = 1; i <= se; i++) {
if (max < arr[i]) {
max = arr[i];
}
}
printf("%d\n", max);
}
乘法口诀
# include<stdio.h>
int main() {
int i, j,sum;
for (i = 1; i <= 9; i++) {
printf("\n");
for (j = 1; j <= i; j++) {
sum = i * j;
printf("%d*%d=%d\t", i, j, sum);
}
}
}
猜数字游戏
1:电脑生成随机数
2:猜数字
# include<stdio.h>
# include<stdlib.h>
# include <time.h>
void game() {
int guess;
int ret = 0;
//用时间戳来表示
ret = rand()%100+1;
//printf("%d\n", ret);
while (1) {
printf("猜数字\n");
scanf("%d", &guess);
if (guess > ret) {
printf("猜大了\n");
}
else if (guess < ret) {
printf("猜小了\n");
}
else printf("猜对了");
break;
}
}
void menu() {
printf("****************\n");
printf("*****1.play 0.exit*****\n");
printf("****************\n");
}
int main() {
int input = 0;
srand((unsigned int)time(NULL));
do {
menu();
printf("请选择\n");
scanf("%d", &input);
switch (input) {
case(1): game();
break;
case(2):
printf("退出游戏\n");
break;
default:
printf("输出错误\n");
break;
}
} while (input);
return 0;
}
上一篇: python6
下一篇: 小白学python系列-(6) 条件判断