码迷,mamicode.com
首页 > 其他好文 > 详细

check whether the subset(no need to be consective) and be sum of X

时间:2017-05-13 17:59:25      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:ons   ++   eth   subsets   false   article   tchar   turn   sizeof   

#include "stdafx.h"

bool isSubsetSum(int set[], int n, int sum)
{
	bool sumOfSubSet[100][100];

	for (int i=0; i<100; i++)
	{
		for (int j=0; j<100; j++)
		{
			sumOfSubSet[i][j] = false;
		}
	}

	/*
	*   F[i][j]  ±ì ? ¥”1-i÷–—°?°μ? ?÷–£¨∫??? j μ? ?∑ò¥ê‘?
	*   true ±ì ?¥ê‘? false ±ì ?≤a¥ê‘?
	*/


	for (int j=0; j<=sum; j++)
		sumOfSubSet[0][j] = false;

	for (int i=0; i<=n; i++)
		sumOfSubSet[i][0] = false;

	sumOfSubSet[0][0] = true;



	for (int i=1; i<=n; i++)
	{
		for (int j=1; j<=sum; j++)
		{
			sumOfSubSet[i][j] = sumOfSubSet[i-1][j];

			if ((j-set[i-1] >= 0) && (sumOfSubSet[i][j-set[i-1]]))
			{
				sumOfSubSet[i][j] = true;
			}
		}
	}

	return sumOfSubSet[n][sum];
}



int _tmain(int argc, _TCHAR* argv[])
{
	int set[] = {3, 34, 4, 12, 5, 2};
	int sum = 13;
	int n = sizeof(set)/sizeof(set[0]);
	if (isSubsetSum(set, n, sum) == true)
		 printf("Found a subset with given sum");
	else
		 printf("No subset with given sum");
	return 0;
}

check whether the subset(no need to be consective) and be sum of X

标签:ons   ++   eth   subsets   false   article   tchar   turn   sizeof   

原文地址:http://www.cnblogs.com/ljbguanli/p/6849310.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!