递归拆分,一个数,拆成两个,两个拆成3个一直到无法拆分 import java.util.Arrays;public class SumTest { /* * 假定结果集合为一个从小到大排列的数组,初始化为一个元素,将元素拆分,最终形成若干个数组,使每个数组的和等于原数组的值 */ public static void sum(int min, int[] result) { int sum = result[result.length - 1]; for (int i = min; i <= sum - i; i++) { if (sum - i >= i) { int[] temp = new int[result.length + 1]; System.arraycopy(result, 0, temp, 0, result.length - 1); temp[result.length - 1] = i; temp[result.length] = sum - i; System.out.println(Arrays.toString(temp)); sum(i, temp); } } } public static void main(String args[]) { SumTest.sum(1, new int[] { 20 }); } }
import java.util.ArrayList;public class LouTi { public static void main(String[] args) { paoLouTi(new ArrayList<Integer>(), 1, 20); } public static void paoLouTi(ArrayList a, int i, int t) { //和到了t就打印出来,多了不符合,少了继续找 if (t == 0) System.out.println(a); //这里函数分开来两个分支,一个是加原来一样多,一种是比原来多加了1 else if (i <= t) { a.add(i); paoLouTi(a, i, t - i); a.remove(a.size() - 1); paoLouTi(a, i + 1, t); } } }
我的该了下import java.util.ArrayList;public class LouTi { public static void main(String[] args) { paoLouTi(new ArrayList<Integer>(), 1, 20); } public static void paoLouTi(ArrayList a, int i, int t) { //和到了t就打印出来,多了不符合,少了继续找 if (t == 0) System.out.println(a); //这里函数分开来两个分支,一个是加原来一样多,一种是比原来多加了1 else if (i <= t) { if(a!=null&&a.contains(i)) paoLouTi(a, i + 1 ,t); else{ a.add(i); paoLouTi(a, i, t - i); a.remove(a.size() - 1); paoLouTi(a, i + 1 ,t); } } }
package zjz.test;public class Test04 { static int maxLength = 19 ; static int array[] ; static{ array = new int[maxLength]; for (int i = 1; i <= maxLength ; i++) { array[i - 1] = i ; } } static void combination(int array[] , int len , int exceptResult){ combination(array , new int[len] , 0 , 0 , exceptResult); } static void combination(int array[] , int tmp[] , int arrayIndex , int tmpIndex , int exceptResult ){ if(tmpIndex == tmp.length){ int result = calcResult(tmp , tmpIndex); if(result == exceptResult){ printArray(tmp , tmpIndex); } return ; } for (int i = arrayIndex; i < array.length; i++) { tmp[tmpIndex] = array[i] ; combination(array , tmp , i + 1 , tmpIndex + 1 , exceptResult); } } static int calcResult(int array[] , int index){ int sum = 0 ; for (int i = 0; i < index ; i++) { sum += array[i]; } return sum ; } static void printArray(int[] array , int index){ for (int i = 0; i < index ; i++) { System.out.print(array[i] + " "); } System.out.println(); } public static void main(String[] args) { combination(array , 3 , 20); }} 1 2 17 1 3 16 1 4 15 1 5 14 1 6 13 1 7 12 1 8 11 1 9 10 2 3 15 2 4 14 2 5 13 2 6 12 2 7 11 2 8 10 3 4 13 3 5 12 3 6 11 3 7 10 3 8 9 4 5 11 4 6 10 4 7 9 5 6 9 5 7 8
public class CNumber { private static int count=0; public static void main(String args[]) throws Exception{ for(int i=1;i<20;i++){ getResultNumber(""+i,i,i); } }
void test_xxxxx(int a, int c_sum, int lay,int g_sum); int array_result[10] = {0};int main() { int sum = 20, lay = 0; test_xxxxx(1, 0, lay, sum); return 0; }void test_xxxxx(int a, int c_sum, int lay,int g_sum) { if(c_sum > g_sum) { /* do nothing */ } else if(c_sum == g_sum) { for(int i = 0;i < lay; i++) printf("%d ", array_result[i]); printf("\n"); } else { for(int j = a; j< g_sum; j++) { array_result[lay] = j; test_xxxxx(j+1, c_sum + j, lay + 1, g_sum); } } }
def all_comb(n, m={1:set([(1,)]), 2:set([(2,)])}): return m.get(n, set()) or [[[ [m.setdefault(n, set([(n,)])), m[n].add(tuple(sorted(e+(x,)))),m[n]] [2] for e in all_comb(n-x, m) if len(set(e+(x,)))==len(e+(x,))] for x in range(1,(n+1)/2)], m[n]][1] print "\n".join( "+".join([str(n) for n in x]) for x in sorted(list(all_comb(20))) ) 无聊的Python爱好者路过…………
原来元素不能重复呀,看错题目了,修改了一下现在可以保证不重复了。 public static void partition(int min, int max, int[] result) { int sum = result[result.length - 1];// 待拆分数字 for (int i = min; i <= max; i++) { if (sum - i > i) { int[] temp = new int[result.length + 1]; System.arraycopy(result, 0, temp, 0, result.length - 1); temp[result.length - 1] = i; temp[result.length] = sum - i; System.out.println(Arrays.toString(temp));// 输出当前符合要求的队列 partition(i + 1, sum - i - 1, temp); } } } public static void main(String args[]) { SumTest.partition(1, 19, new int[] { 20 }); }
按自己思路写的(看成只有两个数相加,后面的数可递归拆分,且加数从小到大排序避免重复)public class Digui { private static int sum=20;
public static void main(String[] args) { for(int i=1;i<=sum/2;i++){ int m = sum-i; if (m>i){ System.out.println(i+"+"+m+"=20"); getStr(i,sum-i); } } } public static void getStr(int a,int b){ for (int i=a+1;i<=b/2;i++){ int m = b-i; if (m>i){ System.out.println(a+"+"+i+"+"+m+"=20"); getStr(i,m); } } } }
改进一下 public class Digui { private static int sum=20;
public static void main(String[] args) { getStr(0,sum); } public static void getStr(int a,int b){ for (int i=a+1;i<=b/2;i++){ int m = b-i; if (m>i){ if(a==0) System.out.println(i+"+"+m+"=20"); else System.out.println(a+"+"+i+"+"+m+"=20"); getStr(i,m); } } } }
不会递归,所以没用递归 private static List<string> list = new List<string>(); // 这是结果 private int max = 19; private int a1 = 1, a2 = 1, a3 = 1, a4 = 1, a5 = 1; public void reset() { if (a5 == max) if (a4 != max) { a4++; a5 = 1; } if (a4 == max) if (a3 != max) { a3++; a4 = 1; } if (a3 == max) if (a2 != max) { a2++; a3 = 1; } if (a2 == max) if (a1 != max) { a2 = 1; a1++; } } public bool getTrue(int i) { if (i == 0) { if (a1 != a2 && a1 != a3 && a1 != a4 && a1 != a5 && a2 != a3 && a2 != a4 && a2 != a5 && a3 != a4 && a3 != a5 && a4 != a5) return true; else return false; } else if (i == 1) { if (a1 != a2 && a1 != a3 && a1 != a4 && a2 != a3 && a2 != a4 && a3 != a4) return true; else return false; } else if (i == 2) { if (a1 != a2 && a1 != a3 && a2 != a3) return true; else return false; } else { if (a1 != a2) return true; else return false; } } public string intArr(int[] arr) { string str = ""; arr = arr.OrderBy(x => x).ToArray(); for (int i = 0; i < arr.Length; i++) { str += arr[i] + "+"; } return str.Substring(0, str.Length - 1) + "=20"; } public void main() { for (int i = 0; i < 4; i++) { a1 = 1; a2 = 1; a3 = 1; a4 = 1; a5 = 1; for (; true; ) { switch (i) { case 0: a5++; if (a1 + a2 + a3 + a4 + a5 == 20) if (getTrue(i)) { int[] str = { a1, a2, a3, a4, a5 }; string tmpString = intArr(str); if (!list.Contains(tmpString)) list.Add(tmpString); } break; case 1: a4++; if (a1 + a2 + a3 + a4 == 20) if (getTrue(i)) { int[] str = { a1, a2, a3, a4 }; string tmpString = intArr(str); if (!list.Contains(tmpString)) list.Add(tmpString); } break; case 2: a3++; if (a1 + a2 + a3 == 20) if (getTrue(i)) { int[] str = { a1, a2, a3 }; string tmpString = intArr(str); if (!list.Contains(tmpString)) list.Add(tmpString); } break; case 3: a2++; if (a1 + a2 == 20) if (getTrue(i)) { int[] str = { a1, a2 }; string tmpString = intArr(str); if (!list.Contains(tmpString)) list.Add(tmpString); } break; } if (i == 0) { if (a1 == max && a2 == max && a3 == max && a4 == max && a5 == max) break; } else if (i == 1) { if (a1 == max && a2 == max && a3 == max && a4 == max) break; } else if (i == 2) { if (a1 == max && a2 == max && a3 == max) break; } else if (i == 3) { if (a1 == max && a2 == max) break; } reset(); } } }
for (int i = 1; i <= 19; i++) { for (int j = i; j <= 19; j++) { int num = i + j; if (num == 20) { System.out.println(i + " , " + j); } } }
import java.util.Arrays;public class SumTest {
/*
* 假定结果集合为一个从小到大排列的数组,初始化为一个元素,将元素拆分,最终形成若干个数组,使每个数组的和等于原数组的值
*/
public static void sum(int min, int[] result) {
int sum = result[result.length - 1];
for (int i = min; i <= sum - i; i++) {
if (sum - i >= i) {
int[] temp = new int[result.length + 1];
System.arraycopy(result, 0, temp, 0, result.length - 1);
temp[result.length - 1] = i;
temp[result.length] = sum - i;
System.out.println(Arrays.toString(temp));
sum(i, temp); } }
} public static void main(String args[]) {
SumTest.sum(1, new int[] { 20 });
}
}
public static void main(String[] args) {
paoLouTi(new ArrayList<Integer>(), 1, 20);
} public static void paoLouTi(ArrayList a, int i, int t) {
//和到了t就打印出来,多了不符合,少了继续找
if (t == 0)
System.out.println(a);
//这里函数分开来两个分支,一个是加原来一样多,一种是比原来多加了1
else if (i <= t) {
a.add(i);
paoLouTi(a, i, t - i);
a.remove(a.size() - 1);
paoLouTi(a, i + 1, t); } }
}
ArrayList<String> al=new ArrayList<String>();
for(int i=1;i<=10;i++){
for(int j=1;j<20;j++){
if(i+j==20){
al.add(i+","+j);
}
}
}
package zjz.test;public class Test03 { static int maxLength = 19 ;
static int array[] ;
static{
array = new int[maxLength];
for (int i = 1; i <= maxLength ; i++) {
array[i - 1] = i ;
}
} static void combination(int array[] , int exceptResult){
combination(array , new int[array.length] , 0 , 0 , exceptResult);
} static void combination(int array[] , int tmp[] , int arrayIndex , int tmpIndex , int exceptResult ){
if(tmpIndex == array.length){
return ;
}
for (int i = arrayIndex; i < array.length; i++) {
tmp[tmpIndex] = array[i] ;
int result = calcResult(tmp , tmpIndex);
if(result == exceptResult){
printArray(tmp , tmpIndex);
}else if(result > exceptResult){
return ;
}
combination(array , tmp , i + 1 , tmpIndex + 1 , exceptResult);
}
} static int calcResult(int array[] , int index){
int sum = 0 ;
for (int i = 0; i <= index ; i++) {
sum += array[i];
}
return sum ;
} static void printArray(int[] array , int index){
for (int i = 0; i <= index ; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
} public static void main(String[] args) {
combination(array , 20);
}}Console1 2 3 4 10
1 2 3 5 9
1 2 3 6 8
1 2 3 14
1 2 4 5 8
1 2 4 6 7
1 2 4 13
1 2 5 12
1 2 6 11
1 2 7 10
1 2 8 9
1 2 17
1 3 4 5 7
1 3 4 12
1 3 5 11
1 3 6 10
1 3 7 9
1 3 16
1 4 5 10
1 4 6 9
1 4 7 8
1 4 15
1 5 6 8
1 5 14
1 6 13
1 7 12
1 8 11
1 9 10
1 19
2 3 4 5 6
2 3 4 11
2 3 5 10
2 3 6 9
2 3 7 8
2 3 15
2 4 5 9
2 4 6 8
2 4 14
2 5 6 7
2 5 13
2 6 12
2 7 11
2 8 10
2 18
3 4 5 8
3 4 6 7
3 4 13
3 5 12
3 6 11
3 7 10
3 8 9
3 17
4 5 11
4 6 10
4 7 9
4 16
5 6 9
5 7 8
5 15
6 14
7 13
8 12
9 11
发现问题了for (int j = i + 1; j < max; j++) {把i+1改成i就对了for (int j = i; j < max; j++) {
最后两行代码修改一下
for(int j=i+1;j<max;j++){
if("".equals(str)){
getStr(str+i,tempSum+i,j);
}else{
getStr(str+"+"+i,tempSum+i,j);
}
}
递归部分中 j=i+1 所以下面的循环代码中用j就OK
public static void main(String[] args) {
paoLouTi(new ArrayList<Integer>(), 1, 20);
} public static void paoLouTi(ArrayList a, int i, int t) {
//和到了t就打印出来,多了不符合,少了继续找
if (t == 0)
System.out.println(a);
//这里函数分开来两个分支,一个是加原来一样多,一种是比原来多加了1
else if (i <= t) {
if(a!=null&&a.contains(i))
paoLouTi(a, i + 1 ,t);
else{
a.add(i);
paoLouTi(a, i, t - i);
a.remove(a.size() - 1);
paoLouTi(a, i + 1 ,t);
}
} }
[1, 2, 3, 4, 10]
[1, 2, 3, 5, 9]
[1, 2, 3, 6, 8]
[1, 2, 3, 14]
[1, 2, 4, 5, 8]
[1, 2, 4, 6, 7]
[1, 2, 4, 13]
[1, 2, 5, 12]
[1, 2, 6, 11]
[1, 2, 7, 10]
[1, 2, 8, 9]
[1, 2, 17]
[1, 3, 4, 5, 7]
[1, 3, 4, 12]
[1, 3, 5, 11]
[1, 3, 6, 10]
[1, 3, 7, 9]
[1, 3, 16]
[1, 4, 5, 10]
[1, 4, 6, 9]
[1, 4, 7, 8]
[1, 4, 15]
[1, 5, 6, 8]
[1, 5, 14]
[1, 6, 13]
[1, 7, 12]
[1, 8, 11]
[1, 9, 10]
[1, 19]
[2, 3, 4, 5, 6]
[2, 3, 4, 11]
[2, 3, 5, 10]
[2, 3, 6, 9]
[2, 3, 7, 8]
[2, 3, 15]
[2, 4, 5, 9]
[2, 4, 6, 8]
[2, 4, 14]
[2, 5, 6, 7]
[2, 5, 13]
[2, 6, 12]
[2, 7, 11]
[2, 8, 10]
[2, 18]
[3, 4, 5, 8]
[3, 4, 6, 7]
[3, 4, 13]
[3, 5, 12]
[3, 6, 11]
[3, 7, 10]
[3, 8, 9]
[3, 17]
[4, 5, 11]
[4, 6, 10]
[4, 7, 9]
[4, 16]
[5, 6, 9]
[5, 7, 8]
[5, 15]
[6, 14]
[7, 13]
[8, 12]
[9, 11]
[20]
public static int sum() {
int sum=0;
for(int a=1; a<=19; a++){
for(int b=a+1; b<=19; b++){
if(a+b==20 && a!=b){
sum++;
System.out.println("2个:"+a+","+b);
}
for(int c=b+1; c<=19; c++){
if(a+b+c==20 && a!=b && a!=c && b!=c){
sum++;
System.out.println("3个:"+a+","+b+","+c);
}
for(int d=c+1; d<=19; d++){
if(a+b+c+d==20 && a!=b && a!=c && a!=d && b!=c && b!=d && c!=d){
sum++;
System.out.println("4个:"+a+","+b+","+c+","+d);
}
for(int e=d+1; e<=19; e++){
if(a+b+c+d+e == 20 && a!=b && a!=c && a!=d && a!=e &&b!=c && b!=d && b!=e && c!=d && c!=e && d!=e){
sum++;
System.out.println("5个:"+a+","+b+","+c+","+d+","+e);
}
}
}
}
}
}
return sum; //返回总个数
}
int sum=0;
for(int a=1; a<=19; a++){
for(int b=a+1; b<=19; b++){
if(a+b==20){
sum++;
System.out.println("2"+a+","+b);
}
for(int c=b+1; c<=19; c++){
if(a+b+c==20){
sum++;
System.out.println("3"+a+","+b+","+c);
}
for(int d=c+1; d<=19; d++){
if(a+b+c+d==20){
sum++;
System.out.println("4"+a+","+b+","+c+","+d);
}
for(int e=d+1; e<=19; e++){
if(a+b+c+d+e == 20 ){
sum++;
System.out.println("5"+a+","+b+","+c+","+d+","+e);
}
}
}
}
}
}
return sum; //
}
static int array[] ;
static{
array = new int[maxLength];
for (int i = 1; i <= maxLength ; i++) {
array[i - 1] = i ;
}
} static void combination(int array[] , int len , int exceptResult){
combination(array , new int[len] , 0 , 0 , exceptResult);
} static void combination(int array[] , int tmp[] , int arrayIndex , int tmpIndex , int exceptResult ){
if(tmpIndex == tmp.length){
int result = calcResult(tmp , tmpIndex);
if(result == exceptResult){
printArray(tmp , tmpIndex);
}
return ;
}
for (int i = arrayIndex; i < array.length; i++) {
tmp[tmpIndex] = array[i] ;
combination(array , tmp , i + 1 , tmpIndex + 1 , exceptResult);
}
} static int calcResult(int array[] , int index){
int sum = 0 ;
for (int i = 0; i < index ; i++) {
sum += array[i];
}
return sum ;
} static void printArray(int[] array , int index){
for (int i = 0; i < index ; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
} public static void main(String[] args) {
combination(array , 3 , 20);
}}
1 2 17
1 3 16
1 4 15
1 5 14
1 6 13
1 7 12
1 8 11
1 9 10
2 3 15
2 4 14
2 5 13
2 6 12
2 7 11
2 8 10
3 4 13
3 5 12
3 6 11
3 7 10
3 8 9
4 5 11
4 6 10
4 7 9
5 6 9
5 7 8
public static void main(String args[]) throws Exception{
for(int i=1;i<20;i++){
getResultNumber(""+i,i,i);
}
}
public static void getResultNumber(String resultStr,int number,int result){
if(result==20){
System.out.println(resultStr);
}else if(result<20){
for(int j=number+1;j<20;j++){
getResultNumber(resultStr+"+"+j,j,result+j);
}
}
}
}
int array_result[10] = {0};int main()
{
int sum = 20, lay = 0; test_xxxxx(1, 0, lay, sum); return 0;
}void test_xxxxx(int a, int c_sum, int lay,int g_sum)
{
if(c_sum > g_sum)
{
/* do nothing */
}
else if(c_sum == g_sum)
{
for(int i = 0;i < lay; i++)
printf("%d ", array_result[i]);
printf("\n");
}
else
{
for(int j = a; j< g_sum; j++)
{
array_result[lay] = j;
test_xxxxx(j+1, c_sum + j, lay + 1, g_sum);
}
}
}
def all_comb(n, m={1:set([(1,)]), 2:set([(2,)])}):
return m.get(n, set()) or [[[ [m.setdefault(n, set([(n,)])), m[n].add(tuple(sorted(e+(x,)))),m[n]] [2] for e in all_comb(n-x, m) if len(set(e+(x,)))==len(e+(x,))] for x in range(1,(n+1)/2)], m[n]][1]
print "\n".join( "+".join([str(n) for n in x]) for x in sorted(list(all_comb(20))) )
无聊的Python爱好者路过…………
int sum = result[result.length - 1];// 待拆分数字
for (int i = min; i <= max; i++) {
if (sum - i > i) {
int[] temp = new int[result.length + 1];
System.arraycopy(result, 0, temp, 0, result.length - 1);
temp[result.length - 1] = i;
temp[result.length] = sum - i;
System.out.println(Arrays.toString(temp));// 输出当前符合要求的队列
partition(i + 1, sum - i - 1, temp);
} }
} public static void main(String args[]) {
SumTest.partition(1, 19, new int[] { 20 });
}
return;
}这个return,return到哪里去了?为什么tempSum又变成3了
import java.util.Set;public class Test { public static void main(String[] args) {
getResult(1, 19, 20);
} static void getResult(int start, int end, int sum) {
// <算式,算式的和>
HashMap<String, Integer> map = new HashMap<String, Integer>();
for (int i = start; i <= end; ++i) {
Set<String> set = map.keySet();
for (Object key : set.toArray()) {
int result = map.get(key);
// 如果当前算式的和加上a[i]小于sum,那么将新的算式加入map
if (result + i < sum) {
map.put(key + "+" + i, result + i);
continue;
// 等于就输出结果
} else if (result + i == sum) {
System.out.printf("%s+%d=%d\n", key, i, sum);
}
// 无论大于等于都将算式移除
map.remove(key);
}
// 将新数字加入map
if (i < sum / 2) {
map.put(String.valueOf(i), i);
// 如果新数字恰好是sum就输出,然后中断循环,因为后面不可能再有结果了
} else if(i == sum) {
System.out.printf("%d=%d\n", i, sum);
break;
}
}
}
}
public class Test1 {
public static void main(String[] args)
{
int count = 0;
for(int j=1;j<=20;j++)
{
if(j==20)
{
count++;
System.out.println("count++:"+count+" "+j);
}
}
for(int j=1;j<=19;j++)
{
for(int x=j+1;x<=20;x++)
{
if(j+x==20)
{
count++;
System.out.println("count++:"+count+" "+j+" "+x);
break;
}
}
}
for(int j=1;j<=18;j++)
{
for(int x=j+1;x<=19;x++)
{
for(int y=x+1;y<=20;y++)
{
if(j+x+y==20)
{
count++;
System.out.println("count++:"+count+" "+j+" "+x+" "+y);
break;
}
}
}
}
for(int j=1;j<=17;j++)
{
for(int x=j+1;x<=18;x++)
{
for(int y=x+1;y<=19;y++)
{
for(int z=y+1;z<=20;z++)
{
if(j+x+y+z==20)
{
count++;
System.out.println("count++:"+count+" "+j+" "+x+" "+y+" "+z);
break;
}
}
}
}
}
for(int j=1;j<=16;j++)
{
for(int x=j+1;x<=17;x++)
{
for(int y=x+1;y<=18;y++)
{
for(int z=y+1;z<=19;z++)
{
for(int w=z+1;w<=20;w++)
{
if(j+x+y+z+w==20)
{
count++;
System.out.println("count++:"+count+" "+j+" "+x+" "+y+" "+z+" "+w);
break;
}
}
}
}
}
}
System.out.println("Count Last::"+count);
}
}
count++:1 20
count++:2 1 19
count++:3 2 18
count++:4 3 17
count++:5 4 16
count++:6 5 15
count++:7 6 14
count++:8 7 13
count++:9 8 12
count++:10 9 11
count++:11 1 2 17
count++:12 1 3 16
count++:13 1 4 15
count++:14 1 5 14
count++:15 1 6 13
count++:16 1 7 12
count++:17 1 8 11
count++:18 1 9 10
count++:19 2 3 15
count++:20 2 4 14
count++:21 2 5 13
count++:22 2 6 12
count++:23 2 7 11
count++:24 2 8 10
count++:25 3 4 13
count++:26 3 5 12
count++:27 3 6 11
count++:28 3 7 10
count++:29 3 8 9
count++:30 4 5 11
count++:31 4 6 10
count++:32 4 7 9
count++:33 5 6 9
count++:34 5 7 8
count++:35 1 2 3 14
count++:36 1 2 4 13
count++:37 1 2 5 12
count++:38 1 2 6 11
count++:39 1 2 7 10
count++:40 1 2 8 9
count++:41 1 3 4 12
count++:42 1 3 5 11
count++:43 1 3 6 10
count++:44 1 3 7 9
count++:45 1 4 5 10
count++:46 1 4 6 9
count++:47 1 4 7 8
count++:48 1 5 6 8
count++:49 2 3 4 11
count++:50 2 3 5 10
count++:51 2 3 6 9
count++:52 2 3 7 8
count++:53 2 4 5 9
count++:54 2 4 6 8
count++:55 2 5 6 7
count++:56 3 4 5 8
count++:57 3 4 6 7
count++:58 1 2 3 4 10
count++:59 1 2 3 5 9
count++:60 1 2 3 6 8
count++:61 1 2 4 5 8
count++:62 1 2 4 6 7
count++:63 1 3 4 5 7
count++:64 2 3 4 5 6
Count Last::64
public static void main(String[] args) throws Exception {
calc(20, 19, "");
} private static void calc(int targetValue, int current, String content) {
if (current <= 0)
return;
if (current == targetValue) {
System.out.println(content + targetValue);
calc(targetValue, current - 1, content);
} else if (current < targetValue) {
calc(targetValue - current, current - 1, content + current + ",");
calc(targetValue, current - 1, content);
} else if (current > targetValue) {
calc(targetValue, current - 1, content);
}
}
19,1
18,2
17,3
17,2,1
16,4
16,3,1
15,5
15,4,1
15,3,2
14,6
14,5,1
14,4,2
14,3,2,1
13,7
13,6,1
13,5,2
13,4,3
13,4,2,1
12,8
12,7,1
12,6,2
12,5,3
12,5,2,1
12,4,3,1
11,9
11,8,1
11,7,2
11,6,3
11,6,2,1
11,5,4
11,5,3,1
11,4,3,2
10,9,1
10,8,2
10,7,3
10,7,2,1
10,6,4
10,6,3,1
10,5,4,1
10,5,3,2
10,4,3,2,1
9,8,3
9,8,2,1
9,7,4
9,7,3,1
9,6,5
9,6,4,1
9,6,3,2
9,5,4,2
9,5,3,2,1
8,7,5
8,7,4,1
8,7,3,2
8,6,5,1
8,6,4,2
8,6,3,2,1
8,5,4,3
8,5,4,2,1
7,6,5,2
7,6,4,3
7,6,4,2,1
7,5,4,3,1
6,5,4,3,2
public static void main(String[] args) throws Exception {
calc(20, 19, "");
} private static void calc(int targetValue, int current, String content) {
if (current <= 0)
return;
if (current == targetValue) {
System.out.println(content + targetValue);
} else if (current < targetValue) {
calc(targetValue - current, current - 1, content + current + ",");
}
calc(targetValue, current - 1, content);
}这样更短,不过理解更恼火
max-1;i++){
getStr("",0,i);
}
10个循环就ok吧
for(int i=1;i<
10;i++){
getStr("",0,i);
}
private static int sum=20;
public static void main(String[] args) {
for(int i=1;i<=sum/2;i++){
int m = sum-i;
if (m>i){
System.out.println(i+"+"+m+"=20");
getStr(i,sum-i);
}
}
}
public static void getStr(int a,int b){
for (int i=a+1;i<=b/2;i++){
int m = b-i;
if (m>i){
System.out.println(a+"+"+i+"+"+m+"=20");
getStr(i,m);
}
}
}
}
public class Digui {
private static int sum=20;
public static void main(String[] args) {
getStr(0,sum);
}
public static void getStr(int a,int b){
for (int i=a+1;i<=b/2;i++){
int m = b-i;
if (m>i){
if(a==0)
System.out.println(i+"+"+m+"=20");
else
System.out.println(a+"+"+i+"+"+m+"=20");
getStr(i,m);
}
}
}
}
private static List<string> list = new List<string>(); // 这是结果 private int max = 19; private int a1 = 1, a2 = 1, a3 = 1, a4 = 1, a5 = 1; public void reset()
{
if (a5 == max)
if (a4 != max)
{
a4++;
a5 = 1;
}
if (a4 == max)
if (a3 != max)
{
a3++;
a4 = 1;
}
if (a3 == max)
if (a2 != max)
{
a2++;
a3 = 1;
}
if (a2 == max)
if (a1 != max)
{
a2 = 1;
a1++;
}
} public bool getTrue(int i)
{
if (i == 0)
{
if (a1 != a2 && a1 != a3 && a1 != a4 && a1 != a5 && a2 != a3 && a2 != a4 && a2 != a5 && a3 != a4 && a3 != a5 && a4 != a5)
return true;
else
return false;
}
else if (i == 1)
{
if (a1 != a2 && a1 != a3 && a1 != a4 && a2 != a3 && a2 != a4 && a3 != a4)
return true;
else
return false;
}
else if (i == 2)
{
if (a1 != a2 && a1 != a3 && a2 != a3)
return true;
else
return false;
}
else
{
if (a1 != a2)
return true;
else
return false;
}
} public string intArr(int[] arr)
{
string str = "";
arr = arr.OrderBy(x => x).ToArray(); for (int i = 0; i < arr.Length; i++)
{
str += arr[i] + "+";
} return str.Substring(0, str.Length - 1) + "=20";
} public void main()
{
for (int i = 0; i < 4; i++)
{
a1 = 1; a2 = 1; a3 = 1; a4 = 1; a5 = 1; for (; true; )
{
switch (i)
{
case 0:
a5++;
if (a1 + a2 + a3 + a4 + a5 == 20)
if (getTrue(i))
{
int[] str = { a1, a2, a3, a4, a5 };
string tmpString = intArr(str);
if (!list.Contains(tmpString))
list.Add(tmpString);
} break;
case 1:
a4++;
if (a1 + a2 + a3 + a4 == 20)
if (getTrue(i))
{
int[] str = { a1, a2, a3, a4 };
string tmpString = intArr(str);
if (!list.Contains(tmpString))
list.Add(tmpString);
}
break;
case 2:
a3++;
if (a1 + a2 + a3 == 20)
if (getTrue(i))
{
int[] str = { a1, a2, a3 };
string tmpString = intArr(str);
if (!list.Contains(tmpString))
list.Add(tmpString);
}
break;
case 3:
a2++;
if (a1 + a2 == 20)
if (getTrue(i))
{
int[] str = { a1, a2 };
string tmpString = intArr(str);
if (!list.Contains(tmpString))
list.Add(tmpString);
}
break;
} if (i == 0)
{
if (a1 == max && a2 == max && a3 == max && a4 == max && a5 == max)
break;
}
else if (i == 1)
{
if (a1 == max && a2 == max && a3 == max && a4 == max)
break;
}
else if (i == 2)
{
if (a1 == max && a2 == max && a3 == max)
break;
}
else if (i == 3)
{
if (a1 == max && a2 == max)
break;
} reset();
}
}
}
{
for (int j = i; j <= 19; j++)
{
int num = i + j;
if (num == 20)
{
System.out.println(i + " , " + j);
}
}
}
public class Count20 {
static int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17, 18, 19 };
public static void main(String[] args) {
Count20 t = new Count20();
t.test(0, 19, "", 20);
}
/**
* @param num 有19个数
* @param sum 求和为20
*/
public void test(int index, int num , String str,int sum) {
for(int i=index;i<num;i++){
for(int j=i+1;j<num;j++){
if( array[i]+array[j]==sum ){
System.out.println(str+array[i]+","+array[j]);
test(i+1,j,str+array[i]+",",array[j]);
}
}
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
int[] arr = new int[19];
for(int i=0; i<19; ) {
arr[i] = ++i;
}
cal(0 , 20 , arr);
}
private static void cal(int startIndex , int leftWeight , int[] arr) {
if(startIndex == arr.length || leftWeight < 0) {
return ;
}
for(int i=startIndex ; i<arr.length ; ++i) {
//默认数组递增
if(leftWeight < arr[i]) {
return ;
} else if (arr[i] == leftWeight) {
System.out.println(answer+" ,"+arr[i]);
return ;
} else {
answer.add(arr[i]);
cal(i+1 , leftWeight-arr[i] , arr);
answer.remove(answer.size()-1);
}
}
}
方法getStr2为最初优化版,方法getStr3为最终版package test;import java.util.Formatter;public class TestSum { private static int max = 19;
private static int sum = 20; private static int i1 = 0;
private static int i2 = 0;
private static int i3 = 0; public static void main(String[] args) {
long time0 = System.nanoTime();
for (int i = 1; i < max - 1; i++) {
getStr1("", 0, i);
}
long time1 = System.nanoTime();
for (int i = 1; i < max - 1; i++) {
getStr2("", 0, i);
}
long time2 = System.nanoTime();
for (int i = 1; i < max - 1; i++) {
getStr3("", 0, i);
}
long time3 = System.nanoTime();
Formatter f = new Formatter(System.out);
f.format("%-10s %9s %15s\n", "Item", "[Cost time]", "[Loop amount]");
f.format("%-10s %9s %15s\n", "----", "-----------", "--------------");
f.format("%-10s %9s %12s\n", "Method1:", time1 - time0, i1);
f.format("%-10s %9s %12s\n", "Method2:", time2 - time1, i2);
f.format("%-10s %9s %12s\n", "Method3:", time3 - time2, i3);
} public static void getStr1(String str, int tempSum, int i) {
i1++;
if (i > max) {
return;
}
if (tempSum + i == sum) {
// System.out.println(str + "+" + i + "=" + sum);
} else if (tempSum + i > sum) {
return;
} else {
for (int j = i + 1; j < max; j++) {
if ("".equals(str)) {
getStr1(str + i, tempSum + i, j);
} else {
getStr1(str + "+" + i, tempSum + i, j);
}
}
}
} public static boolean getStr2(String str, int tempSum, int i) {
i2++;
if (i > max) {
return true;
}
if (tempSum + i == sum) {
// System.out.println(str + "+" + i + "=" + sum);
} else if (tempSum + i < sum) {
for (int j = i + 1; j < max; j++) {
if ("".equals(str)) {
if (getStr2(str + i, tempSum + i, j)) {
break;
}
} else {
if (getStr2(str + "+" + i, tempSum + i, j)) {
break;
}
}
}
return false;
}
return true;
} public static boolean getStr3(String str, int tempSum, int i) {
i3++;
if (i > max) {
return true;
}
if (tempSum + i == sum) {
// System.out.println(str + i + "=" + sum);
} else if (tempSum + i < sum) {
for (int j = i + 1; j < max; j++) {
if (getStr3(str + i + "+", tempSum + i, j)) {
break;
}
}
return false;
}
return true;
}
}
Item [Cost time] [Loop amount]
---- ----------- --------------
Method1: 3854596 2765
Method2: 202856 606
Method3: 185114 606
package test;import java.util.Formatter;public class TestSum { private static int max = 19;
private static int sum = 20; private static int i1 = 0;
private static int i2 = 0;
private static int i3 = 0; public static void main(String[] args) {
long time0 = System.nanoTime();
for (int i = 1; i < max - 1; i++) {
getStr1("", 0, i);
}
long time1 = System.nanoTime();
for (int i = 1; i < max - 1; i++) {
getStr2("", 0, i);
}
long time2 = System.nanoTime();
for (int i = 1; i < max - 1; i++) {
getStr3("", 0, i);
}
long time3 = System.nanoTime();
Formatter f = new Formatter(System.out);
f.format("%-10s %9s %15s\n", "Item", "[Cost time]", "[Loop amount]");
f.format("%-10s %9s %15s\n", "----", "-----------", "--------------");
f.format("%-10s %9s %12s\n", "Method1:", time1 - time0, i1);
f.format("%-10s %9s %12s\n", "Method2:", time2 - time1, i2);
f.format("%-10s %9s %12s\n", "Method3:", time3 - time2, i3);
} public static void getStr1(String str, int tempSum, int i) {
i1++;
if (i > max) {
return;
}
if (tempSum + i == sum) {
// System.out.println(str + "+" + i + "=" + sum);
} else if (tempSum + i > sum) {
return;
} else {
for (int j = i + 1; j < max; j++) {
if ("".equals(str)) {
getStr1(str + i, tempSum + i, j);
} else {
getStr1(str + "+" + i, tempSum + i, j);
}
}
}
} public static boolean getStr2(String str, int tempSum, int i) {
i2++;
if (i > max) {
return true;
}
if (tempSum + i == sum) {
// System.out.println(str + "+" + i + "=" + sum);
} else if (tempSum + i < sum) {
for (int j = i + 1; j < max; j++) {
if ("".equals(str)) {
if (getStr2(str + i, tempSum + i, j)) {
break;
}
} else {
if (getStr2(str + "+" + i, tempSum + i, j)) {
break;
}
}
}
return false;
}
return true;
} public static boolean getStr3(String str, int tempSum, int i) {
i3++;
if (i > max) {
return true;
}
if (tempSum + i == sum) {
// System.out.println(str + i + "=" + sum);
} else if (tempSum + i < sum) {
for (int j = i + 1; j < max; j++) {
if (getStr3(str + i + "+", tempSum + i, j)) {
break;
}
}
return false;
}
return true;
}
}
上面的代码多应该把j+1改成j
private static int sum = 20;
private static int max = 20;
public static void main(String[] args) {
for(int a = 1; a < max-a; a++) {
Fenge(a,max-a,"");
}
}
public static void Fenge (int i, int sumTemp, String result) {
if(i>=sumTemp || sumTemp >= sum) {
sum = 20;
return;
}
if(i+sumTemp == sum) {
result = result +" " + i +" ";
System.out.print(result+sumTemp);
System.out.println("");
sumTemp = sumTemp - (i + 1);
i = i+1;
sum = i + sumTemp;
Fenge(i,sumTemp,result);
}
}
}
这个简单1 19
1 2 17
1 2 3 14
1 2 3 4 10
2 18
2 3 15
2 3 4 11
2 3 4 5 6
3 17
3 4 13
3 4 5 8
4 16
4 5 11
5 15
5 6 9
6 14
7 13
8 12
9 11
public static void main(String[] args) {
ut.println("go2");
for (int i = 1; i < 20; i++) {
test(i, 20, "");
}
}
for (int i = 1; i < 20; i++) {
test(i, 20, "");
}
} public static void test(int t, int he, String str) {
if ((he - t) > t) {
int result = he - t;
str += "-" + t;
System.out.println(str + "-" + result);
for (int i = t; i < 10; i++) {
test(i, result, str);
}
}
}
var max = 20; //最大数
var r = 20; //和/*
递归求和
@param i - 前一次迭代和
@parma l - 前一个参与计算数字
@param str - 前一次迭代参与计算数字字符串
*/
function sum(i,l,str){
for(var j=l+1;j<max;j++){
var c = i+j;
if(c == r){
a.push(str + ',' + j);
}else if(c<r){
sum(c,j,str + ',' +j);
}else{
break;
}
}
}//主程序
function test(){
for(var i=1;i<max;i++){
sum(i,i,i);
}
document.write('满足条件的数共:' + a.length + '组, 如下表:<br>');
document.write(a.join('<br>'));
}test();
输出结果:
满足条件的数共:63组, 如下表:
1,2,3,4,10
1,2,3,5,9
1,2,3,6,8
1,2,3,14
1,2,4,5,8
1,2,4,6,7
1,2,4,13
1,2,5,12
1,2,6,11
1,2,7,10
1,2,8,9
1,2,17
1,3,4,5,7
1,3,4,12
1,3,5,11
1,3,6,10
1,3,7,9
1,3,16
1,4,5,10
1,4,6,9
1,4,7,8
1,4,15
1,5,6,8
1,5,14
1,6,13
1,7,12
1,8,11
1,9,10
1,19
2,3,4,5,6
2,3,4,11
2,3,5,10
2,3,6,9
2,3,7,8
2,3,15
2,4,5,9
2,4,6,8
2,4,14
2,5,6,7
2,5,13
2,6,12
2,7,11
2,8,10
2,18
3,4,5,8
3,4,6,7
3,4,13
3,5,12
3,6,11
3,7,10
3,8,9
3,17
4,5,11
4,6,10
4,7,9
4,16
5,6,9
5,7,8
5,15
6,14
7,13
8,12
9,11
static void Main(string[] args)
{
List<string> arr = getTheResult(1,20);
foreach (string s in arr)
{
Console.WriteLine(s);
}
Console.ReadKey();
}
private static List<string> getTheResult(int startIndex,int number)
{
List<string> lis = new List<string>();
for (int i = startIndex; i < number; i++)
{
if (i < number - i)
{
lis.Add(i.ToString() + "+" + (number - i).ToString());
List<string> temp = getTheResult(i + 1, number - i);
if (temp.Count > 0)
{
for(int k=0;k<temp.Count;k++)
{
lis.Add(i.ToString() + "+" + temp[k].ToString());
}
}
}
}
return lis;
}
public static int count = 0;
public static void sum(int min, int[] result) {
int sum = result[result.length - 1];
for (int i = min; i < sum - i; i++) {
int[] temp = new int[result.length + 1];
System.arraycopy(result, 0, temp, 0, result.length - 1);
temp[result.length - 1] = i;
temp[result.length] = sum - i;
System.out.println(Arrays.toString(temp));
count++;
sum(i+1, temp);
}
} public static void main(String[] args) {
sum(1, new int[] { 20 });
System.out.println("总数:"+count);
}
}
//结果
[1, 19]
[1, 2, 17]
[1, 2, 3, 14]
[1, 2, 3, 4, 10]
[1, 2, 3, 5, 9]
[1, 2, 3, 6, 8]
[1, 2, 4, 13]
[1, 2, 4, 5, 8]
[1, 2, 4, 6, 7]
[1, 2, 5, 12]
[1, 2, 6, 11]
[1, 2, 7, 10]
[1, 2, 8, 9]
[1, 3, 16]
[1, 3, 4, 12]
[1, 3, 4, 5, 7]
[1, 3, 5, 11]
[1, 3, 6, 10]
[1, 3, 7, 9]
[1, 4, 15]
[1, 4, 5, 10]
[1, 4, 6, 9]
[1, 4, 7, 8]
[1, 5, 14]
[1, 5, 6, 8]
[1, 6, 13]
[1, 7, 12]
[1, 8, 11]
[1, 9, 10]
[2, 18]
[2, 3, 15]
[2, 3, 4, 11]
[2, 3, 4, 5, 6]
[2, 3, 5, 10]
[2, 3, 6, 9]
[2, 3, 7, 8]
[2, 4, 14]
[2, 4, 5, 9]
[2, 4, 6, 8]
[2, 5, 13]
[2, 5, 6, 7]
[2, 6, 12]
[2, 7, 11]
[2, 8, 10]
[3, 17]
[3, 4, 13]
[3, 4, 5, 8]
[3, 4, 6, 7]
[3, 5, 12]
[3, 6, 11]
[3, 7, 10]
[3, 8, 9]
[4, 16]
[4, 5, 11]
[4, 6, 10]
[4, 7, 9]
[5, 15]
[5, 6, 9]
[5, 7, 8]
[6, 14]
[7, 13]
[8, 12]
[9, 11]
总数:63
http://xieyan30.iteye.com/admin/blogs/1814117
{
for (int i =1; i < 10; i++) {
getNext(i+"" ,i) ;
}
}
public static void getNext(String s ,int i){
for (int j = i+1; j < 19; j++) {
if(i + j < 20 )
getNext(s+"+"+j,i + j) ;
else if(i + j == 20)
System.out.println(s + "+"+j +" ="+20);
else
return ;
}
}
1,2,3,5,9
1,2,3,6,8
1,2,3,14
1,2,4,5,8
1,2,4,6,7
1,2,4,13
1,2,5,12
1,2,6,11
1,2,7,10
1,2,8,9
1,2,17
1,3,4,5,7
1,3,4,12
1,3,5,11
1,3,6,10
1,3,7,9
1,3,16
1,4,5,10
1,4,6,9
1,4,7,8
1,4,15
1,5,6,8
1,5,14
1,6,13
1,7,12
1,8,11
1,9,10
1,19
2,3,4,5,6
2,3,4,11
2,3,5,10
2,3,6,9
2,3,7,8
2,3,15
2,4,5,9
2,4,6,8
2,4,14
2,5,6,7
2,5,13
2,6,12
2,7,11
2,8,10
2,18
3,4,5,8
3,4,6,7
3,4,13
3,5,12
3,6,11
3,7,10
3,8,9
3,17
4,5,11
4,6,10
4,7,9
4,16
5,6,9
5,7,8
5,15
6,14
7,13
8,12
9,11
Total count is 63
public static void main(String[] args) {
int temp = 0;
int b = 0;
for(int i = 1; i < 20; i++) {
temp += i;
if(temp >= 20){
b = i-1;
break;
}
}
//计算b的值求出最大组合位数创建数组用
sum(b);
} public static void sum(int index) {
for (int i = 1; i < 20/2+1; i++) {
for (int t = i + 1; t < 20; t++) {
if (i + t == 20) {
//打印2位数组合为20的
print(i,t);
int[] result = new int[index];
result[0] = i;
saveSum(result, i, t, 0);
}
}
}
} public static void saveSum(int[] result, int i, int t, int index) {
for (int b = i + 1; b < t / 2 + 1; b++) {
for (int c = i + 2; c < t; c++) {
if (b + c == t) {
prints(result,b,c,index);
if (c - b < 2) {
return;
}
else {
result[index+1] = b;
//递归
saveSum(result,b,c,index+1);
}
}
}
}
}
private static void print(int i, int t) {
System.out.println(" "+i + " " + t);
} private static void prints(int[] result, int b, int c,int index) {
if(b == c) {
return;
}
String temp = "";
for(int i =0; i <= index; i ++) {
temp =temp+" "+ result[i];
}
temp = temp+" "+ b + " " + c;
System.out.println(temp);
}}
输出结果
1 19
1 2 17
1 2 3 14
1 2 3 4 10
1 2 3 5 9
1 2 3 6 8
1 2 4 13
1 2 4 5 8
1 2 4 6 7
1 2 5 12
1 2 6 11
1 2 7 10
1 2 8 9
1 3 16
1 3 4 12
1 3 4 5 7
1 3 5 11
1 3 6 10
1 3 7 9
1 4 15
1 4 5 10
1 4 6 9
1 4 7 8
1 5 14
1 5 6 8
1 6 13
1 7 12
1 8 11
1 9 10
2 18
2 3 15
2 3 4 11
2 3 4 5 6
2 3 5 10
2 3 6 9
2 3 7 8
2 4 14
2 4 5 9
2 4 6 8
2 5 13
2 5 6 7
2 6 12
2 7 11
2 8 10
3 17
3 4 13
3 4 5 8
3 4 6 7
3 5 12
3 6 11
3 7 10
3 8 9
4 16
4 5 11
4 6 10
4 7 9
5 15
5 6 9
5 7 8
6 14
7 13
8 12
9 11
没注意看,是有点错,修改如下
public class Digui {
//1~19这19个数字,任意多个数相加等于20的所有组合,不重复
public static void main(String[] args) {
getStr(0,20,"");
}
//b为两个数的和
public static void getStr(int a,int b,String str){
for (int i=a+1,j=b-i;i<(b+1)/2 && i<j;i++,j--){
if (str.equals("")){
System.out.println(i+"+"+j+"=20");
getStr(i,j,String.valueOf(i));
}else{
System.out.println(str+"+"+i+"+"+j+"=20");
getStr(i,j,str+"+"+String.valueOf(i));
}
}
}
}