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

华为机试—01背包(等于总值)

时间:2015-01-11 14:56:20      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:华为机试   01背包   等于总值   

给定一个总值,和一个整数数组,从数组中找出和等于总值的那几个数,

如果存在,数相应的下标为1,其余的为0,如果不存在,输出no。


#include <stdio.h>
#include <string.h>

int a[50];
int f[50]={0};

int find(int n,int m)
{
	if(n==0)
		return 1;//刚好递归结束
	else if(n<0||n>0&&m==0)
		return 0;
	else 
	{
		if(find(n-a[m-1],m-1))
		{
			f[m-1]=1;
			return 1;
		}
		else
			find(n,m-1);
	}
}

int main()
{
	int n,m,i;
	scanf("%d%d",&n,&m);

	for(i=0;i<m;i++)
		scanf("%d",&a[i]);

	if(find(n,m))
	{
		for(i=0;i<m;i++)
			printf("%d ",f[i]);
		printf("\n");
	}
	else
		printf("no\n");

	return 0;
}

技术分享

华为机试—01背包(等于总值)

标签:华为机试   01背包   等于总值   

原文地址:http://blog.csdn.net/wtyvhreal/article/details/42610573

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