C语言车牌限行算法及代码实现
C语言车牌限行算法及代码实现
本文将介绍如何使用C语言解决根据星期几和雾霾指数判断车牌是否限行的问题,并提供完整的代码示例。
问题描述
某市根据雾霾指数对车辆进行限行,限行规则如下:
- 限行时间段:周一至周五,周六周日不限行。
- 雾霾指数低于200,不限行。
- 雾霾指数大于等于200且低于400,每天限行两个尾号的汽车:
- 周一:1 和 6
- 周二:2 和 7
- 周三:3 和 8
- 周四:4 和 9
- 周五:5 和 0
- 雾霾指数大于等于400,每天限行五个尾号的汽车:
- 周一、周三、周五:1, 3, 5, 7, 9
- 周二、周四:0, 2, 4, 6, 8
给定星期几(1~7,1表示周一,2表示周二,依次类推,7表示周日)、雾霾指数(0~600)和车牌号,判断该车牌号是否限行。
C语言代码实现
#include <stdio.h>
int main() {
int day, haze, plate;
scanf('%d %d %d', &day, &haze, &plate);
int lastDigit = plate % 10;
char* restriction = 'no';
if (day >= 1 && day <= 5) {
if (haze < 200) {
restriction = 'no';
} else if (haze >= 200 && haze < 400) {
if ((day == 1 && (lastDigit == 1 || lastDigit == 6)) ||
(day == 2 && (lastDigit == 2 || lastDigit == 7)) ||
(day == 3 && (lastDigit == 3 || lastDigit == 8)) ||
(day == 4 && (lastDigit == 4 || lastDigit == 9)) ||
(day == 5 && (lastDigit == 5 || lastDigit == 0))) {
restriction = 'yes';
}
} else if (haze >= 400) {
if ((day == 1 || day == 3 || day == 5) && (lastDigit == 1 || lastDigit == 3 || lastDigit == 5 || lastDigit == 7 || lastDigit == 9) ||
(day == 2 || day == 4) && (lastDigit == 0 || lastDigit == 2 || lastDigit == 4 || lastDigit == 6 || lastDigit == 8)) {
restriction = 'yes';
}
}
} else {
restriction = 'no';
}
printf('%d %s\n', lastDigit, restriction);
return 0;
}
输入输出样例
输入样例1
4 230 80801
输出样例1
1 no
输入样例2
3 300 67008
输出样例2
8 yes
代码解释
- 首先,程序读取输入的星期几、雾霾指数和车牌号。
- 接着,计算车牌号的最后一位数字。
- 然后,根据限行规则判断该车牌是否限行,并将结果存储在
restriction变量中。 - 最后,输出车牌号的最后一位数字和限行结果。
希望本文能够帮助您理解如何使用C语言解决车牌限行问题。如果您有任何疑问,请随时提出。
原文地址: https://www.cveoy.top/t/topic/bkdU 著作权归作者所有。请勿转载和采集!