商品#C++

题目描述

小明去超市买商品,买了N件,每件商品的价格都是整数(价格不会全都一样),小明想知道这N件商品中,最贵商品和最便宜商品分别有几个(最贵和最便宜分别可以有多个)?请你编写程序帮忙小明计算。

输入

第1行有一个整数n(n<=100)
第2行有n个整数,用空格隔开。

输出

输出2个整数,用空格隔开,第1个整数代表最贵的商品有几个,第2个整数代表最便宜的商品有几个。

样例输入 
6
12 3 3 15 34 17
样例输出 
1 2

代码如下

#include<iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;
    int c1 = 1;  // 最贵数量
    int c2 = 1; // 最便宜数量
    int max = 0;
    int min = 10000000;
    while (n--) // 商品数量
    {
        int a;
        cin >> a;
        if (max < a) // 最贵
        {
            max = a;
            c1 = 1; // 有新最大值时重置计数器
        }
        else if (max == a) // 有多个最贵
        {
            c1++;
        }
        if (min > a) // 最便宜
        {
            min = a; // 有新的最小值时重置计数器
            c2 = 1;
        }
        else if (min == a)
        {
            c2++;
        }
    }
    cout << c1 << " " << c2 << endl;
    return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,你想了解关于超市商品管理系统的C++代码实现,不是吗?那我可以为你提供一些思路和建议。 首先,你需要确定超市商品管理系统的功能,比如商品的录入、查询、修改、删除、销售、统计等等。然后,根据这些功能,设计合适的数据结构和算法,以便实现这些功能。 下面是一个简单的超市商品管理系统的C++代码实现的示例: ```c++ #include <iostream> #include <string> #include <vector> using namespace std; // 商品结构体 struct Goods { string name; // 商品名称 double price; // 商品价格 int count; // 商品数量 }; // 超市类 class SuperMarket { private: vector<Goods> goodsList; // 商品列表 public: // 添加商品 void addGoods(const Goods& goods) { goodsList.push_back(goods); } // 修改商品 void modifyGoods(const string& name, const double& price, const int& count) { for (auto& goods : goodsList) { if (goods.name == name) { goods.price = price; goods.count = count; break; } } } // 删除商品 void deleteGoods(const string& name) { for (auto iter = goodsList.begin(); iter != goodsList.end(); iter++) { if (iter->name == name) { goodsList.erase(iter); break; } } } // 查询商品 void queryGoods(const string& name) { for (const auto& goods : goodsList) { if (goods.name == name) { cout << "Name: " << goods.name << ", Price: " << goods.price << ", Count: " << goods.count << endl; break; } } } // 列出所有商品 void listAllGoods() { for (const auto& goods : goodsList) { cout << "Name: " << goods.name << ", Price: " << goods.price << ", Count: " << goods.count << endl; } } // 统计商品总数 int countAllGoods() { int count = 0; for (const auto& goods : goodsList) { count += goods.count; } return count; } // 统计商品总销售额 double countTotalSales() { double totalSales = 0; for (const auto& goods : goodsList) { totalSales += goods.price * goods.count; } return totalSales; } }; int main() { SuperMarket sm; // 创建超市对象 // 添加商品 Goods g1 = {"apple", 2.5, 100}; Goods g2 = {"banana", 3.0, 80}; sm.addGoods(g1); sm.addGoods(g2); // 修改商品 sm.modifyGoods("apple", 2.8, 120); // 查询商品 sm.queryGoods("apple"); // 删除商品 sm.deleteGoods("banana"); // 列出所有商品 sm.listAllGoods(); // 统计商品总数 cout << "Total count: " << sm.countAllGoods() << endl; // 统计商品总销售额 cout << "Total sales: " << sm.countTotalSales() << endl; return 0; } ``` 这是一个简单的实现,你可以根据实际需要进行修改和完善。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Julie_Mol

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值