【以下回答由 GPT 生成】
问题1:交换两个数的值
#include <stdio.h>
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x, y;
printf("请输入两个数x和y:");
scanf("%d %d", &x, &y);
if (x < y) {
swap(&x, &y);
}
printf("交换后的值为:%d %d\n", x, y);
return 0;
}
问题2:判断闰年
#include <stdio.h>
int isLeapYear(int year) {
if (year % 400 == 0) {
return 1;
} else if (year % 4 == 0 && year % 100 != 0) {
return 1;
} else {
return 0;
}
}
int main() {
int year;
printf("请输入年份:");
scanf("%d", &year);
if (isLeapYear(year)) {
printf("是闰年\n");
} else {
printf("不是闰年\n");
}
return 0;
}
问题3:判断成绩等级
#include <stdio.h>
char getGrade(int score) {
if (score < 0 || score > 100) {
return 'I'; // 表示成绩无效
} else if (score >= 0 && score <= 60) {
return 'D'; // 不及格
} else if (score <= 70) {
return 'C'; // 合格
} else if (score <= 90) {
return 'B'; // 良好
} else {
return 'A'; // 优秀
}
}
int main() {
int score;
printf("请输入成绩:");
scanf("%d", &score);
char grade = getGrade(score);
if (grade == 'I') {
printf("成绩无效\n");
} else {
printf("成绩等级:%c\n", grade);
}
return 0;
}
问题4:从大到小排序
#include <stdio.h>
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
void sortDescending(int* a, int* b, int* c) {
if (*a < *b) {
swap(a, b);
}
if (*a < *c) {
swap(a, c);
}
if (*b < *c) {
swap(b, c);
}
}
int main() {
int a, b, c;
printf("请输入三个整数:");
scanf("%d %d %d", &a, &b, &c);
sortDescending(&a, &b, &c);
printf("从大到小的顺序:%d %d %d\n", a, b, c);
return 0;
}
问题5:自动购票系统
#include <stdio.h>
int main() {
int age;
float adultPrice, childPrice, seniorPrice;
int isPeakSeason;
printf("请输入游客年龄:");
scanf("%d", &age);
printf("请输入成人票价:");
scanf("%f", &adultPrice);
if (age >= 18 && age <= 65) {
isPeakSeason = 1;
} else {
isPeakSeason = 0;
}
if (isPeakSeason) {
childPrice = adultPrice * 0.8;
seniorPrice = adultPrice * 0.75;
} else {
childPrice = adultPrice * 0.75;
seniorPrice = adultPrice * 0.5;
}
printf("成人票价:%.2f元\n", adultPrice);
printf("儿童票价:%.2f元\n", childPrice);
printf("老人票价:%.2f元\n", seniorPrice);
return 0;
}
希望这些解决方案能够帮到您,如有其他问题请随时提问。
【相关推荐】
如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^