简体   繁体   English

如何计算小于n且总和大于n * 2的3个整数的组合数?

[英]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. 您的代码不正确。

  1. First, the for loops should be corrected as 首先,for循环应更正为
i < n,
j < n,
k < n,

because you mentioned "less than". 因为您提到“少于”。

  1. 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,显然您的代码将返回更大的数字,这是不正确的。

  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方程式”,证明没有通用的算法可解决所有方程式,并且该方程式与原点问题有关,所以我想没有。

  1. 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.

相关问题 生成 n 个随机数,其总和为 m 且所有数应大于零 - Generate n random numbers whose sum is m and all numbers should be greater than zero 确定一个数(n)的因数,然后返回乘以小于(n)的可能的正整数对? - Determine factors of a number (n), then return possible pairs of positive integers that when multiplied together are less than (n)? 小于n的平方根的n的因数 - number of factors of n that are less than square root of n 在小于 N 的数字中找到每个数字的总和为 d 的数字的数量 - Find the number of numbers where the sum of each number is d in numbers less than N 在小于 O(n^2) 的范围内对给定的整数列表进行排名 - Ranking a given list of integers in less than O(n^2) 如何找到总和等于或小于给定数量的元组数? - How to find number of tuples whose sum is equal or less than a given number? 计算小于或等于N的两个数字的对数,以使对数的数字总和为质数 - Count number of pairs of two numbers less than or equal to N such that Sum of the digits of numbers of pair is Prime 打印出素数小于给定数N - Print out prime Number less than a given number N 查找数组在小于O(n ^ 2)内重复的次数 - Find the number of times a number is repeated in an array in less than O(n^2) 模式java:2个字符小于n次,它们的总和也小于n - Pattern java : 2 caracters less than n times and their sum of appereance less n too
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM