跳到正文

题解

P2347题解

Tian_Stone约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟
题目
P2347

版本

  1. v1当前

题目分析

这道题可以使用暴力枚举法,尝试每一种可能,用 book 数组统计。(66 层 for 循环嵌套)

代码

C++
#include <bits/stdc++.h> //万能头
using namespace std;
int x[10];
int book[1005];
int main()
{
    for(int i = 1; i <= 6; i++)
        cin >> x[i];
    for(int a = 0; a <= x[1]; a++)
    {
        for(int b = 0; b <= x[2]; b++)
        {
            for(int c = 0; c <= x[3]; c++)
            {
                for(int d = 0; d <= x[4]; d++)
                {
                    for(int e = 0; e <= x[5]; e++)
                    {
                        for(int f = 0; f <= x[6]; f++) // 6层for循环,枚举个数
                        {
                            book[a + b * 2 + c * 3 + d * 5 + e * 10 + f * 20] = 1; // 数量 * 重量
                        }
                    }
                }
            }
        }
    }
    int ans = 0;
    for(int i = 1; i <= 1000 ;i++) // 从 1 开始,排除不选的情况
    {
        ans += book[i];
    }
    cout << "Total=" << ans; // 别忘输出 "Total="
    return 0;
}
原文