2 solutions

  • 0
    @ 2022-2-19 12:01:50

    动态规划:dp[i][j]表示前i个砝码能不能称出j的重量,最后统计能称出多少种重量。

    #include<iostream>
    using namespace std;
    
    int dp[105][100005];
    int a[19];
    int main()
    {
    	
    	int n,sum=0;cin >> n;
    	for(int i=1;i<=n;i++){
    		cin>>a[i];
    		sum+=a[i];
    	}
    	dp[0][0] = 1;
    	for (int i = 1;i <= n;i++)
    	{
    		
    		for (int j = 0;j <= sum;j++)
    		{
    			
    			if (dp[i-1][j])
    			{
    				
    				dp[i][j] = 1;
    				dp[i][j + a[i]] = 1;
    				dp[i][abs(j - a[i])] = 1;
    			}
    		}
    	}
    	int ans = 0;
    	for (int i = 1;i <= sum;i++)if (dp[n][i]) ans++;
    	cout << ans;
    }
    • 1

    Information

    ID
    1555
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    8
    Tags
    # Submissions
    157
    Accepted
    29
    Uploaded By