数组分组

描述

编写一个函数,传入一个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;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值