第七题
题目分析:求最大最小值转换为条件判断问题,最大值有四种可能,最小值相应有三种情况,给出下列代码。
示例代码:
#include <stdio.h>
int main() {
int num1, num2, num3, num4; // 定义四个变量来存储输入的数字
int max, min; // 定义变量来保存最大值和最小值
printf("Enter four integere: ");
scanf_s("%d %d %d %d", &num1, &num2, &num3, &num4);
// 初始化最大值和最小值为第一个数字
max = num1;
min = num1;
if (num2 > max || num3 > max || num4 > max) {
max = num2 > num3 ? (num2 > num4 ? num2 : num4) : (num3 > num4 ? num3 : num4);
}
if (num2 < min || num3 < min || num4 < min) {
min = num2 < num3 ? (num2 < num4 ? num2 : num4) : (num3 < num4 ? num3 : num4);
}
// 输出结果
printf("Largest: %d\n", max);
printf("Smallest: %d\n", min);
return 0;
}
输出
第八题
题目分析:根据输入时间,匹配最接近的下一时刻,然后匹配对应的到达时间,得出代码。
示例代码
#include <stdio.h>
#include <string.h>
int main() {
int hour, minu, input;
char found[100], time[100];
// 输入时间
printf("Enter a 24-hour time: ");
scanf_s("%d:%d", &hour, &minu);
input = hour * 60 + minu;
// 飞机起飞时间
int fly_time1 = 8 * 60;
int fly_time2 = 9 * 60 + 43;
int fly_time3 = 11 * 60 + 19;
int fly_time4 = 12 * 60 + 47;
int fly_time5 = 14 * 60;
int fly_time6 = 15 * 60 + 45;
int fly_time7 = 19 * 60;
int fly_time8 = 21 * 60 + 45;
const char* time1 = "8:00 a.m.";
const char* time2 = "9:43 a.m.";
const char* time3 = "11:19 a.m.";
const char* time4 = "12:47 p.m.";
const char* time5 = "14:00 p.m.";
const char* time6 = "15:45 p.m.";
const char* time7 = "19:00 p.m.";
const char* time8 = "21:45 p.m.";
int closestGreater = -1; // 初始化为一个不可能的值
// 检查每一个单独的值
if (fly_time1 > input) {
closestGreater = fly_time1;
strcpy_s(time, time1);
strcpy_s(found, "10:16 a.m.");
}
if (fly_time2 > input && (closestGreater == -1 || fly_time2 - input < closestGreater - input)) {
closestGreater = fly_time2;
strcpy_s(time, time2);
strcpy_s(found, "11:52 a.m.");
}
if (fly_time3 > input && (closestGreater == -1 || fly_time3 - input < closestGreater - input)) {
closestGreater = fly_time3;
strcpy_s(time, time3);
strcpy_s(found, "1:31 p.m.");
}
if (fly_time4 > input && (closestGreater == -1 || fly_time4 - input < closestGreater - input)) {
closestGreater = fly_time4;
strcpy_s(time, time4);
strcpy_s(found, "3:00 p.m.");
}
if (fly_time5 > input && (closestGreater == -1 || fly_time5 - input < closestGreater - input)) {
closestGreater = fly_time5;
strcpy_s(time, time5);
strcpy_s(found, "4:08 p.m.");
}
if (fly_time6 > input && (closestGreater == -1 || fly_time6 - input < closestGreater - input)) {
closestGreater = fly_time6;
strcpy_s(time, time6);
strcpy_s(found, "5:05 p.m.");
}
if (fly_time7 > input && (closestGreater == -1 || fly_time7 - input < closestGreater - input)) {
closestGreater = fly_time7;
strcpy_s(time, time7);
strcpy_s(found, "9:20 p.m.");
}
if (fly_time8 > input && (closestGreater == -1 || fly_time8 - input < closestGreater - input)) {
closestGreater = fly_time8;
strcpy_s(time, time8);
strcpy_s(found, "11:58 p.m.");
}
if (closestGreater != -1) {
printf("Closest departure time is %s, arriving at %s\n", time, found);
}
else {
printf("没有找到大于输入时间的飞行时间。\n");
}
return 0;
}
输出
第九题
示例代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 函数声明
int compareDates(int m1, int d1, int y1, int m2, int d2, int y2);
int main() {
int month1, day1, year1;
int month2, day2, year2;
// 提示用户输入第一个日期
printf("Enter first date(mm/dd/yy): ");
if (scanf_s("%d/%d/%d", &month1, &day1, &year1) != 3) {
printf("输入错误。\n");
return 1;
}
// 提示用户输入第二个日期
printf("Enter second date(mm/dd/yy): ");
if (scanf_s("%d/%d/%d", &month2, &day2, &year2) != 3) {
printf("输入错误。\n");
return 1;
}
// 比较两个日期
if (compareDates(month1, day1, year1, month2, day2, year2)) {
printf("%02d/%02d/%02d is earlier than %02d/%02d/%02d\n", month1, day1, year1, month2, day2, year2);
}
else {
printf("%02d/%02d/%02d is earlier than %02d/%02d/%02d\n", month2, day2, year2, month1, day1, year1);
}
return 0;
}
// 比较两个日期,返回 1 表示第一个日期更早,0 表示第二个日期更早
int compareDates(int m1, int d1, int y1, int m2, int d2, int y2) {
if (y1 < y2 || (y1 == y2 && m1 < m2) || (y1 == y2 && m1 == m2 && d1 < d2)) {
return 1;
}
else if (y1 > y2 || (y1 == y2 && m1 > m2) || (y1 == y2 && m1 == m2 && d1 > d2)) {
return 0;
}
else {
return 0; // 如果两个日期相同
}
}
输出
第十题
示例代码
#include <stdio.h>
int main() {
int score; // 存储成绩分数
// 提示用户输入成绩分数
printf("Enter a numerical grade: ");
scanf_s("%d", &score);
// 检查输入的成绩分数是否在合理范围内
if (score < 0 || score > 100) {
printf("错误:成绩分数应在 0 到 100 之间。\n");
return 1; // 返回错误代码
}
// 使用 switch 语句判断成绩等级
switch (score / 10) {
case 10:
printf("Letter grade: A\n");
break;
case 9:
printf("Letter grade: A\n");
break;
case 8:
printf("Letter grade: B\n");
break;
case 7:
printf("Letter grade: C\n");
break;
case 6:
printf("Letter grade: D\n");
break;
default:
printf("Letter grade: F\n");
break;
}
return 0;
}
输出
第十一题
示例代码
#include <stdio.h>
#include <string.h>
int main() {
int number; // 存储输入的两位数
char result[50] = ""; // 存储英文单词的结果
// 提示用户输入一个两位数
printf("Enter a two-digit number: ");
if (scanf_s("%d", &number) != 1) {
printf("输入错误。\n");
return 1;
}
// 检查输入的数字是否在合理范围内
if (number < 10 || number > 99) {
printf("错误:输入的数字应为两位数(10-99)。\n");
return 1; // 返回错误代码
}
// 获取十位和个位上的数字
int tens = number / 10;
int ones = number % 10;
// 特殊处理 11 到 19 的情况
if (tens == 1 && ones >= 1) {
switch (number) {
case 11:
strcpy_s(result, sizeof(result), "eleven");
break;
case 12:
strcpy_s(result, sizeof(result), "twelve");
break;
case 13:
strcpy_s(result, sizeof(result), "thirteen");
break;
case 14:
strcpy_s(result, sizeof(result), "fourteen");
break;
case 15:
strcpy_s(result, sizeof(result), "fifteen");
break;
case 16:
strcpy_s(result, sizeof(result), "sixteen");
break;
case 17:
strcpy_s(result, sizeof(result), "seventeen");
break;
case 18:
strcpy_s(result, sizeof(result), "eighteen");
break;
case 19:
strcpy_s(result, sizeof(result), "nineteen");
break;
}
}
else {
// 处理其他情况
switch (tens) {
case 2:
strcpy_s(result, sizeof(result), "twenty");
break;
case 3:
strcpy_s(result, sizeof(result), "thirty");
break;
case 4:
strcpy_s(result, sizeof(result), "forty");
break;
case 5:
strcpy_s(result, sizeof(result), "fifty");
break;
case 6:
strcpy_s(result, sizeof(result), "sixty");
break;
case 7:
strcpy_s(result, sizeof(result), "seventy");
break;
case 8:
strcpy_s(result, sizeof(result), "eighty");
break;
case 9:
strcpy_s(result, sizeof(result), "ninety");
break;
}
// 如果个位数不为零,追加连字符和个位数的英文单词
if (ones != 0) {
char temp[10];
strcpy_s(temp, sizeof(temp), "-");
strcat_s(result, sizeof(result), temp);
switch (ones) {
case 1:
strcat_s(result, sizeof(result), "one");
break;
case 2:
strcat_s(result, sizeof(result), "two");
break;
case 3:
strcat_s(result, sizeof(result), "three");
break;
case 4:
strcat_s(result, sizeof(result), "four");
break;
case 5:
strcat_s(result, sizeof(result), "five");
break;
case 6:
strcat_s(result, sizeof(result), "six");
break;
case 7:
strcat_s(result, sizeof(result), "seven");
break;
case 8:
strcat_s(result, sizeof(result), "eight");
break;
case 9:
strcat_s(result, sizeof(result), "nine");
break;
}
}
}
// 对于 10 的情况
if (number == 10) {
strcpy_s(result, sizeof(result), "ten");
}
// 输出结果
printf("You entered the number:%s\n", result);
return 0;
}
输出