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;
}
相关推荐
怕浪猫3 小时前
GEO 优化到底是什么?AI时代内容创作者必须懂的新技能
算法·面试·github
朝朝辞暮i4 小时前
C++ 第 23 课:class —— 开始真正进入面向对象
开发语言·c++·算法
Evand J5 小时前
【MATLAB例程】三维RRT+APF避障路径规划与到达角(AOA)定位算法|三维路径优化与定位仿真例程
算法·matlab·路径规划·代码·定位·rrt·aoa
weixin_307779136 小时前
有限产能智能排产与动态重排智能体:从需求解构到技术实现
开发语言·人工智能·算法·架构
朝朝辞暮i6 小时前
C++ 第 27 课:智能指针 shared_ptr
开发语言·c++·算法
朝朝辞暮i7 小时前
C++ 第 29 课:回调函数 Callback
开发语言·c++·算法
Godspeed Zhao7 小时前
现代智能雷达技术14——算法(0)
线性代数·算法·雷达
可乐ea7 小时前
Agent 模型分级升级路由:用置信度阈值把贵模型省下来
人工智能·算法·机器学习·结构化输出·置信度路由·模型升级路由·大模型成本优化
Navigator_Z7 小时前
LeetCode //C - 1278. Palindrome Partitioning III
c语言·算法·leetcode
Godspeed Zhao8 小时前
现代智能雷达技术18——算法(4)
算法