题解 | #网购#
网购
https://www.nowcoder.com/practice/5d7dfd405e5f4e4fbfdff6862c46b751
#include <stdio.h>
int checkDate(int monthInput, int dateInput);
int main() {
// 思路
// 阶段一: 准备
// 1. 各种变量(注意变量的类型)
// 阶段二: 输入
// 1. scanf()
// 阶段三: 处理
// 1. 保证输入日期只有双11和双12
// 2.
// - 确定日期
// - 打几折
// - 有无优惠券
// 阶段四: 输出
double price;
int month, date;
int flagCoupon = 1; // 有优惠券为1,无优惠券为0
int flagDate = 0; // 1为双11,2为双12,0为默认值
scanf("%lf %d %d %d", &price, &month, &date, &flagCoupon);
getchar(); // 吸收缓冲区的换行符,好习惯
// 判断是否为有效日期哈
flagDate = checkDate(month, date);
if (flagDate) {
if (flagDate == 1 && flagCoupon == 1) {
price = price * 0.7 - 50;
} else if (flagDate == 1 && flagCoupon == 0) {
price = price * 0.7;
} else if (flagDate == 2 && flagCoupon == 1) {
price = price * 0.8 - 50;
} else {
price = price * 0.8;
}
// 如果商品比较便宜,可能会出现用了优惠券价格成负数的情况!
// 但是这不符合逻辑,因为商家不会倒给买家钱
price > 0 ? (price = price) : (price = 0);
printf("%.2lf\n", price);
}
getchar();
return 0;
}
int checkDate(int monthInput, int dateInput) {
if (monthInput == 11 && dateInput == 11) {
return 1;
} else if (monthInput == 12 && dateInput == 12) {
return 2;
}
// 不符合输入要求
return 0;
}