打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
整数和为n的划分(不能重复)
1-19的整数和为20如: 
1+19=20 
1+2+17=20 
1+2+3+14=20 
1+2+3+4+10=20 

  2+18=20 
  2+3+15=20 
  2+3+4+11=20 
  2+3+4+5+6=20 

    3+17=20 
    3+4+13=20 
    3+4+5+8=30 

      4+16=20 
      4+5+11=20 

        5+15=20 
5+6+9=20 
          
         6+14=20 
注:每个加数不能重复比如(4+5+5+6=20 5和5重复不可以。1+19和19+1也不可以2+18和18+2等等)

下面要贴出的是我的解法:

public class Tttt {
public int a;
public Tttt(int b){
a=b;
}
public int q(){
int count=0 ;
for(int i=1;i<=a/2;i++){
count = count+part(i,a-i);
}
return count;
}
public int part(int low,int high){
int count = 0;
if(low >= high)
return 0;
else{
count = part(low+1,high-low-1)+1;
while(low + high != a && (--high - ++low > 0)){
int temp = 0;
temp = part(low+1,high-low-1)+1;
count = count + temp;
}
}
return count;
}
   
public static void main(String[] args){
Tttt t = new Tttt(20); //可以通过改变括号里的值来算出任意的整数和为n的组合数
System.out.println(t.q());
}
}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
剑指offer 31 整数中1出现的次数(从1到n整数中1出现的次数)
排序算法--折半插入排序(二分查找排序)
C语言中常用的查找方法2---折半查找法
Java教程教:你5分钟了解快速排序
快速排序
缠论选股公式(一笔回调出现底分型)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服