哈理工OJ 2115 Equal(DFS对整数划分)

61 篇文章 0 订阅
39 篇文章 0 订阅

题目链接:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=2115

Equal
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submit: 59(26 users) Total Accepted: 26(20 users) Rating: Special Judge: No
Description
Given a set of positive numbers, can you find that if these numbers can be separated into 6 subsets, so that the sum of each subsets is equal.

For example, a set of {1, 1, 1, 1, 1, 1} can meet the condition, but {1, 2, 3, 4, 5, 6} can not.

Input
The input contains multiple test cases.

The first line contains a integer T, means the number of test cases.

Each test case begins with a integer N(N<=30) the number of numbers in the set, followed by N integers, each number is between 1 and 10000.

Output
For each test case print “yes” if the set full-fill the problem, and “no” if not.

Sample Input
2
20 1 2 3 4 5 5 4 3 2 1 1 2 3 4 5 5 4 3 2 1
20 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Sample Output
yes
no
Source
ACM-ICPC黑龙江省第九届大学生程序设计竞赛选拔赛(2)

【中文题意】给你n个数,问你能不能用这n个数组成6个相等的数(每个数是其它数的和),并且这n个数每个数只能用一次。
【AC代码】

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;
#define LL long long

int a[35],n;
int book[35];

int dfs(int num,int j)
{
    for(int i=j;i<=n;i++)
    {
        if(i==n&&num!=a[i])
        {
            return 0;
        }
        if(book[i]==0&&num>=a[i])
        {
            book[i]=1;
            if(num-a[i]==0)
            {
                return 1;
            }
            else if(dfs(num-a[i],i+1)==1)
            {
                return 1;
            }
            else
            {
                book[i]=0;
            }

        }
    }
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int maxn=0,sum=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
            maxn=max(maxn,a[i]);
            sum+=a[i];
            book[i]=0;
        }
        if(sum%6!=0||maxn>sum/6)
        {
            printf("no\n");
        }
        else
        {
            //printf("++%d\n",sum);
            int ss=0;
            for(int i=1;i<=6;i++)
            {
                if(dfs(sum/6,1))ss++;
                /*printf("***\n");
                for(int i=1;i<=n;i++)
                {
                    printf("%d ",book[i]);
                }
                printf("\n");*/
            }
            //printf("%d\n",ss);
            if(ss==6)
            {
                printf("yes\n");
            }
            else
            {
                printf("no\n");
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值