[英]How to calculate the number of combinations of 3 integers less than n whose sum is greater than n * 2?
I am solving some java algorithm-analysis questions and this problem has me stumped. 我正在解决一些Java算法分析问题,而这个问题使我感到困惑。 This particular problem asks for the value that is returned by x(10) where is x is the following function: 此特定问题要求x(10)返回的值,其中x是以下函数:
public static int x(int n)
{
int count = 0;
for(int i = 0; i <= n; i++)
{
for (int j = 0; j <= n; j++)
{
for (int k = 0; k <= n; k++)
{
System.out.println(i+","+j+","+k);
if (i + j + k > 2 * n)
count++;
}
}
}
return count;
}
Essentially, the problem is asking for the number of combinations of 3 integers less than n whose sum is greater than n * 2. 本质上,问题是要求小于n且其和大于n * 2的3个整数的组合数。
What is the fastest problem-solving technique for this problem, and just general "complicated" nested loop problems? 什么是最快的解决问题的技术,而仅仅是一般的“复杂”嵌套循环问题?
I set up a variable table and kept track of variables a, b, and c representing the 3 integers and a count variable which increments each time 'a+b+c > n*2' but after n=3, the tables became unnecessarily tedious. 我建立了一个变量表,并跟踪代表3个整数的变量a,b和c,以及一个计数变量,该变量每次'a + b + c> n * 2'都会增加,但是在n = 3之后,这些表就变得不必要了乏味。 There must be a mathematical solution. 必须有一个数学解。
x(10) returns 220, but I do not know how the algorithm arrives at that answer and how to find, say, x(7). x(10)返回220,但我不知道算法如何得出该答案以及如何找到x(7)。
Your code is incorrect. 您的代码不正确。
i < n,
j < n,
k < n,
because you mentioned "less than". 因为您提到“少于”。
As you mentioned "Combination", but your code doesn't remove some repeated combinations, for example, if n = 5, there are only two combinations which satisfise the conditions, they are (4, 4, 4) and (4, 4, 3), thus the result is 2, apparently your code will return a bigger number which is incorrect. 正如您提到的“组合”,但是您的代码不会删除某些重复的组合,例如,如果n = 5,则只有两个满足条件的组合,分别是(4,4,4)和(4,4 ,3),结果为2,显然您的代码将返回更大的数字,这是不正确的。
Could the result of this problem be a mathmatic expression ? 这个问题的结果可能是数学表达式吗? think about this follow equation: 考虑下面的等式:
n1 + n2 + n3 = 2 * n
this equation is a typical se called "Diophantine Equation", which is proved that there doesn't exist general algorithm to resolve all of them, and this equation is so relative to the origin problem, so i guess no. 该方程式是一个典型的称为“ Diophantine方程式”,证明没有通用的算法可解决所有方程式,并且该方程式与原点问题有关,所以我想没有。
I've changed your code, using hashset to remove all repeated combinations, hope is helpful. 我已更改您的代码,使用哈希集删除所有重复的组合,希望对您有所帮助。
public static int getCombinationNumber(int num) { HashSet<String> hs = new HashSet(); // To save the unic form (or representation) for each combination int count = 0; for (int i = 0; i < num; i++) for (int j = 0; j < num; j++) for (int k = 0; k < num; k++) { int[] nums = {i, j, k}; sort(nums); // To ensure all the combinations of i, j, k form a unic array String unicForm = Arrays.toString(nums); // Convert array to string in order to compare and save if (i + j + k > 2 * num && !hs.contains(unicForm)) { count++; hs.add(unicForm); System.out.println(i + ", " + j + ", " + k); } } return count; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.