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;
}
相关推荐
历程里程碑18 小时前
LeetCode热题11:盛水容器双指针妙解
c语言·数据结构·c++·经验分享·算法·leetcode·职场和发展
wadesir1 天前
Rust中的条件变量详解(使用Condvar的wait方法实现线程同步)
开发语言·算法·rust
yugi9878381 天前
基于MATLAB实现协同过滤电影推荐系统
算法·matlab
TimberWill1 天前
哈希-02-最长连续序列
算法·leetcode·排序算法
Morwit1 天前
【力扣hot100】64. 最小路径和
c++·算法·leetcode
leoufung1 天前
LeetCode 373. Find K Pairs with Smallest Sums:从暴力到堆优化的完整思路与踩坑
java·算法·leetcode
wifi chicken1 天前
数组遍历求值,行遍历和列遍历谁更快
c语言·数据结构·算法
胡楚昊1 天前
NSSCTF动调题包通关
开发语言·javascript·算法
Gold_Dino1 天前
agc011_e 题解
算法
bubiyoushang8881 天前
基于蚁群算法的直流电机PID参数整定 MATLAB 实现
数据结构·算法·matlab