check whether the subset(no need to be consective) and be sum of X
2014-06-22 12:00
776 查看
#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 ±Ì æ≤ª¥Ê‘⁄ */ 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 [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; }
相关文章推荐
- FW - check whether tree is balanced or not - find the max and min of one tree- 2013年12月18日13:12:04
- File Comparer - To compare two files and check whether they have the same content
- failed to sync branch You might need to open a shell and debug the state of this repo
- failed to sync branch You might need to open a shell and debug the state of this repo
- failed to sync branch You might need to open a shell and debug the state of this repo
- 提交到app store遇到checksum validation failed和an ssl error has occurred and a secure connection to the se
- failed to sync branch You might need to open a shell and debug the state of this repo
- failed to sync branch You might need to open a shell and debug the state of this repo
- failed to sync branch You might need to open a shell and debug the state of this repo
- Details of different phases of a spam-saint update and the logs that needs to be checked while impor
- System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list
- Row index must be greater than or equal to zero and less than the number of rows.
- Github出现failed to sync branch You might need to open a shell and debug the state of this repo的解决方法
- failed to sync branch You might need to open a shell and debug the state of this repo
- 【翻译】Why JavaScript Is and Will Continue to Be the First Choice of Programmers
- 【译】Why JavaScript Is and Will Continue to Be the First Choice of Programmers
- failed to sync branch You might need to open a shell and debug the state of this repo
- failed to sync branch You might need to open a shell and debug the state of this repo
- failed to sync branch You might need to open a shell and debug the state of this repo
- failed to sync branch You might need to open a shell and debug the state of this repo