题解 | #Knapsack Problem#

Knapsack Problem

https://ac.nowcoder.com/acm/problem/13882

遍历求和

#include <iostream>
using namespace std;

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        int n, w, c;
        cin >> n >> w >> c;
        int wc[2 * n];
        for (int i = 0; i < 2 * n; i++)
        {
            cin >> wc[i];
        }
        int wtotal = 0, ctotal = 0;
        for (int i = 0; i < n; i++)
        {
            wtotal += wc[i];
            ctotal += wc[i + n];
        }
        if (wtotal > w || ctotal > c)
        {
            cout << "NO" << endl;
        }
        else
        {
            cout << "YES" << endl;
        }
    }
    return 0;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 15:37
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务