跳过正文

丙加·第7章·答案

·662 字·4 分钟·
  1. 使用if-else实现:

    #include <iostream>
    
    int main() {
        // 显示菜单
        std::cout << "[1] 西瓜 ¥2/斤" << std::endl;
        std::cout << "[2] 大象 ¥1919/斤" << std::endl;
        std::cout << "[3] 生瓜蛋子 ¥0.5/斤" << std::endl;
        std::cout << "[4] 吸铁石 ¥42/斤" << std::endl;
    
        // 获取用户输入
        int choice = 0;
        std::cout << "输入您要购买的商品:";
        std::cin >> choice;
    
        // 使用if-else处理用户选择
        if (choice == 1) {
            std::cout << "您购买了\"西瓜\"。" << std::endl;
        } else if (choice == 2) {
            std::cout << "您购买了\"大象\"。" << std::endl;
        } else if (choice == 3) {
            std::cout << "您购买了\"生瓜蛋子\"。" << std::endl;
        } else if (choice == 4) {
            std::cout << "您购买了\"吸铁石\"。" << std::endl;
        } else {
            std::cout << "没有这个商品。" << std::endl;
        }
    
        return 0;
    }
    

    使用switch-case实现:

    #include <iostream>
    
    int main() {
        // 显示菜单
        std::cout << "[1] 西瓜 ¥2/斤" << std::endl;
        std::cout << "[2] 大象 ¥1919/斤" << std::endl;
        std::cout << "[3] 生瓜蛋子 ¥0.5/斤" << std::endl;
        std::cout << "[4] 吸铁石 ¥42/斤" << std::endl;
    
        // 获取用户输入
        int choice = 0;
        std::cout << "输入您要购买的商品:";
        std::cin >> choice;
    
        // 使用switch-case处理用户选择
        switch (choice) {
            case 1:
                std::cout << "您购买了\"西瓜\"。" << std::endl;
                break;
            case 2:
                std::cout << "您购买了\"大象\"。" << std::endl;
                break;
            case 3:
                std::cout << "您购买了\"生瓜蛋子\"。" << std::endl;
                break;
            case 4:
                std::cout << "您购买了\"吸铁石\"。" << std::endl;
                break;
            default:
                std::cout << "没有这个商品。" << std::endl;
                break;
        }
    
        return 0;
    }
    
  2. 可以这样实现:

    #include <iostream>
    
    int main() {
        std::cout << "[1] 西瓜 2/斤" << std::endl;
        std::cout << "[2] 大象 1919/斤" << std::endl;
        std::cout << "[3] 生瓜蛋子 0.5/斤" << std::endl;
        std::cout << "[4] 吸铁石 42/斤" << std::endl;
        std::cout << "[0] 退出" << std::endl;
    
        // 使用while(1)循环
        while (true) {
            int choice = 0;
            std::cout << "输入您要购买的商品:";
            std::cin >> choice;
    
            // 使用if-else处理用户选择
            if (choice == 1) {
                std::cout << "您购买了\"西瓜\"。" << std::endl;
            } else if (choice == 2) {
                std::cout << "您购买了\"大象\"。" << std::endl;
            } else if (choice == 3) {
                std::cout << "您购买了\"生瓜蛋子\"。" << std::endl;
            } else if (choice == 4) {
                std::cout << "您购买了\"吸铁石\"。" << std::endl;
            } else if (choice == 0) {
                break; // 退出循环
            } else {
                std::cout << "没有这个商品。" << std::endl;
            }
        }
    
        return 0;
    }
    
  3. 基本的实现方式:

    #include <iostream>
    
    int main() {
        int choice = 0;
    
        // 使用do-while循环
        do {
            // 每次循环都显示菜单
            std::cout << "[1] 西瓜 2/斤" << std::endl;
            std::cout << "[2] 大象 1919/斤" << std::endl;
            std::cout << "[3] 生瓜蛋子 0.5/斤" << std::endl;
            std::cout << "[4] 吸铁石 42/斤" << std::endl;
            std::cout << "[0] 退出" << std::endl;
    
            std::cout << "输入您要购买的商品:";
            std::cin >> choice;
    
            // 使用switch-case处理用户选择
            switch (choice) {
                case 1:
                    std::cout << "您购买了\"西瓜\"。" << std::endl;
                    break;
                case 2:
                    std::cout << "您购买了\"大象\"。" << std::endl;
                    break;
                case 3:
                    std::cout << "您购买了\"生瓜蛋子\"。" << std::endl;
                    break;
                case 4:
                    std::cout << "您购买了\"吸铁石\"。" << std::endl;
                    break;
                case 0:
                    break;
                default:
                    std::cout << "没有这个商品。" << std::endl;
                    break;
            }
        } while (choice != 0); // 当用户选择0时退出循环
    
        return 0;
    }
    

    当然,如果学习了更多知识,你甚至可以这样写:

    #include <iostream>
    #include <map>
    #include <string>
    
    int main() {
        // 使用map存储商品信息:编号 -> {名称, 价格}
        std::map<int, std::pair<std::string, double>> products = {
            {1, {"西瓜", 2.0}},
            {2, {"大象", 1919.0}},
            {3, {"生瓜蛋子", 0.5}},
            {4, {"吸铁石", 42.0}}
        };
    
        int choice = 0;
    
        do {
            // 显示菜单
            for (const auto& item : products) {
                std::cout << "[" << item.first << "] " << item.second.first 
                          << " " << item.second.second << "/斤" << std::endl;
            }
            std::cout << "[0] 退出" << std::endl;
    
            std::cout << "输入您要购买的商品:";
            std::cin >> choice;
    
            // 处理用户选择
            if (choice == 0)  break;
    
            auto it = products.find(choice);
            if (it != products.end()) {
                std::cout << "您购买了\"" << it->second.first << "\"。"
                          << "价格:" << it->second.second << "元/斤" << std::endl;
            } else {
                std::cout << "没有这个商品。" << std::endl;
            }
        } while (true);
    
        return 0;
    

    这样写可以使所有商品信息集中在一起,让商品易于扩展,菜单也得以自动生成,还消除了冗长的switch-case(或者if-else)语句,可以让程序更方便、更健壮。记住这个卖瓜,我们后面学习class的时候还会回来的。

  4. 较简单的版本如下:

    #include <iostream>
    
    int main() {
        for (int i = 1; i < 1000; i++) {
            // 检查是否能被7整除
            if (i % 7 != 0) {
                continue; // 不能被7整除,直接跳过后续检查,进入下一次循环,节省时间
            }
    
            // 检查是否包含数字7
            int num = i;
            bool hasSeven = false; // 标记是否包含7
            while (num > 0) {
                int digit = num % 10; // 取最后一位数字
                if (digit == 7) {
                    hasSeven = true;
                    break; // 包含数字7,跳出循环
                }
                num /= 10; // 去掉最后一位
            }
    
            // 如果包含数字7,则跳过输出
            if (hasSeven) {
                continue;
            }
    
            // 能被7整除且不包含7,输出幸运数字
            std::cout << i << " ";
        }
        return 0;
    }
    
命令提示符@CommandPrompt-Wang
作者
命令提示符@CommandPrompt-Wang