设为首页 加入收藏

TOP

最近面试的算法题目
2014-11-23 20:10:32 来源: 作者: 【 】 浏览:20
Tags:最近 面试 算法 题目

一个集合x有都不相同的n个元素,使用这个集合中的不定个数的元素,组成一个和为s的序列,求出所有符合的序列,元素可以重复使用,只要元素的个数相同不考虑顺序。

比如集合是x={2,3,4,5,7}; n=5, s=12可以得出以下的序列:

2 2 2 2 2 2
2 2 2 2 4
2 2 2 3 3
2 2 3 5
2 2 4 4
2 3 3 4
2 3 7
2 5 5
3 3 3 3
3 4 5
4 4 4
5 7








#include    
#include    
using namespace std;  
int PushVector(int *x,int n,int s,vector &tablelist,int Position)  
{  
    if(s<0)  
    {  
        tablelist.clear();  
        return 0;  
    }  
    else if(s==0)  
    {  
        for(int i=0;i0)  
    {  
        for(int i=Position;i newtablelist;  
            for (int j=0;j tablelist;  
    tablelist.clear();  
    PushVector(x,n,s,tablelist,0);  
    return 0;  
}  
  
  
  
  
int main(int argc, char** argv)   
{  
    int x[]= {2,3,4,5,7};  
    int n =5;  
    int s=12;  
    FindResult(x,n,s);  
    system("pause");  
    return 0;  
}  

#include 
#include 
using namespace std;
int PushVector(int *x,int n,int s,vector &tablelist,int Position)
{
	if(s<0)
	{
		tablelist.clear();
		return 0;
	}
	else if(s==0)
	{
		for(int i=0;i0)
	{
		for(int i=Position;i newtablelist;
			for (int j=0;j tablelist;
	tablelist.clear();
	PushVector(x,n,s,tablelist,0);
	return 0;
}




int main(int argc, char** argv) 
{
	int x[]= {2,3,4,5,7};
	int n =5;
	int s=12;
	FindResult(x,n,s);
	system("pause");
	return 0;
}

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇hdu 3996 (最大权闭合图) 下一篇hdu-4679-Terrorist’s destroy

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·微服务 Spring Boot (2025-12-26 18:20:10)
·如何调整 Redis 内存 (2025-12-26 18:20:07)
·MySQL 数据类型:从 (2025-12-26 18:20:03)
·Linux Shell脚本教程 (2025-12-26 17:51:10)
·Qt教程,Qt5编程入门 (2025-12-26 17:51:07)