C语言循环及判断相关练习题

(1)写代码将三个整数从大到小输出

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
//void sort(int a, int b, int c) {
//	if (a <= b) {
//		if (b <= c) {
//			printf("%d %d %d\n", c, b, a);
//		}
//		else
//		{
//			if (a <= c) {
//				printf("%d %d %d\n", b, c, a);
//
//			}
//			else
//			{
//				printf("%d %d %d\n", b, a, c);
//
//			}
//
//		}
//	}
//	else {
//		if (a <= c) {
//			printf("%d %d %d\n", c, a, b);
//
//		}
//		else
//		{
//			if (b >= c) {
//				printf("%d %d %d\n", a, b, c);
//
//			}
//			else {
//				printf("%d %d %d\n", a, c, b);
//
//			}
//		}
//	}
//}
//int main() {
//	int x, y, z;
//	scanf("%d %d %d", &x, &y, &z);
//	sort(x, y, z);
//	return 0;
//}
int main() {
	int x, y, z;
	scanf("%d %d %d", &x, &y, &z);
	if (y< z) {
		int temp = y;
		y = z;
		z = temp;
	}
	if (x < y) {
		int temp = x;
		x = y;
		y = temp;
	}
	if (y < z) {
		int temp = y;
		y = z;
		z = temp;
	}
	printf("%d %d %d", x, y, z);
	return 0;
}

(2)打印1-100中3的倍数的数字

int main() {
	for (int i = 1; i <= 100; i++) {
		if (i % 3 == 0) {
			printf("%d\n", i);
		}
	}
	return 0;
}

(3)求两个数的最大公约数

int min(int x, int y) {
	if (x<y)
	{
		return x;
	}
	else
	{
		return y;
	}
}
int main() {
	int a, b;
	scanf("%d %d", &a, &b);
	int flag = 1;
	for (int i = 1; i <= min(a, b); i++) {
		if (a % i == 0 && b % i == 0&&i>flag) {
			flag = i;
		}
	}
	printf("%d", flag);
	return 0;
}

(4)打印1000-2000年之间的闰年

int main() {
	int count=0;
	for (int y = 1000; y <= 2000; y++) {
		if (y % 4 == 0 && y % 100 != 0) {
			printf("%d\n", y);
			count++;
		}
		else if (y % 400 == 0) {
			printf("%d\n", y);
			count++;
		}
	}
	printf("%d\n", count);
	return 0;
}

(5)打印100-200之间的素数

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include<math.h>
int main() {
	int count = 0;
	int i,j;
	for (i = 101; i <= 200; i+=2) {
		int flag = 1;
		for (j = 2; j <=sqrt(i); j++) {
			if (i % j == 0) {
				flag = 0;
				break;
			}
		}
		if (flag==1) {
			printf("%d\n", i);
			count++;
		}
		
	}
	printf("%d\n", count);
	return 0;
}

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值