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<stdio.h> int main() { int n,a[2][100],i,j,sum=0; scanf("%d",&n); for(i=0;i<2;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } for(j=0;j<n;j++) { if(a[1][j]==1) sum=sum+a[0][j]; } printf("%d",sum); return 0; }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值