c++货币系统

货币系统

描述

给你一个n种面值的货币系统,求组成面值为m的货币有多少种方案。

输入

第一行为n和m。

输出

一行,方案数。

输入样例 1 

3 10        //3种面值组成面值为10的方案
1           //面值1
2           //面值2
5           //面值5

输出样例 1

10          //有10种方案
#include <iostream>
#include <vector>

using namespace std;

int main() {
    int n, m;
    cin >> n >> m;

    vector<int> currency(n);
    for (int i = 0; i < n; ++i) {
        cin >> currency[i];
    }
    vector<long long> dp(m + 1, 0);
    dp[0] = 1;  
    for (int i = 0; i < n; ++i) {
        for (int j = currency[i]; j <= m; ++j) {
            dp[j] += dp[j - currency[i]];
        }
    }
    cout << dp[m] << endl;
    return 0;
}

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是一个简单的超市库存管理函数的例子,它使用了C++的类来实现: ```cpp #include <iostream> #include <string> using namespace std; class Product { public: string name; int quantity; double price; // 构造函数 Product(string n, int q, double p) { name = n; quantity = q; price = p; } // 输出商品信息 void printInfo() { cout << "Name: " << name << endl; cout << "Quantity: " << quantity << endl; cout << "Price: " << price << endl; } }; class Inventory { private: Product* products[100]; // 最多100种商品 int numProducts; // 当前商品数量 public: // 构造函数 Inventory() { numProducts = 0; } // 添加商品 void addProduct(Product* p) { products[numProducts] = p; numProducts++; } // 根据名称查找商品 Product* findProduct(string name) { for (int i = 0; i < numProducts; i++) { if (products[i]->name == name) { return products[i]; } } return nullptr; } // 更新商品数量 void updateQuantity(string name, int quantity) { Product* p = findProduct(name); if (p != nullptr) { p->quantity = quantity; } } // 输出所有商品信息 void printInventory() { for (int i = 0; i < numProducts; i++) { products[i]->printInfo(); cout << endl; } } }; int main() { // 初始化超市库存 Inventory inventory; Product* p1 = new Product("Apple", 10, 2.5); Product* p2 = new Product("Orange", 20, 1.5); inventory.addProduct(p1); inventory.addProduct(p2); // 输出所有商品信息 inventory.printInventory(); // 更新商品数量 inventory.updateQuantity("Apple", 15); // 输出更新后的商品信息 inventory.printInventory(); return 0; } ``` 这个例子中,我们使用了两个类:`Product` 和 `Inventory`。`Product` 类表示一个商品,包含名称、数量和价格等属性;`Inventory` 类表示整个超市的库存,包含一个商品数组和一些针对商品的操作,比如添加商品、查找商品和更新商品数量等。 在 `main` 函数中,我们首先创建了两个商品 `p1` 和 `p2`,然后将它们添加到 `inventory` 实例中。接着,我们输出了所有商品信息,然后更新了苹果的数量,最后再次输出所有商品信息。 当然,这只是一个简单的例子,实际的库存管理系统可能会更加复杂。例如,我们可能需要支持从文件中读取商品信息,或者支持多种货币的价格计算。但是,这个例子可以帮助你了解如何用 C++ 实现一个基本的库存管理函数。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值