牛骨文教育服务平台(让学习变的简单)
博文笔记

数组分组

创建时间:2015-08-27 投稿人: 浏览次数:645

描述

编写一个函数,传入一个int型数组,返回该数组能否分成两组,使得两组中各元素加起来的和相等,并且,所有5的倍数必须在其中一个组中,所有3的倍数在另一个组中(不包括5的倍数),能满足以上条件,返回true;不满足时返回false。 

知识点 字符串,循环,函数,指针,枚举,位运算,结构体,联合体,文件操作,递归
运行时间限制 10M
内存限制 128
输入

输入输入的数据个数

输入一个int型数组


输出

返回true或者false


样例输入 4 1 5 -5 1
样例输出

true

#include<iostream>

using namespace std;

bool finds(int shu[],int length,int current,int sum)

{

    if(current>=length)return false;

    else if(shu[current]%3==0||shu[current]>sum)

       return finds(shu,length,current+1,sum);

    else if(shu[current]<sum)

       return finds(shu,length,current+1,sum-shu[current])||finds(shu,length,current+1,sum);

    else

    {

       bool check=true;

       for(int i=current+1;i<length;i++)

       {

           if(shu[i]%5==0)

           {

              check=false;

           }

       }         

       return check||finds(shu,length,current+1,sum);

    }

}

int main()

{

    int ch[1024],n,sum=0;

    cin>>n;

    for (int i=0;i<n;i++)

    {

       cin>>ch[i];

       sum+=ch[i];

    }

    if(sum%2!=0)cout<<"false"<<endl;

    else if(finds(ch,n,0,sum/2))cout<<"true"<<endl;

    else cout<<"false"<<endl;

    return 0;

}


声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。