/** * @author PLA * 输入两个整数n 和m,从数列1,2,3.......n 中随意取几个数, * 使其和等于m ,要求将其中所有的可能组合列出来. */ public static void main(String[] args) { int m=27,n=20; comb(m,n); } public static void comb(int m,int n){ int start,end,sum; int[] temp = new int[n+1]; for(int i=1;i<=n;i++){ temp[i] = i; } if(m<=1) System.out.println("Error"); if(m<=n) end = m-1; else end = n; start = 0; while(end>start){ sum =temp[start]+temp[end]; if(sum <m) start++; if(sum>m) end--; if(sum == m){ System.out.println(temp[start]+"+"+temp[end]+"="+m); start++; end--; } } }