The Values You Can Mak CF687C(简单DP)

题目链接:http://codeforces.com/problemset/problem/687/C

题意分析:给定n个数,用这n个数在和为k的情况下从n个数中找到所有的value,这些value之和也为k.

                    DP题我练的真的太少了,总是写不出状态状态方程,下一步打算好好刷刷紫书第八章的题目。

                    不能因为不擅长就逃避它们,感谢小伙伴给我讲这道题。定义数组dp[i][j] = 1表示由j 可以组成i

                    若dp[i][j] == 1则dp[i + c][j] = 1  dp[i + c][j + c] = 1; (c为常数)由此确立状态转移方程。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<iomanip>
#include<cctype>
#include<string>
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstdlib>
#define LL long long
typedef long long ll;
using namespace std;
const int maxn = 510;
//ll gcd(ll a, ll b)
//{
//    return !b ? a : gcd(b, a % b);
//}
//ll lcm(ll a, ll b)
//{
//    return a / gcd(a, b) * b;
//}

vector<int> ans;
int dp[maxn][maxn];
int main()
{
    int n, k;
    while(scanf("%d%d",&n, &k) == 2)
    {
        int x;
        dp[0][0] = 1;
        for(int i = 0; i < n; i++)
        {
            scanf("%d",&x);//输入x后判断它能否和其他数组成k(x必须小于等于k才有可能成立)
            for(int j = k; j >= x; j--)
            {
                for(int l = 0; l + x <= k; l++)
                {
                    if(dp[j - x][l])
                    {
                        dp[j][l] = 1; dp[j][l + x] = 1;
                    }
                }
            }
        }
        for(int i = 0; i <= k; i++)
        {
           if(dp[k][i]) ans.push_back(i);
        }
        printf("%d\n",ans.size());
        for(int i = 0; i < ans.size(); i++)
        {
            if(i) printf(" ");
            printf("%d",ans[i]);
        }
        printf("\n");
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值