英文:
The for-loop The product of numbers from a to b
问题
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long a = scanner.nextLong();
long b = scanner.nextLong();
long multiply = 1; // Initialize with 1 to not affect the multiplication
for (long i = a; i < b; i++) {
multiply *= i;
}
System.out.println(multiply);
}
}
英文:
I need to write a program that prints the product of all integer numbers from a to b (a < b).
Include a and exclude b from the product.
Sample Input 1:
1 2
Sample Output 1:
1
Your code output:
2
Here is my code:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long a = scanner.nextLong();
long b = scanner.nextLong();
long multiply = 0;
for(long i = a; i<b; i++){
multiply = i * (i+1);
}
System.out.println(multiply);
}
}
What I'm doing wrong? Please a hint
UPDATE:
It didn't help either.
Test input:
1 2
Correct output:
1
Your code output:
0
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long a = scanner.nextLong();
long b = scanner.nextLong();
long multiply = 0;
for(long i = a+1; i<b; i++){
multiply = i * (i+1);
if(multiply==2){
--multiply;
}
}
System.out.println(multiply);
}
}
答案1
得分: 10
你应该从 multiply = 1
开始,然后继续用后面的整数相乘。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long a = scanner.nextLong();
long b = scanner.nextLong();
long multiply = 1;
for (long i = a; i < b; i++) {
multiply *= i;
}
System.out.println(multiply);
}
}
一个样例运行:
1 2
1
另一个样例运行:
2 5
24
英文:
You should start with multiply = 1
and then keep multiplying it with the next integers.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long a = scanner.nextLong();
long b = scanner.nextLong();
long multiply = 1;
for (long i = a; i < b; i++) {
multiply *= i;
}
System.out.println(multiply);
}
}
A sample run:
1 2
1
Another sample run:
2 5
24
答案2
得分: 5
让我们在编写代码之前尝试解决方案并理解“从a到b的所有整数的乘积(a < b)”是什么意思。
对于a = 1和b = 2,因为我们包括1但不包括2,结果是1。
同样,对于a = 1和b = 3,结果是1 * 2。
所以你的第一个实现实际上是接近的:你希望循环遍历结果中包括的所有数字,并通过乘法累积这些数字。你不应该只是multiply = i * (i+1);
,因为它会在每次循环迭代中替换multiply
的值,而是应该使用先前的multiply
值,并将其与i
结合起来。
英文:
Let's try to work out the solution before writing code & understand what "product of all integer numbers from a to b (a < b)" is
For a = 1 and b = 2, because we're including 1 and excluding 2, the result is 1
Likewise, for a = 1 and b = 3, result is 1 * 2
So your first implementation is actually close: you'd want to loop through all number included in the result and accumulate those number by multiplication. You wouldn't want just multiply = i * (i+1);
because it will replace the value of multiply
in each loop iteration, instead you should use the previous value of multiply
and combine it with i
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论