求子集重量之和c++

求子集重量之和(Calculate the sum of a subset’s weight)
时限:100ms 内存限制:10000K 总时限:1000ms

描述:

现有n件物品,已知它们的重量,求其中一个子集的重量之和。
There are n things, we are known their weight, calculate the sum of a subset’s weight.

输入:

先输入一个整数n,再输入n件物品的重量,最后输入n个元素表示子集(第i个元素为0,表示子集中不包含该物品,第i个元素为1,表示子集中包含该物品)。
Input a positive integer n first, and then Input the weight of n items, at last we input the n numbers indicate the subset (i-th element is 0, which means that the subset does not contain the i-th item, the i-th element is 1, which means that the subset contains the i-th item).

输出:

输出该子集的重量之和。
Output the sum of subset’s weight.

输入样例:

5
2 9 8 7 5
0 1 1 0 1

输出样例:

22

代码:

#include <iostream>

using namespace std;
#include<vector>
#include<algorithm>
typedef struct node{
int index;
int weight;
int flag;

}node;

int main()
{
   int sum;
   cin>>sum;
   vector<node>v;

   for(int i=0;i<sum;i++){
    int w;
    cin>>w;
    node n;
    n.index=i;
    n.weight=w;
   v.push_back(n);
   }
   for(int i=0;i<sum;i++){
   cin>>v[i].flag;
   }

   int answer=0;
   for(vector<node>::iterator it=v.begin();it!=v.end();it++){
    if((*it).flag==1){
        answer+=(*it).weight;
    }
   }
   cout<<answer<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值