当和do-while循环 最小值

huangapple go评论69阅读模式
英文:

The while and do-while loops The smallest value

问题

以下是翻译好的代码部分:

import java.util.*;
class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long number = scanner.nextLong();
        int fact = 1;
        int count = 1;
        while (fact <= number) {
            count++;
            fact *= count;
        }
        System.out.println(count);
    }
}

测试输入:
6188989133
正确输出:
13

你的代码输出:

我的代码输出是空的。我在这里漏掉了什么?

英文:

I need to write a program that a user inputs a long positive number m. I need to find out what is the smallest int number n such that n! > m.

So far I have written such a code:

import java.util.*;
class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner (System.in);
        long number = scanner.nextLong();
        int fact = 1;
        int count =1;
        while (fact &lt;= number) {
            count ++;
            fact *=count;
        }
        System.out.println(count);
    }
}

Test input:
6188989133
Correct output:
13

Your code output:

My code output is empty. What I am missing here?

答案1

得分: 6

你必须使用 long 类型来定义变量 fact。当你使用 int 类型时,可能的取值范围介于 -2,147,483,6482,147,483,647 之间。

然而,你永远不会超过输入数字 6,188,989,133,因此你的 while(fact &lt;= number) 循环将永远不会退出。每个可能的整数值都会小于 6,188,989,133。这就解释了为什么你的代码输出是 "empty",因为它根本不会执行到那行 System.out.println(count);

请记住,long 类型也有这样的限制,值只能在 -9,223,372,036,854,775,8089,223,372,036,854,775,807 之间。当输入的数字非常大时,你将无法找到大于输入的正确 fact 值,因为受到 long 类型值范围的限制。例如,对于输入数字 9,000,000,000,000,000,000(位于 long 类型值范围内),下一个阶乘数是 51,090,942,171,709,440,00021! 的值),它超出了 long 类型的值范围。

你可以使用 java.math.BigInteger 类,它具有 "无限" 范围的可能值,并且可以在 BigInteger 对象上进行数学运算。

英文:

You have to use long for the fact variable. When you use int the possible values are between -2,147,483,648 and 2,147,483,647.

However, you will never exceed the input number 6,188,989,133, so your while(fact &lt;= number) loop will never exits. Every integer value possible will be smaller than 6,188,989,133. This explains why your code output is "empty", as it doesn't reach that System.out.println(count); line at all.

Keep in mind that the long type has such a limit as well, values can only be between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807. When the input number is that big, you will not find the correct fact value which will be greater than the input because you are limited by the long type value range. As an example, for the input number 9,000,000,000,000,000,000 (which is inside the long type value range) the next factorial number is 51,090,942,171,709,440,000 (the value of 21!), which is outside the long type value range.

You can use the java.math.BigInteger class which has an "unlimited" range of possible values and do your math operations on BigInteger objects.

答案2

得分: 0

fact 应声明为 long。检查下面的代码:

public class TestJava {
    public static void main(String[] args) {
    	
    	try{
    		
    		System.out.println("输入数字:");
    	    Scanner scanner = new Scanner(System.in);
    	    long number = scanner.nextLong();
    	    
            if(number >= 2432902008176640000L) {
                // 记录一个适当的消息,例如无法处理大于 2432902008176640000L 的 long 值
                return 21;
            }
    
    	    long fact = 1;
    	    int count = 1;
    	    while (fact <= number) {
    	        count++;
    	        fact *= count;
    	    }
    	    System.out.println("输出:" + count);
    	}catch(Exception e){
    	    e.printStackTrace();
    	}
    }
}
**更新警告):**

正如 @Pshemo 所提到的如果没有 `if 条件`,上面的代码在计算 20! 时没问题但在计算 20! 之后将导致无限循环问题因此必须注意这个条件代码已经相应地进行了更新
英文:

fact should be declared as long. Check the code below:

public class TestJava {
    public static void main(String[] args) {
	    	
    	try{
	    	
    		System.out.println(&quot;Input number: &quot;);
		       Scanner scanner = new Scanner (System.in);
		        long number = scanner.nextLong();
                if(number &gt;= 2432902008176640000L) {
                  // log an appropriate message like can not handle 
                  // long value greater than 2432902008176640000L
                   return 21;                       
                }

		        long fact = 1;
		        int count =1;
		        while (fact &lt;= number) {
		            count ++;
		            fact *=count;
		        }
		        System.out.println(&quot;output: &quot;+ count);
		    }catch(Exception e){	    	
		    	e.printStackTrace();
	    	}
	   }
}

Update (Warning):

As @Pshemo mentioned, with out the if condition, the above code is fine till 20!, but after that it will cause the infinite loop problem. Hence this condition has to be taken care of. Code has been updated accordingly.

答案3

得分: -1

你需要将 fact 类型更改为 long。

英文:

You need to change fact type to long

huangapple
  • 本文由 发表于 2020年9月17日 18:43:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63936351.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定