# 从数列1, 2, 3, ... , n 中 随意取几个数,使其和等于sum. [No. 24]

2364人阅读 评论(2)

3  5 或者 1  5  2 或者 1  4  3

findSum(sum, n) = n + findSum(sum - n, n -1) 或者 findSum (sum, n) = findSum (sum, n -1)

public class FindSum {
private static Stack<Integer> stack = new Stack<Integer>();
public static void findSum(int sum, int n) {
//找不到满足条件的情况
if ( sum <= 0 || n <= 0) return;
//sum 大于 n， 需要再次递归
if (sum > n) {
stack.push(n); //取 n
findSum(sum - n, n - 1);// n加入，取 n = n-1, sum = sum-n
stack.pop(); // 不取 n
findSum(sum, n - 1);    // n没有加入，取n = n-1,sum = sum
} else {
System.out.print(sum);  //  sum <= n ,直接输出n就可以满足了
for (int i = 0; i < stack.size(); i ++)
System.out.print(" "+ stack.get(i));
System.out.println();

//上面部分得到的是一种情况，但是还存在另一种情况，我们需要找出从 1 到 sum - 1 里面是否存在值，它们的和等于sum
if (sum != 1) {
findSum(sum, sum - 1);
}
}
}
public static void main(String[] args) {
int sum = 8;
int n = 5;
findSum(sum,n);
}
}

public class FindSum {
private static Stack<Integer> stack = new Stack<Integer>();
public static void findSum(int sum, int n, int number) {
//找不到满足条件的情况
if ( sum <= 0 || n <= 0 || number <= 0 || n < number) return;
//sum 大于 n， 需要再次递归
if (sum > n ) {
stack.push(n); //取 n
findSum(sum - n, n - 1, number - 1);// n加入，取 n = n-1, sum = sum-n
stack.pop(); // 不取 n
findSum(sum, n - 1, number);    // n没有加入，取n = n-1,sum = sum
} else if (number == 1) {
System.out.print(sum);  //  sum <= n ,直接输出n就可以满足了
for (int i = 0; i < stack.size(); i ++)
System.out.print(" "+ stack.get(i));
System.out.println();
} else {
findSum(sum, sum -1, number);
}
}
public static void main(String[] args) {
int sum = 10;
int n = 12;
int number = 4;
findSum(sum,n, number);
}
}

public class FindSum {
public static int[] array = {5, 2, 1, 7, 3, 4, 6, 9};
private static Stack<Integer> stack = new Stack<Integer>();
public static void findSum(int sum, int n, int number) {

if (number == 0 && sum == 0) {
for (int i = 0; i < stack.size(); i ++) {
System.out.print(" "+ stack.get(i));
}
System.out.println();
return;
}

if (sum <= 0 || n < 0 || number <= 0 || n < number - 1) return;

stack.push(array[n]); //取 n
findSum(sum - array[n], n - 1, number - 1);// n加入，取 n = n-1, sum = sum-n
stack.pop(); // 不取 n
findSum(sum, n - 1, number);    // n没有加入，取n = n-1,sum = sum
}
public static void main(String[] args) {
int sum = 10;
int number = 4;
findSum(sum, array.length - 1, number);
}
}

public class FindSum {
public static int[] array = {5, 5, 5,5,3,3,3, 2,2,2, 1,1,1,1};
private static Stack<Integer> stack = new Stack<Integer>();
public static void findSum(int sum, int n, int number) {
if (number == 0 && sum == 0) {
for (int i = 0; i < stack.size(); i ++) {
System.out.print(" "+ stack.get(i));
}
System.out.println();
return;
}
if (sum <= 0 || n < 0 || number <= 0 || n < number - 1) return;
stack.push(array[n]); //取 n
findSum(sum - array[n], n - 1, number - 1);// n加入，取 n = n-1, sum = sum-n
stack.pop(); // 不取 n
//如果下一个值重复了，就要找到与pop出来的那个值不同的第一个。
if (n > 0 && array[n - 1] == array[n] ) {
while ((n - 1) >= 0 && array[n - 1] == array[n]) {
n--;
}
} else {
findSum(sum, n - 1, number);    // n没有加入，取n = n-1,sum = sum
}
}

public static void main(String[] args) {
int sum = 18;
int number = 6;
findSum(sum, array.length - 1, number);
}
}

3
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：587596次
• 积分：6296
• 等级：
• 排名：第3698名
• 原创：122篇
• 转载：21篇
• 译文：0篇
• 评论：170条
文章分类
评论排行
最新评论