袜子1双10元2双163双20 如果买4双袜子最少需要多少钱呢?输入第一行输入三个正整数a、b、c表示买1、2、3双袜子的钱数保证买3双比2双合适2双比1双合适。第二行有若干个正整数x表示问你买x双袜子最少需要多少钱以0结束0的问题不回答。输出一行一数样例输入 复制10 16 201 2 3 4 5 0样例输出 复制10162030用c语言
#include <stdio.h>
int main() { int a, b, c, x; scanf("%d%d%d", &a, &b, &c); while (scanf("%d", &x) && x) { if (x == 1) { printf("%d\n", a); } else if (x == 2) { printf("%d\n", b); } else if (x == 3) { printf("%d\n", c); } else { // 买4双及以上时,先买3双,再买剩下的 int price = c; for (int i = 4; i <= x; i++) { price += a; } printf("%d\n", price); } } return 0;
原文地址: https://www.cveoy.top/t/topic/hsM7 著作权归作者所有。请勿转载和采集!