top1编程
← 返回题目
题解

【入门】买杯子

1 条题解

  • 0
    @ 2026-7-29 0:17:54
    #include <iostream>
    using namespace std;
    
    int main() {
        // 定义3个整型变量存储3个杯子的价格,读取输入
        int p1, p2, p3;
        cin >> p1 >> p2 >> p3;
        
        int count = 0;  // 统计符合条件的杯子数量,初始为0
        int total = 0;  // 统计符合条件的杯子总价,初始为0
        
        // 逐个判断价格是否在[10,20]区间,符合则计数+累加总价
        if (p1 >= 10 && p1 <= 20) {
            count++;
            total += p1;
        }
        if (p2 >= 10 && p2 <= 20) {
            count++;
            total += p2;
        }
        if (p3 >= 10 && p3 <= 20) {
            count++;
            total += p3;
        }
        
        // 按格式输出:数量 总价,空格分隔
        cout << count << " " << total << endl;
        
        return 0;
    }
    
    • 1