从1到20这20个数中找出所有和为30的组合,注意组合中不能有重复的数,
不知道用递归是不是很快,还是用其他的办法?看谁设计的最好了。

解决方案 »

  1.   

    /* min = 1, max = 20, sum = 30 */void test(int min, int max, int sum){
         int k = sum / 2;
         int n = sum - max < 1 ? 1 : sum - max;     for (int i = n; i < k; i ++){
              if ((sum - n) =< max && (sum - n) >= 0){
                   /* n 和 sum - n 就是需要的 */
              }
         }
    }
      

  2.   


    public class NumQuestion1 { static void Get()
    {
    int count=0;
    int j=0;
    for (int i = 20; i > j; i--) {
    j=30-i;
    System.out.println(i+"+"+j+"=30");
    ++count;
    }
    System.out.println("count="+count);
    }
    public static void main(String[] args) {
    Get();
    }
    }
    /*Output:
     *20+10=30
     *19+11=30
     *18+12=30
     *17+13=30
     *16+14=30
     *15+15=30
     *count=6
    */
      

  3.   

    组合中不能有重复的数.
    在 j = 30 - i; 后加上 if (i == j ) continue;
      

  4.   

    java爱好者可以加此群我们一块学习:100756746
      

  5.   

    不知道这个算不算简单
    public static void main(String[] args){
    int[] a=new int[20];
    for(int i=0,len=a.length;i<len;i++){
    a[i]=i+1;
    }

    int loopCount=0xffff;
    int sum,mask;
    boolean isSelect;
    List<Integer> nums=new ArrayList<Integer>();

    do{
    sum=0;
    mask=0x01;
    nums.clear();

    for(int i=0,len=a.length;i<len;i++){
    isSelect=(loopCount&mask)>0;
    if(isSelect){
    sum+=a[i];
    nums.add(a[i]);
    }
    mask<<=1;
    }

    if(sum==30) System.out.println(nums);

    }while(--loopCount>0);
    }
      

  6.   

    用20个二进制为代表选择情况0代表未选择,1代表选择.
    20个数的所有组合就是0x01-0xffff
      

  7.   

    晕,确实少打了一个,为了预防这种错误,改了下代码
    public static void main(String[] args){
    int[] a=new int[20];
    for(int i=0,len=a.length;i<len;i++){
    a[i]=i+1;
    }

    int sum,mask;
    boolean isSelect;
    List<Integer> nums=new ArrayList<Integer>();

    for(int loopCount=1<<a.length;loopCount>0;loopCount--){
    sum=0;
    mask=0x01;
    nums.clear();

    for(int i=0,len=a.length;i<len;i++){
    isSelect=(loopCount&mask)>0;
    if(isSelect){
    sum+=a[i];
    nums.add(a[i]);
    }
    mask<<=1;
    }

    if(sum==30) System.out.println(nums);
    }
    }
      

  8.   

    先求出 sum = 1+2+3+...+20; 
    b = 1 + 2;
    e = sum - b;
    大概是这样
      

  9.   

    mask 和isSelect的语句是什么意思呢,有什么作用,谢谢~!
    还有for(int i=0,len=a.length;i<len;i++)中,为什么不能直接写i<a.length呢,谢谢!
      

  10.   

    对代码有点大致了解,应该还可以优化一下吧,最多只有七个数相加 最少有两个
    =========
    这个优化性能提高很多,应该可以在内层的for中判断>30&&就立即终止啦
      

  11.   

    package com.jwh.cn;import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;public class Num {
    /*
     * 1-20中所有和为30的组合。。数字不能重复
     */
    int result  ;
    List list = new ArrayList();

    public Num(int sesult,int num2){
    this.result = sesult;
    for(int i = 1;i<num2+1;i++){
    list.add(i);
    }
    }

    public void print(int sum,List list){
    int maxNum = list.size();
    for(int i = maxNum-1; i>=0 ; i--){
    int rNum = sum - (Integer)list.get(i);
    if(rNum<0){}
    else if(rNum==0){
    System.out.println((Integer)list.get(i)+"=");
    break;
    }else if(rNum>=(Integer)list.get(0)){
    System.out.print(list.get(i)+"+");
    list.remove(i);
    print(rNum,list);
    }

    }

    }

    public static void main(String[] args) {
    Num num = new Num(15,10);

    List list = num.list;
    num.print(num.result, list);
    }
    }递归做法 手稿有问题仅供参考高手指导下 我哪里做的有问题。。
      

  12.   

    line 1:1+2+3+4+5+6+9
    line 2:1+2+3+4+5+7+8
    line 3:1+2+3+4+5+15
    line 4:1+2+3+4+6+14
    line 5:1+2+3+4+7+13
    line 6:1+2+3+4+8+12
    line 7:1+2+3+4+9+11
    line 8:1+2+3+4+20
    line 9:1+2+3+5+6+13
    line 10:1+2+3+5+7+12
    line 11:1+2+3+5+8+11
    line 12:1+2+3+5+9+10
    line 13:1+2+3+5+19
    line 14:1+2+3+6+7+11
    line 15:1+2+3+6+8+10
    line 16:1+2+3+6+18
    line 17:1+2+3+7+8+9
    line 18:1+2+3+7+17
    line 19:1+2+3+8+16
    line 20:1+2+3+9+15
    line 21:1+2+3+10+14
    line 22:1+2+3+11+13
    line 23:1+2+4+5+6+12
    line 24:1+2+4+5+7+11
    line 25:1+2+4+5+8+10
    line 26:1+2+4+5+18
    line 27:1+2+4+6+7+10
    line 28:1+2+4+6+8+9
    line 29:1+2+4+6+17
    line 30:1+2+4+7+16
    line 31:1+2+4+8+15
    line 32:1+2+4+9+14
    line 33:1+2+4+10+13
    line 34:1+2+4+11+12
    line 35:1+2+5+6+7+9
    line 36:1+2+5+6+16
    line 37:1+2+5+7+15
    line 38:1+2+5+8+14
    line 39:1+2+5+9+13
    line 40:1+2+5+10+12
    line 41:1+2+6+7+14
    line 42:1+2+6+8+13
    line 43:1+2+6+9+12
    line 44:1+2+6+10+11
    line 45:1+2+7+8+12
    line 46:1+2+7+9+11
    line 47:1+2+7+20
    line 48:1+2+8+9+10
    line 49:1+2+8+19
    line 50:1+2+9+18
    line 51:1+2+10+17
    line 52:1+2+11+16
    line 53:1+2+12+15
    line 54:1+2+13+14
    line 55:1+3+4+5+6+11
    line 56:1+3+4+5+7+10
    line 57:1+3+4+5+8+9
    line 58:1+3+4+5+17
    line 59:1+3+4+6+7+9
    line 60:1+3+4+6+16
    line 61:1+3+4+7+15
    line 62:1+3+4+8+14
    line 63:1+3+4+9+13
    line 64:1+3+4+10+12
    line 65:1+3+5+6+7+8
    line 66:1+3+5+6+15
    line 67:1+3+5+7+14
    line 68:1+3+5+8+13
    line 69:1+3+5+9+12
    line 70:1+3+5+10+11
    line 71:1+3+6+7+13
    line 72:1+3+6+8+12
    line 73:1+3+6+9+11
    line 74:1+3+6+20
    line 75:1+3+7+8+11
    line 76:1+3+7+9+10
    line 77:1+3+7+19
    line 78:1+3+8+18
    line 79:1+3+9+17
    line 80:1+3+10+16
    line 81:1+3+11+15
    line 82:1+3+12+14
    line 83:1+4+5+6+14
    line 84:1+4+5+7+13
    line 85:1+4+5+8+12
    line 86:1+4+5+9+11
    line 87:1+4+5+20
    line 88:1+4+6+7+12
    line 89:1+4+6+8+11
    line 90:1+4+6+9+10
    line 91:1+4+6+19
    line 92:1+4+7+8+10
    line 93:1+4+7+18
    line 94:1+4+8+17
    line 95:1+4+9+16
    line 96:1+4+10+15
    line 97:1+4+11+14
    line 98:1+4+12+13
    line 99:1+5+6+7+11
    line 100:1+5+6+8+10
    line 101:1+5+6+18
    line 102:1+5+7+8+9
    line 103:1+5+7+17
    line 104:1+5+8+16
    line 105:1+5+9+15
    line 106:1+5+10+14
    line 107:1+5+11+13
    line 108:1+6+7+16
    line 109:1+6+8+15
    line 110:1+6+9+14
    line 111:1+6+10+13
    line 112:1+6+11+12
    line 113:1+7+8+14
    line 114:1+7+9+13
    line 115:1+7+10+12
    line 116:1+8+9+12
    line 117:1+8+10+11
    line 118:1+9+20
    line 119:1+10+19
    line 120:1+11+18
    line 121:1+12+17
    line 122:1+13+16
    line 123:1+14+15
    line 124:2+3+4+5+6+10
    line 125:2+3+4+5+7+9
    line 126:2+3+4+5+16
    line 127:2+3+4+6+7+8
    line 128:2+3+4+6+15
    line 129:2+3+4+7+14
    line 130:2+3+4+8+13
    line 131:2+3+4+9+12
    line 132:2+3+4+10+11
    line 133:2+3+5+6+14
    line 134:2+3+5+7+13
    line 135:2+3+5+8+12
    line 136:2+3+5+9+11
    line 137:2+3+5+20
    line 138:2+3+6+7+12
    line 139:2+3+6+8+11
    line 140:2+3+6+9+10
    line 141:2+3+6+19
    line 142:2+3+7+8+10
    line 143:2+3+7+18
    line 144:2+3+8+17
    line 145:2+3+9+16
    line 146:2+3+10+15
    line 147:2+3+11+14
    line 148:2+3+12+13
    line 149:2+4+5+6+13
    line 150:2+4+5+7+12
    line 151:2+4+5+8+11
    line 152:2+4+5+9+10
    line 153:2+4+5+19
    line 154:2+4+6+7+11
    line 155:2+4+6+8+10
    line 156:2+4+6+18
    line 157:2+4+7+8+9
    line 158:2+4+7+17
    line 159:2+4+8+16
    line 160:2+4+9+15
    line 161:2+4+10+14
    line 162:2+4+11+13
    line 163:2+5+6+7+10
    line 164:2+5+6+8+9
    line 165:2+5+6+17
    line 166:2+5+7+16
    line 167:2+5+8+15
    line 168:2+5+9+14
    line 169:2+5+10+13
    line 170:2+5+11+12
    line 171:2+6+7+15
    line 172:2+6+8+14
    line 173:2+6+9+13
    line 174:2+6+10+12
    line 175:2+7+8+13
    line 176:2+7+9+12
    line 177:2+7+10+11
    line 178:2+8+9+11
    line 179:2+8+20
    line 180:2+9+19
    line 181:2+10+18
    line 182:2+11+17
    line 183:2+12+16
    line 184:2+13+15
    line 185:3+4+5+6+12
    line 186:3+4+5+7+11
    line 187:3+4+5+8+10
    line 188:3+4+5+18
    line 189:3+4+6+7+10
    line 190:3+4+6+8+9
    line 191:3+4+6+17
    line 192:3+4+7+16
    line 193:3+4+8+15
    line 194:3+4+9+14
    line 195:3+4+10+13
    line 196:3+4+11+12
    line 197:3+5+6+7+9
    line 198:3+5+6+16
    line 199:3+5+7+15
    line 200:3+5+8+14
    line 201:3+5+9+13
    line 202:3+5+10+12
    line 203:3+6+7+14
    line 204:3+6+8+13
    line 205:3+6+9+12
    line 206:3+6+10+11
    line 207:3+7+8+12
    line 208:3+7+9+11
    line 209:3+7+20
    line 210:3+8+9+10
    line 211:3+8+19
    line 212:3+9+18
    line 213:3+10+17
    line 214:3+11+16
    line 215:3+12+15
    line 216:3+13+14
    line 217:4+5+6+7+8
    line 218:4+5+6+15
    line 219:4+5+7+14
    line 220:4+5+8+13
    line 221:4+5+9+12
    line 222:4+5+10+11
    line 223:4+6+7+13
    line 224:4+6+8+12
    line 225:4+6+9+11
    line 226:4+6+20
    line 227:4+7+8+11
    line 228:4+7+9+10
    line 229:4+7+19
    line 230:4+8+18
    line 231:4+9+17
    line 232:4+10+16
    line 233:4+11+15
    line 234:4+12+14
    line 235:5+6+7+12
    line 236:5+6+8+11
    line 237:5+6+9+10
    line 238:5+6+19
    line 239:5+7+8+10
    line 240:5+7+18
    line 241:5+8+17
    line 242:5+9+16
    line 243:5+10+15
    line 244:5+11+14
    line 245:5+12+13
    line 246:6+7+8+9
    line 247:6+7+17
    line 248:6+8+16
    line 249:6+9+15
    line 250:6+10+14
    line 251:6+11+13
    line 252:7+8+15
    line 253:7+9+14
    line 254:7+10+13
    line 255:7+11+12
    line 256:8+9+13
    line 257:8+10+12
    line 258:9+10+11
    line 259:10+20
    line 260:11+19
    line 261:12+18
    line 262:13+17
    line 263:14+16
      

  13.   

    public class NumQuestion1 {    static void Get()
        {        
            int count=0;
            int j=0;
            for (int i = 20; i > j; i--) {
                j=30-i;
                System.out.println(i+"+"+j+"=30");
                ++count;
            }
            System.out.println("count="+count);
        }
        public static void main(String[] args) {
            Get();
        }
    }
      

  14.   

    考虑到最多7位数相加为30:1+2+3+4+5+6+9=30 for(int i = 1;i<=20;i++){
    for(int j=i+1;j<=20;j++){
    if(i+j==30){
    System.out.println(i+"+"+j+"=30");
    }
    for(int x=j+1;x<=20;x++){
    if(i+j+x==30){
    System.out.println(i+"+"+j+"+"+x+" =30");
    }
    for(int y=x+1;y<=20;y++){
    if(i+j+x+y==30){
    System.out.println(i+"+"+j+"+"+x+"+"+y+" =30");
    }
    for(int a=y+1;a<=20;a++){
    if(i+j+x+y+a==30){
    System.out.println(i+"+"+j+"+"+x+"+"+y+"+"+a+"=30");
    }
    for(int b=a+1;b<=20;b++){
    if(i+j+x+y+a+b==30){
    System.out.println(i+"+"+j+"+"+x+"+"+y+"+"+a+"+"+b+"=30");
    }
    for(int c=b+1;c<=20;c++){
    if(i+j+x+y+a+b+c == 30){
    System.out.println(i+"+"+j+"+"+x+"+"+y+"+"+a+"+"+b+"+"+c+"=30");
    }
    }
    }
    }
    }
    }
    }
    }
      

  15.   

    额,递归实现:
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Iterator;public class test {
    public static void main(String[] args) {
    ArrayList<ArrayList<Integer>> list = getArrays(1, 20, 30);
    Iterator<ArrayList<Integer>> iter = list.iterator();
    while (iter.hasNext())
    System.out.println(Arrays.toString(iter.next().toArray()));
    }

    public static int sum(int min, int max) {
    if (min > max)
    return 0;
    return (min + max) * (max - min + 1) / 2;
    }

    public static ArrayList<ArrayList<Integer>> getArrays(int min, int max, int sum) {
    if (sum(min, max) < sum)
    return null;
    ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
    for (int i = max; i >= min; i--) {
    ArrayList<Integer> array;
    if (i > sum)
    continue;
    else if (i == sum) {
    array = new ArrayList<Integer>();
    array.add(i);
    list.add(array);
    }
    else {
    ArrayList<ArrayList<Integer>> temp = getArrays(min, i - 1, sum - i);
    if (temp == null)
    continue;
    Iterator<ArrayList<Integer>> iter = temp.iterator();
    while (iter.hasNext()) {
    array = iter.next();
    array.add(i);
    list.add(array);
    }
    }
    }
    return list;
    }
    }
    输出:
    [10, 20]
    [1, 9, 20]
    [2, 8, 20]
    [3, 7, 20]
    [1, 2, 7, 20]
    [4, 6, 20]
    [1, 3, 6, 20]
    [1, 4, 5, 20]
    [2, 3, 5, 20]
    [1, 2, 3, 4, 20]
    [11, 19]
    [1, 10, 19]
    [2, 9, 19]
    [3, 8, 19]
    [1, 2, 8, 19]
    [4, 7, 19]
    [1, 3, 7, 19]
    [5, 6, 19]
    [1, 4, 6, 19]
    [2, 3, 6, 19]
    [2, 4, 5, 19]
    [1, 2, 3, 5, 19]
    [12, 18]
    [1, 11, 18]
    [2, 10, 18]
    [3, 9, 18]
    [1, 2, 9, 18]
    [4, 8, 18]
    [1, 3, 8, 18]
    [5, 7, 18]
    [1, 4, 7, 18]
    [2, 3, 7, 18]
    [1, 5, 6, 18]
    [2, 4, 6, 18]
    [1, 2, 3, 6, 18]
    [3, 4, 5, 18]
    [1, 2, 4, 5, 18]
    [13, 17]
    [1, 12, 17]
    [2, 11, 17]
    [3, 10, 17]
    [1, 2, 10, 17]
    [4, 9, 17]
    [1, 3, 9, 17]
    [5, 8, 17]
    [1, 4, 8, 17]
    [2, 3, 8, 17]
    [6, 7, 17]
    [1, 5, 7, 17]
    [2, 4, 7, 17]
    [1, 2, 3, 7, 17]
    [2, 5, 6, 17]
    [3, 4, 6, 17]
    [1, 2, 4, 6, 17]
    [1, 3, 4, 5, 17]
    [14, 16]
    [1, 13, 16]
    [2, 12, 16]
    [3, 11, 16]
    [1, 2, 11, 16]
    [4, 10, 16]
    [1, 3, 10, 16]
    [5, 9, 16]
    [1, 4, 9, 16]
    [2, 3, 9, 16]
    [6, 8, 16]
    [1, 5, 8, 16]
    [2, 4, 8, 16]
    [1, 2, 3, 8, 16]
    [1, 6, 7, 16]
    [2, 5, 7, 16]
    [3, 4, 7, 16]
    [1, 2, 4, 7, 16]
    [3, 5, 6, 16]
    [1, 2, 5, 6, 16]
    [1, 3, 4, 6, 16]
    [2, 3, 4, 5, 16]
    [1, 14, 15]
    [2, 13, 15]
    [3, 12, 15]
    [1, 2, 12, 15]
    [4, 11, 15]
    [1, 3, 11, 15]
    [5, 10, 15]
    [1, 4, 10, 15]
    [2, 3, 10, 15]
    [6, 9, 15]
    [1, 5, 9, 15]
    [2, 4, 9, 15]
    [1, 2, 3, 9, 15]
    [7, 8, 15]
    [1, 6, 8, 15]
    [2, 5, 8, 15]
    [3, 4, 8, 15]
    [1, 2, 4, 8, 15]
    [2, 6, 7, 15]
    [3, 5, 7, 15]
    [1, 2, 5, 7, 15]
    [1, 3, 4, 7, 15]
    [4, 5, 6, 15]
    [1, 3, 5, 6, 15]
    [2, 3, 4, 6, 15]
    [1, 2, 3, 4, 5, 15]
    [3, 13, 14]
    [1, 2, 13, 14]
    [4, 12, 14]
    [1, 3, 12, 14]
    [5, 11, 14]
    [1, 4, 11, 14]
    [2, 3, 11, 14]
    [6, 10, 14]
    [1, 5, 10, 14]
    [2, 4, 10, 14]
    [1, 2, 3, 10, 14]
    [7, 9, 14]
    [1, 6, 9, 14]
    [2, 5, 9, 14]
    [3, 4, 9, 14]
    [1, 2, 4, 9, 14]
    [1, 7, 8, 14]
    [2, 6, 8, 14]
    [3, 5, 8, 14]
    [1, 2, 5, 8, 14]
    [1, 3, 4, 8, 14]
    [3, 6, 7, 14]
    [1, 2, 6, 7, 14]
    [4, 5, 7, 14]
    [1, 3, 5, 7, 14]
    [2, 3, 4, 7, 14]
    [1, 4, 5, 6, 14]
    [2, 3, 5, 6, 14]
    [1, 2, 3, 4, 6, 14]
    [5, 12, 13]
    [1, 4, 12, 13]
    [2, 3, 12, 13]
    [6, 11, 13]
    [1, 5, 11, 13]
    [2, 4, 11, 13]
    [1, 2, 3, 11, 13]
    [7, 10, 13]
    [1, 6, 10, 13]
    [2, 5, 10, 13]
    [3, 4, 10, 13]
    [1, 2, 4, 10, 13]
    [8, 9, 13]
    [1, 7, 9, 13]
    [2, 6, 9, 13]
    [3, 5, 9, 13]
    [1, 2, 5, 9, 13]
    [1, 3, 4, 9, 13]
    [2, 7, 8, 13]
    [3, 6, 8, 13]
    [1, 2, 6, 8, 13]
    [4, 5, 8, 13]
    [1, 3, 5, 8, 13]
    [2, 3, 4, 8, 13]
    [4, 6, 7, 13]
    [1, 3, 6, 7, 13]
    [1, 4, 5, 7, 13]
    [2, 3, 5, 7, 13]
    [1, 2, 3, 4, 7, 13]
    [2, 4, 5, 6, 13]
    [1, 2, 3, 5, 6, 13]
    [7, 11, 12]
    [1, 6, 11, 12]
    [2, 5, 11, 12]
    [3, 4, 11, 12]
    [1, 2, 4, 11, 12]
    [8, 10, 12]
    [1, 7, 10, 12]
    [2, 6, 10, 12]
    [3, 5, 10, 12]
    [1, 2, 5, 10, 12]
    [1, 3, 4, 10, 12]
    [1, 8, 9, 12]
    [2, 7, 9, 12]
    [3, 6, 9, 12]
    [1, 2, 6, 9, 12]
    [4, 5, 9, 12]
    [1, 3, 5, 9, 12]
    [2, 3, 4, 9, 12]
    [3, 7, 8, 12]
    [1, 2, 7, 8, 12]
    [4, 6, 8, 12]
    [1, 3, 6, 8, 12]
    [1, 4, 5, 8, 12]
    [2, 3, 5, 8, 12]
    [1, 2, 3, 4, 8, 12]
    [5, 6, 7, 12]
    [1, 4, 6, 7, 12]
    [2, 3, 6, 7, 12]
    [2, 4, 5, 7, 12]
    [1, 2, 3, 5, 7, 12]
    [3, 4, 5, 6, 12]
    [1, 2, 4, 5, 6, 12]
    [9, 10, 11]
    [1, 8, 10, 11]
    [2, 7, 10, 11]
    [3, 6, 10, 11]
    [1, 2, 6, 10, 11]
    [4, 5, 10, 11]
    [1, 3, 5, 10, 11]
    [2, 3, 4, 10, 11]
    [2, 8, 9, 11]
    [3, 7, 9, 11]
    [1, 2, 7, 9, 11]
    [4, 6, 9, 11]
    [1, 3, 6, 9, 11]
    [1, 4, 5, 9, 11]
    [2, 3, 5, 9, 11]
    [1, 2, 3, 4, 9, 11]
    [4, 7, 8, 11]
    [1, 3, 7, 8, 11]
    [5, 6, 8, 11]
    [1, 4, 6, 8, 11]
    [2, 3, 6, 8, 11]
    [2, 4, 5, 8, 11]
    [1, 2, 3, 5, 8, 11]
    [1, 5, 6, 7, 11]
    [2, 4, 6, 7, 11]
    [1, 2, 3, 6, 7, 11]
    [3, 4, 5, 7, 11]
    [1, 2, 4, 5, 7, 11]
    [1, 3, 4, 5, 6, 11]
    [3, 8, 9, 10]
    [1, 2, 8, 9, 10]
    [4, 7, 9, 10]
    [1, 3, 7, 9, 10]
    [5, 6, 9, 10]
    [1, 4, 6, 9, 10]
    [2, 3, 6, 9, 10]
    [2, 4, 5, 9, 10]
    [1, 2, 3, 5, 9, 10]
    [5, 7, 8, 10]
    [1, 4, 7, 8, 10]
    [2, 3, 7, 8, 10]
    [1, 5, 6, 8, 10]
    [2, 4, 6, 8, 10]
    [1, 2, 3, 6, 8, 10]
    [3, 4, 5, 8, 10]
    [1, 2, 4, 5, 8, 10]
    [2, 5, 6, 7, 10]
    [3, 4, 6, 7, 10]
    [1, 2, 4, 6, 7, 10]
    [1, 3, 4, 5, 7, 10]
    [2, 3, 4, 5, 6, 10]
    [6, 7, 8, 9]
    [1, 5, 7, 8, 9]
    [2, 4, 7, 8, 9]
    [1, 2, 3, 7, 8, 9]
    [2, 5, 6, 8, 9]
    [3, 4, 6, 8, 9]
    [1, 2, 4, 6, 8, 9]
    [1, 3, 4, 5, 8, 9]
    [3, 5, 6, 7, 9]
    [1, 2, 5, 6, 7, 9]
    [1, 3, 4, 6, 7, 9]
    [2, 3, 4, 5, 7, 9]
    [1, 2, 3, 4, 5, 6, 9]
    [4, 5, 6, 7, 8]
    [1, 3, 5, 6, 7, 8]
    [2, 3, 4, 6, 7, 8]
    [1, 2, 3, 4, 5, 7, 8]
      

  16.   


    牛B,大哥, 这么多for循环亏你想得出来,第一个否定你
      

  17.   

    两个for循环轻松搞定
    代码如下:
    public class TestDemo3 {
         public static void main(String[] args) {
                   for (int i = 1; i <= 20; i++) {
                         for (int j =1; j <= 20; j++) {
                               if (i + j==30) {
                                  System.out.println(i + "+" +j +" =30");
                                }
                           }
                        }
         }
    }
    已执行通过
      

  18.   

    ………………只有两个数的情况,显然不符合要求………………额,对于上面的略作改动import java.util.ArrayList;
    import java.util.Iterator;public class test {
    public static void main(String[] args) {
    ArrayList<ArrayList<Integer>> list = getArrays(1, 20, 30);
    Iterator<ArrayList<Integer>> iter = list.iterator();
    while (iter.hasNext())
    System.out.println(iter.next());
    } public static int sum(int min, int max) {
    if (min > max)
    return 0;
    // int sum = max;
    // for (int i = min; i < max; i++)
    // sum += i;
    // return sum;
    return (min + max) * (max - min + 1) / 2;
    } public static ArrayList<ArrayList<Integer>> getArrays(int min, int max, int sum) {
    if (max > sum)
    max = sum;
    if (sum(min, max) < sum)
    return null;
    ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
    for (int i = max; i >= min; i--) {
    ArrayList<Integer> array;
    if (i == sum) {
    array = new ArrayList<Integer>();
    array.add(i);
    list.add(array);
    }
    if (i < sum) {
    ArrayList<ArrayList<Integer>> temp = getArrays(min, i - 1, sum - i);
    if (temp == null)
    continue;
    Iterator<ArrayList<Integer>> iter = temp.iterator();
    while (iter.hasNext()) {
    array = iter.next();
    array.add(i);
    list.add(array);
    }
    }
    }
    return list;
    }
    }
      

  19.   

    嗯,回溯解法~~~public class NumQuestion1 {
    static int len = 20; // 输入长度.
    static int sum = 30; // 和.
    static int[] data = new int[20]; // 数据.
    static boolean[] output = new boolean[20];
    static int count = 1;
    static {
    for (int i = 0; i < 20; ) {
    output[i] = false;
    data[i] = i++ + 1;
    }
    } static int Get(int s, int k, int r) {
    output[k] = true;
    if (s + data[k] == sum) {
    PrintResult();
    } else if (s + data[k] + data[k + 1] <= sum) {
    Get(s + data[k], k + 1, r - data[k]);
    }
    if (s + r - data[k] >= sum && s + data[k + 1] <= sum) {
    output[k] = false;
    Get(s, k + 1, r - data[k]);
    }
    output[k] = false;
    return 0;
    } static void PrintResult() {
    int i;
    System.out.print("\r\n所求子集" + count++ + "为: ");
    for (i = 0; i < len; i++) {
    if (output[i]) {
    System.out.print(data[i] + " ");
    }
    }
    } public static void main(String[] args) {
    int s = 0;
    int k = 0;
    int sumAll = 210;
    Get(s, k, sumAll);
    }
    }
      

  20.   

    public class ee {
        public static void main(String args[]){
           int count=0;
            for(int i=1;i<=20;i++){
                for(int j=1;j<=20;j++){
                    if(i+j==30)
                    { System.out.println(i+"+"+j+"=30");
                    count++;}
                }
            }
           System.out.println("count="+count);
        }
    }
      

  21.   

    不一定是两个啊,不然我一个循环就搞定了public class NumQuestion1 {    static void Get()
        {        
            int count=0;
            int j=0;
            for (int i = 20; i > j; i--) {
                j=30-i;
                System.out.println(i+"+"+j+"=30");
                ++count;
            }
            System.out.println("count="+count);
        }
        public static void main(String[] args) {
            Get();
        }
    }
    /*Output:
     *20+10=30
     *19+11=30
     *18+12=30
     *17+13=30
     *16+14=30
     *15+15=30
     *count=6
    */
      

  22.   


    static LinkedList<Integer> list = new LinkedList<Integer>(); public static void split(int n,int base){
    if(n == 0){
    System.out.println(list);
    return;
    }
    for(int i = base + 1;i <= n & i < 30;i++){
    list.addLast(i);
    split(n - i,i);
    list.removeLast(); 
    }
    } public static void main(String[] args){
    split(30,0);
    }
      

  23.   


    忘了小于20的数了
    for(int i= base+1;i<= n & i<20;i++)
      

  24.   

    这个貌似很简洁,能写个注释吗 对list不是很懂,还是问下递归与回溯是一个意思吗,谢谢!
      

  25.   

    import java.util.Vector;
    public class search
    {
    Vector<Integer> vec=new Vector<Integer>();
    int[] num=new int[21];
    int count=0;
    search()
    {
    for(int i=0;i<21;i++)
    {
    num[i]=i;
    }
    }
    void result(int n,int x,int sub)
    {
    if((sub-num[x])==0)
    {
    vec.add(num[x]);
    System.out.println(output());
    count++;
    vec.remove(vec.size()-1);
    }
    if(x<n&&(sub-num[x])>0)
    {
    result(n,x+1,sub);
    vec.add(num[x]);
    result(n,x+1,sub-num[x]);
    vec.remove(vec.size()-1);
    }
    if(x==n)
    {
    return;
    }
    }
    public static void main(String[] args)
    {
    search s=new search();
    s.result(20,1,30);
    System.out.println("表达式总数:"+s.count);
    }
    String output()
    {
    String string="";
    int size=vec.size();
    for(int i=0;i<size;i++)
    {
    string=string+vec.get(i)+"+";
    }
    string=count+":"+string.substring(0,(string.length()-1))+"=30";
    return string;
    }
    }
      

  26.   


    hmmm,我觉得既然只对队尾进行操作,应该也用Stack比较符合容器本身的用途吧
      

  27.   

    汗 这是我看到题目就想到的一个解法而已,也就是针对题目,7个for怎么啦  对于这个题目,上面的虽然也有好的解法,但如果针对这个题目的可取么。还有不要以为写出牛逼的代码来就很牛逼。初学者看的懂不懂是一回事,你自己看的都累吧,7个for怎么啦  只要看了我的大家都明白什么道理。
    当然如果数字庞大,我也当然不会用这样的方法
      

  28.   

    public class Test {
    public static void main(String[] args) { int num = 0;
    int count = 0;
    String s = "";
    for (int i = 1; i < 1024 * 1024; i++) {
    for (int j = 0; j < 20; j++) {
    int k = (i >> j) & 1;
    if (k == 1) {
    num = num + k * (j + 1);
    if (s.equals("")) {
    s = s + (j + 1);
    } else {
    s = s + "+" + (j + 1);
    }
    }
    }
    if (num == 30) {
    count++;
    System.out.println(count + ": 30=" + s);
    }
    num = 0;
    s = "";
    }
    }
    }这个够简单了吧!
      

  29.   

    用了回溯算法很好很强大~~~学习了
    对Java很熟练,充分运用了数据的特征是1~20连续的特征,如果不连续会麻烦点~~~学习了
      

  30.   

    楼主,60楼方法挫了点,但结果是正确的!import java.util.Vector;
    public class search
    {
    Vector<Integer> vec=new Vector<Integer>();
    int[] num=new int[21];
    int count=0;
    search()
    {
    for(int i=0;i<21;i++)
    {
    num[i]=i;
    }
    }
    void result(int n,int x,int sub)
    {
    if((sub-num[x])==0)
    {
    vec.add(num[x]);
    System.out.println(output());
    count++;
    vec.remove(vec.size()-1);
    }
    if(x<n&&(sub-num[x])>0)
    {
    result(n,x+1,sub);
    vec.add(num[x]);
    result(n,x+1,sub-num[x]);
    vec.remove(vec.size()-1);
    }
    if(x==n)
    {
    return;
    }
    }
    public static void main(String[] args)
    {
    search s=new search();
    s.result(20,1,30);
    System.out.println("表达式总数:"+s.count);
    }
    String output()
    {
    String string="";
    int size=vec.size();
    for(int i=0;i<size;i++)
    {
    string=string+vec.get(i)+"+";
    }
    string=count+":"+string.substring(0,(string.length()-1))+"=30";
    return string;
    }
    }
      

  31.   

    我是67楼的,这个不需要什么注释吧,使用的都是基本的命令,连import都不需要。就是用int的数字的二进制位来模拟1到20的存在与否,遍历所有可能,并判断和是否为30。(i>>j)&1就是那个位置上是0还是1,而1到20就类似于各位上的权值。
      

  32.   

    热闹一下。//将正整数n折成若干个不同的正整数之和。
    import java.util.*;
    public class TestNumber {
        static List<Integer> stack=new ArrayList<Integer>();//栈
    static void get(int n,int k)//对整数n进行拆分,从k开始拆.拆分的元素不重复。
    {
    for(int i=k;i<n/2;i++)//只要一半
    {
    stack.add(i);//i进栈
    System.out.println(stackToString()+"+"+(n-i));//输出一个拆分
    get(n-i,i+1);//递归,对剩下的n-i继续拆分
    stack.remove(stack.size()-1);//i出栈
    }
    }

    //将栈中的一个拆分在屏幕打印出来
    private static String stackToString()
    {
    if(stack.size()<=0) return "";
    StringBuilder sb=new StringBuilder();
    sb.append(stack.get(0));
    for(int i=1;i<stack.size();i++)
    {
    sb.append("+"+stack.get(i));
    }
    return sb.toString();
    }

    public static void main(String[] args) {
    System.out.println("30的拆分有:");
                get(30,1);
    }}
    运行结果:
    30的拆分有:
    1+29
    1+2+27
    1+2+3+24
    1+2+3+4+20
    1+2+3+4+5+15
    1+2+3+4+5+6+9
    1+2+3+4+6+14
    1+2+3+4+7+13
    1+2+3+4+8+12
    1+2+3+4+9+11
    1+2+3+5+19
    1+2+3+5+6+13
    1+2+3+5+7+12
    1+2+3+5+8+11
    1+2+3+6+18
    1+2+3+6+7+11
    1+2+3+6+8+10
    1+2+3+7+17
    1+2+3+8+16
    1+2+3+9+15
    1+2+3+10+14
    1+2+3+11+13
    1+2+4+23
    1+2+4+5+18
    1+2+4+5+6+12
    1+2+4+5+7+11
    1+2+4+5+8+10
    1+2+4+6+17
    1+2+4+6+7+10
    1+2+4+7+16
    1+2+4+8+15
    1+2+4+9+14
    1+2+4+10+13
    1+2+5+22
    1+2+5+6+16
    1+2+5+6+7+9
    1+2+5+7+15
    1+2+5+8+14
    1+2+5+9+13
    1+2+5+10+12
    1+2+6+21
    1+2+6+7+14
    1+2+6+8+13
    1+2+6+9+12
    1+2+7+20
    1+2+7+8+12
    1+2+7+9+11
    1+2+8+19
    1+2+9+18
    1+2+10+17
    1+2+11+16
    1+2+12+15
    1+3+26
    1+3+4+22
    1+3+4+5+17
    1+3+4+5+6+11
    1+3+4+5+7+10
    1+3+4+6+16
    1+3+4+6+7+9
    1+3+4+7+15
    1+3+4+8+14
    1+3+4+9+13
    1+3+4+10+12
    1+3+5+21
    1+3+5+6+15
    1+3+5+7+14
    1+3+5+8+13
    1+3+5+9+12
    1+3+6+20
    1+3+6+7+13
    1+3+6+8+12
    1+3+6+9+11
    1+3+7+19
    1+3+7+8+11
    1+3+8+18
    1+3+9+17
    1+3+10+16
    1+3+11+15
    1+3+12+14
    1+4+25
    1+4+5+20
    1+4+5+6+14
    1+4+5+7+13
    1+4+5+8+12
    1+4+5+9+11
    1+4+6+19
    1+4+6+7+12
    1+4+6+8+11
    1+4+7+18
    1+4+7+8+10
    1+4+8+17
    1+4+9+16
    1+4+10+15
    1+4+11+14
    1+5+24
    1+5+6+18
    1+5+6+7+11
    1+5+6+8+10
    1+5+7+17
    1+5+8+16
    1+5+9+15
    1+5+10+14
    1+5+11+13
    1+6+23
    1+6+7+16
    1+6+8+15
    1+6+9+14
    1+6+10+13
    1+7+22
    1+7+8+14
    1+7+9+13
    1+7+10+12
    1+8+21
    1+8+9+12
    1+9+20
    1+10+19
    1+11+18
    1+12+17
    1+13+16
    2+28
    2+3+25
    2+3+4+21
    2+3+4+5+16
    2+3+4+5+6+10
    2+3+4+5+7+9
    2+3+4+6+15
    2+3+4+7+14
    2+3+4+8+13
    2+3+4+9+12
    2+3+5+20
    2+3+5+6+14
    2+3+5+7+13
    2+3+5+8+12
    2+3+5+9+11
    2+3+6+19
    2+3+6+7+12
    2+3+6+8+11
    2+3+7+18
    2+3+7+8+10
    2+3+8+17
    2+3+9+16
    2+3+10+15
    2+3+11+14
    2+4+24
    2+4+5+19
    2+4+5+6+13
    2+4+5+7+12
    2+4+5+8+11
    2+4+6+18
    2+4+6+7+11
    2+4+6+8+10
    2+4+7+17
    2+4+8+16
    2+4+9+15
    2+4+10+14
    2+4+11+13
    2+5+23
    2+5+6+17
    2+5+6+7+10
    2+5+7+16
    2+5+8+15
    2+5+9+14
    2+5+10+13
    2+6+22
    2+6+7+15
    2+6+8+14
    2+6+9+13
    2+6+10+12
    2+7+21
    2+7+8+13
    2+7+9+12
    2+8+20
    2+8+9+11
    2+9+19
    2+10+18
    2+11+17
    2+12+16
    2+13+15
    3+27
    3+4+23
    3+4+5+18
    3+4+5+6+12
    3+4+5+7+11
    3+4+5+8+10
    3+4+6+17
    3+4+6+7+10
    3+4+7+16
    3+4+8+15
    3+4+9+14
    3+4+10+13
    3+5+22
    3+5+6+16
    3+5+6+7+9
    3+5+7+15
    3+5+8+14
    3+5+9+13
    3+5+10+12
    3+6+21
    3+6+7+14
    3+6+8+13
    3+6+9+12
    3+7+20
    3+7+8+12
    3+7+9+11
    3+8+19
    3+9+18
    3+10+17
    3+11+16
    3+12+15
    4+26
    4+5+21
    4+5+6+15
    4+5+7+14
    4+5+8+13
    4+5+9+12
    4+6+20
    4+6+7+13
    4+6+8+12
    4+6+9+11
    4+7+19
    4+7+8+11
    4+8+18
    4+9+17
    4+10+16
    4+11+15
    4+12+14
    5+25
    5+6+19
    5+6+7+12
    5+6+8+11
    5+7+18
    5+7+8+10
    5+8+17
    5+9+16
    5+10+15
    5+11+14
    6+24
    6+7+17
    6+8+16
    6+9+15
    6+10+14
    6+11+13
    7+23
    7+8+15
    7+9+14
    7+10+13
    8+22
    8+9+13
    8+10+12
    9+21
    10+20
    11+19
    12+18
    13+17
    14+16
      

  33.   

    小弟愚顿,二进制是知道的,但是逻辑上还是不太懂,数学不好啊... i>>j意思不是将j右移i位吗?
    还有为什么i<1024*1024啊,s j k又都代表什么,我真是太弱了,我就懂基本语法,一循环就晕了,这个算是递归吗?我是不是太笨了,我现在看的是谭浩强主编的<<JAVA程序设计>>,看的茫茫然...哎,我该怎么办啊,真想去死- -
      

  34.   

    改进了一下,满足题意。凑热闹而已//将正整数n折成若干个不同的正整数之和。 import java.util.*;
    public class TestNumber {

        static List<Integer> stack=new ArrayList<Integer>();//栈
        
    //  对整数n进行拆分,从k开始拆.拆分的元素不重复。每一个数都不超过min
    static void get(int n,int k,int min) {
    for(int i=k;i<n/2&&i<min;i++)//只要一半
    {
    stack.add(i);//i进栈
    if(n-i<=min)//找到一个拆分
      System.out.println(stackToString()+"+"+(n-i));//输出一个拆分
    get(n-i,i+1,min);//递归,对剩下的n-i继续拆分,但从i+1开始
    stack.remove(stack.size()-1);//i出栈
    }
    }

    //将栈中的一个拆分在屏幕打印出来
    private static String stackToString()
    {
    if(stack.size()<=0) return "";
    StringBuilder sb=new StringBuilder();
    sb.append(stack.get(0));
    for(int i=1;i<stack.size();i++)
    {
    sb.append("+"+stack.get(i));
    }
    return sb.toString();
    }

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println("30的拆分,每一个数不超过20的有:");
                get(30,1,20);//30的拆分,每一个数不超过20
    }}
    运行结果
    1-20中拆分和为30的拆分有:
    1+2+3+4+20
    1+2+3+4+5+15
    1+2+3+4+5+6+9
    1+2+3+4+6+14
    1+2+3+4+7+13
    1+2+3+4+8+12
    1+2+3+4+9+11
    1+2+3+5+19
    1+2+3+5+6+13
    1+2+3+5+7+12
    1+2+3+5+8+11
    1+2+3+6+18
    1+2+3+6+7+11
    1+2+3+6+8+10
    1+2+3+7+17
    1+2+3+8+16
    1+2+3+9+15
    1+2+3+10+14
    1+2+3+11+13
    1+2+4+5+18
    1+2+4+5+6+12
    1+2+4+5+7+11
    1+2+4+5+8+10
    1+2+4+6+17
    1+2+4+6+7+10
    1+2+4+7+16
    1+2+4+8+15
    1+2+4+9+14
    1+2+4+10+13
    1+2+5+6+16
    1+2+5+6+7+9
    1+2+5+7+15
    1+2+5+8+14
    1+2+5+9+13
    1+2+5+10+12
    1+2+6+7+14
    1+2+6+8+13
    1+2+6+9+12
    1+2+7+20
    1+2+7+8+12
    1+2+7+9+11
    1+2+8+19
    1+2+9+18
    1+2+10+17
    1+2+11+16
    1+2+12+15
    1+3+4+5+17
    1+3+4+5+6+11
    1+3+4+5+7+10
    1+3+4+6+16
    1+3+4+6+7+9
    1+3+4+7+15
    1+3+4+8+14
    1+3+4+9+13
    1+3+4+10+12
    1+3+5+6+15
    1+3+5+7+14
    1+3+5+8+13
    1+3+5+9+12
    1+3+6+20
    1+3+6+7+13
    1+3+6+8+12
    1+3+6+9+11
    1+3+7+19
    1+3+7+8+11
    1+3+8+18
    1+3+9+17
    1+3+10+16
    1+3+11+15
    1+3+12+14
    1+4+5+20
    1+4+5+6+14
    1+4+5+7+13
    1+4+5+8+12
    1+4+5+9+11
    1+4+6+19
    1+4+6+7+12
    1+4+6+8+11
    1+4+7+18
    1+4+7+8+10
    1+4+8+17
    1+4+9+16
    1+4+10+15
    1+4+11+14
    1+5+6+18
    1+5+6+7+11
    1+5+6+8+10
    1+5+7+17
    1+5+8+16
    1+5+9+15
    1+5+10+14
    1+5+11+13
    1+6+7+16
    1+6+8+15
    1+6+9+14
    1+6+10+13
    1+7+8+14
    1+7+9+13
    1+7+10+12
    1+8+9+12
    1+9+20
    1+10+19
    1+11+18
    1+12+17
    1+13+16
    2+3+4+5+16
    2+3+4+5+6+10
    2+3+4+5+7+9
    2+3+4+6+15
    2+3+4+7+14
    2+3+4+8+13
    2+3+4+9+12
    2+3+5+20
    2+3+5+6+14
    2+3+5+7+13
    2+3+5+8+12
    2+3+5+9+11
    2+3+6+19
    2+3+6+7+12
    2+3+6+8+11
    2+3+7+18
    2+3+7+8+10
    2+3+8+17
    2+3+9+16
    2+3+10+15
    2+3+11+14
    2+4+5+19
    2+4+5+6+13
    2+4+5+7+12
    2+4+5+8+11
    2+4+6+18
    2+4+6+7+11
    2+4+6+8+10
    2+4+7+17
    2+4+8+16
    2+4+9+15
    2+4+10+14
    2+4+11+13
    2+5+6+17
    2+5+6+7+10
    2+5+7+16
    2+5+8+15
    2+5+9+14
    2+5+10+13
    2+6+7+15
    2+6+8+14
    2+6+9+13
    2+6+10+12
    2+7+8+13
    2+7+9+12
    2+8+20
    2+8+9+11
    2+9+19
    2+10+18
    2+11+17
    2+12+16
    2+13+15
    3+4+5+18
    3+4+5+6+12
    3+4+5+7+11
    3+4+5+8+10
    3+4+6+17
    3+4+6+7+10
    3+4+7+16
    3+4+8+15
    3+4+9+14
    3+4+10+13
    3+5+6+16
    3+5+6+7+9
    3+5+7+15
    3+5+8+14
    3+5+9+13
    3+5+10+12
    3+6+7+14
    3+6+8+13
    3+6+9+12
    3+7+20
    3+7+8+12
    3+7+9+11
    3+8+19
    3+9+18
    3+10+17
    3+11+16
    3+12+15
    4+5+6+15
    4+5+7+14
    4+5+8+13
    4+5+9+12
    4+6+20
    4+6+7+13
    4+6+8+12
    4+6+9+11
    4+7+19
    4+7+8+11
    4+8+18
    4+9+17
    4+10+16
    4+11+15
    4+12+14
    5+6+19
    5+6+7+12
    5+6+8+11
    5+7+18
    5+7+8+10
    5+8+17
    5+9+16
    5+10+15
    5+11+14
    6+7+17
    6+8+16
    6+9+15
    6+10+14
    6+11+13
    7+8+15
    7+9+14
    7+10+13
    8+9+13
    8+10+12
    10+20
    11+19
    12+18
    13+17
    14+16
      

  35.   

    对于#24的用bit来表示判断的算法比较感兴趣,于是为这个写个注释
        public static void main(String[] args){
            // 初始赋值,数组第n项值为n+1,主要用于后面求和与添加如列表用
            int[] a=new int[20];
            for(int i=0,len=a.length;i<len;i++){
                a[i]=i+1;
            }
            
            int sum,mask;    //sum是一组数的和,mask表示……额,怎么说呢,就是怎么样表示这一位被选中
            boolean isSelect;    //判断某位是否被选中的变量
            List<Integer> nums=new ArrayList<Integer>();    //记录这一组数的列表
            
            /*1<<a.length,这里a.length为20,就是左移20位(其实应该再减1的)
              这样最大的2进制数就是100000000000000000000(减1的话就是20个1,这个才应该是正确的起始条件啊)
              最小是1,每次递减
            */
            for(int loopCount=1<<a.length;loopCount>0;loopCount--){
                sum=0;    //初始和为0
                mask=0x01;    //0x1就可以,或者说1也可以,某位为1表示被选中
                nums.clear();    //清空list,避免上次留下的数据产生干扰
                
                //一共20位,也就是循环20次(再次说明前面过的loopCount初始应该减1)
                for(int i=0,len=a.length;i<len;i++){
                    //判断第i+1位是否被选中,我们认为最低位为第1位
                    isSelect=(loopCount&mask)>0;
                    //如果被选中了,那么添加到列表并且和增加
                    if(isSelect){
                        sum+=a[i];
                        nums.add(a[i]);
                    }
                    //mask左移一位
                    mask<<=1;
                }            
                
                //如果和为30,那么就是满足条件的,打印列表
                if(sum==30) System.out.println(nums);            
            }
        }67楼方法类似
      

  36.   

    67楼虽然解法简单,但不具有可扩展性
    55楼给split在增加个参数就具有无限可扩展性能了
      

  37.   

    哥给个好的你
    public class Test { /**
     * @param args
     */
    private static int count=0;
    public static void main(String[] args) {
    dame(20,1,30);
    System.out.println(count);
    }
    public static void dame(int max,int temp,int value){
    if(temp>=max){
    return;
    }
    if(temp+max<value){
    dame(max,temp+1,value);
    return;
    }
    for(int i=temp;i<=max;i++){
    count++;
    if(i+temp==value){
    System.out.println(i+"+"+temp+"="+value);
    }
    }
    dame(max,temp+1,value);
    }}
      

  38.   

    import java.util.LinkedList;
    public class thirtyCount{
    static LinkedList <Integer> list=new LinkedList <Integer>();
    static int count = 0;
    public static void split(int n,int base){
    if(n==0){
    count++;
    System.out.println(count+"+30="+list);
    return;
    }

    for(int i= base+1;i <= n& i <30;i++){
    list.addLast(i);
    thirtyCount.split(n-i,i);//回溯递归
    list.removeLast();

    } } public static void main(String[] args){
    split(30,0);
    }
    }
    帮55楼打印下:最后一行:295+30=[14, 16]
      

  39.   

    88楼的输出count=65,只打印了5组出来……应该是263组
      

  40.   

    static LinkedList<Integer> list = new LinkedList<Integer>(); public static void split(int n, int max, int base) {
    if (n == 0) {
    System.out.println(list);
    return;
    }
    for (int i = base + 1; i <= n & i <= max; i++) {
    list.addLast(i);
    split(n - i, max, i);
    list.removeLast();
    }
    } public static void main(String[] args) {
    split(30, 20, 0);
    }估计是这样吧