题解 | #数组分组#

数组分组

http://www.nowcoder.com/practice/9af744a3517440508dbeb297020aca86

#include <stdio.h>
int dfs(int *in,int len,int five,int three){
    if((len==0)&&(five==three)) return 1;
    else if((len==0)&&(five!=three)) return 0;
    int next[len-1];
    for(int i=0;i<len;i++){
        for(int j=0;j<len-1;j++){
            if(j<i) next[j]=in[j];
            else if(j>=i) next[j]=in[j+1];
        }
        if(dfs(next,len-1,five+in[i],three)) return 1;
        if(dfs(next,len-1,five,three+in[i])) return 1;
    }
    return 0;
}
int main(){
    int five,three,n,i,tem,len;

    while (scanf("%d",&n)!=-1){
        int input[n];
        five=0;
        three=0;
        len=0;
        for(i=0;i<n;i++){
            scanf("%d",&tem);
            if(tem%5==0) five+=tem;
            else if(tem%3==0) three+=tem;
            else{
                input[len]=tem;
                len++;
            }
        }
        int total=0;
        for(i=0;i<len;i++) total+=input[i];
        if(((total+five+three)%2==0)&&(dfs(input,len, five, three))) printf("true\n");
        else printf("false\n");
    }
    return 0;
}
全部评论

相关推荐

1 1 评论
分享
牛客网
牛客企业服务