[英]Determine factors of a number (n), then return possible pairs of positive integers that when multiplied together are less than (n)?
Basically, the directions go as such: 基本上,方向是这样的:
Here is a sample output: 这是一个示例输出:
Enter an integer: 24
4 = 2 x 2
6 = 2 x 3
8 = 2 x 4
10 = 2 x 5
12 = 2 x 6
14 = 2 x 7
16 = 2 x 8
18 = 2 x 9
22 = 2 x 11
24 = 2 x 12
9 = 3 x 3
12 = 3 x 4
15 = 3 x 5
18 = 3 x 6
21 = 3 x 7
24 = 3 x 8
16 = 4 x 4
20 = 4 x 5
24 = 4 x 6
(Note: Products can appear more than once, but pairs should not) (注意:产品可以出现多次,但配对不能出现)
For my solution, I started by determining the factors of n like so: 对于我的解决方案,我首先确定n的因子,如下所示:
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
int factors = 0;
System.out.println("Enter integer:");
int n = keyboard.nextInt();
for(int i = 2; i <=n ; i++) {
if(n % i == 0) {
System.out.println(i);
}
}
From there, it seems I should pull each factor and multiply it by an incremented variable starting at 2 until it is equal to or exceeds (n). 从那里开始,我似乎应该拉出每个因子,然后将其乘以一个从2开始的递增变量,直到等于或超过(n)。 I started to thing maybe this was incorrect, so I tried something like this instead:
我开始觉得这可能是不正确的,所以我尝试了如下操作:
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter integer:");
int n = keyboard.nextInt();
int index = 2;
int multiplier = 2;
int result = 0;
while(result < n) {
result = multiplier * index;
System.out.println(result);
index++;
}
Which works, but only for result 4 - 24
, as multiplier never increments two 3. Is the actual solution just a hybrid of these possible solutions? 哪个有效,但仅对
result 4 - 24
,因为乘数永远不会增加2 3.实际解决方案只是这些可能解决方案的混合吗? Guidance in the correct direction would be appreciated, thank you! 朝着正确方向的指导将不胜感激,谢谢!
I think your current approach is off, and in particular is missing one critical element to solving this: a double loop. 我认为您当前的方法已失效,尤其是缺少解决此问题的一个关键要素:双循环。 I think the easiest way to handle this is to loop twice and generate all pairs meeting the requirements.
我认为处理此问题的最简单方法是循环两次并生成所有符合要求的对。
int number = 24;
for (int i=2; i < (int)Math.ceil(Math.sqrt(number)); ++i) {
for (int j=i; j <= number / 2; ++j) {
int pair = i * j;
if (pair <= number) {
System.out.println(pair + " = " + i + " x " + j);
}
}
}
The tricky part here was in determining the bounds for the two for
loops. 这里最棘手的部分是确定两个
for
循环的边界。 The second loop variable starts with the value of the first, and can get as large as half the input number. 第二个循环变量以第一个循环变量的值开头,最大可以等于输入数字的一半。 This is because the widest pair would occur when the first number be
2
, forcing the second number to be the input halved. 这是因为当第一个数字为
2
,将出现最宽的一对,从而迫使第二个数字被减半。 The bounds on the first loop are a bit more complex. 第一个循环的边界要复杂一些。 We used the ceiling of the square root as the bounds, because the largest it can ever be would occur when both numbers are the same.
我们将平方根的上限用作边界,因为当两个数字相同时,可能会出现最大的上限。
Try something like this. 尝试这样的事情。
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter integer: ");
int n = keyboard.nextInt();
for (int i = 2; i <= n / 2; i++) {
for (int j = 2; i * j <= n; j++) {
System.out.println(i + " x " + j + " = " + (i * j));
}
}
The result as follows. 结果如下。
Enter integer: 24
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
2 x 11 = 22
2 x 12 = 24
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
7 x 2 = 14
7 x 3 = 21
8 x 2 = 16
8 x 3 = 24
9 x 2 = 18
10 x 2 = 20
11 x 2 = 22
12 x 2 = 24
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.