HJ41 称砝码

Powered by:NEFU AB-IN

Link

文章目录

HJ41 称砝码

题意

现有n种砝码,重量互不相等,分别为 m1,m2,m3...mn ;

每种砝码对应的数量为 x1,x2,x3...xn 。现在要用这些砝码去称物体的重量(放在同一侧),问能称出多少种不同的重量。

思路

求给出的重量,通过排列组合,能求出多少不同的重量

可以采取set去重,一个set保存答案集合(初始放个0),遍历砝码,每次给答案集合的元素都加上砝码重量,set会自动去重

具体操作实现,可以再建一个set,用来保存上一次答案集合的状态,遍历这个set的元素,加上砝码重量,然后塞进答案集合中
*

代码

cpp 复制代码
#include <bits/stdc++.h>
#include <vector>
using namespace std;
#define int long long
#undef int

#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define IOS                                                                                                            \
    ios::sync_with_stdio(false);                                                                                       \
    cin.tie(nullptr);                                                                                                  \
    cout.tie(nullptr)
#define DEBUG(X) cout << #X << ": " << X << '\n'

const int N = 1e2 + 10, INF = 0x3f3f3f3f;

int a[N];
vector<int> v;
signed main()
{
    // freopen("Tests/input_1.txt", "r", stdin);
    IOS;

    set <int> s;

    int n;
    cin >> n;
    for(int i = 1; i <= n; ++i) cin >> a[i];

    for(int i = 1; i <= n; ++ i){
        int x;
        cin >> x;
        for(int j = 1; j <= x; ++ j) v.push_back(a[i]);
    }

    s.insert(0);
    for(auto i : v){
        set <int> tmp(s);
        for(auto k : tmp) s.insert(k + i);
    }

    cout << SZ(s);

    return 0;
}
相关推荐
05Kevin5 小时前
lk每日冒险题--数据结构6.27
算法
To_OC16 小时前
从一次栈溢出报错说起,我把递归彻底扒明白了
javascript·算法·程序员
千纸鹤安安21 小时前
千问Qwen-AgentWorld来了:一个语言模型搞定七大Agent场景,GPT-5.4都输了
算法
七牛开发者1 天前
MCP 到底是什么?为什么 Agent 都想接上它
算法·aigc·agent
kisshyshy1 天前
从递归到迭代,一文吃透二叉树的核心知识与 JavaScript 实现
javascript·算法·代码规范
To_OC2 天前
LC 49 字母异位词分组:想到哈希表很简单,选对 key 才是精髓
javascript·算法·leetcode
用户938515635072 天前
从 O(n²) 到 O(nlogn):一文读懂快速排序的“快”与“妙”
javascript·算法