有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java关闭扫描程序会影响性能吗

我在解决一个竞争问题,在这个问题上,我使用扫描仪获取用户输入
这是两个代码段,一个关闭的扫描仪和一个不关闭的扫描仪

Closing scanner

import java.util.Scanner;

public class JImSelection {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.valueOf(scanner.nextLine());
        while (n-- > 0) {
            double number = (Math.log(Long.valueOf(scanner.nextLine())) / Math.log(2));
            System.out.println((int) number - number == 0 ? "Yes" : "No");
        }
        scanner.close();
    }
}

Not closing scanner

import java.util.Scanner;

public class JImSelection {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.valueOf(scanner.nextLine());
        while (n-- > 0) {
            double number = (Math.log(Long.valueOf(scanner.nextLine())) / Math.log(2));
            System.out.println((int) number - number == 0 ? "Yes" : "No");
        }           
    }
}

第一个(关闭扫描器)给我评分^{
第二个(不关闭扫描器)给出15.22
我认为,当我使用scanner.close();时,编译器正在释放资源,这就是分数不同的原因

这是评分规则

It is assigned a score of 100. Suppose, you submit a solution of n characters, then your score is (56/n)*100.


共 (1) 个答案

  1. # 1 楼答案

    It is assigned a score of 100. Suppose, you submit a solution of n characters, then your score is (56/n)*100.

    你在开玩笑吧?一个解决方案的得分是14.47。这意味着您的源代码是56 / (14.47/100) =~= 387个字符。(=~=表示缺少“大约相等”符号)

    在另一个例子中,你的分数是15.22,这意味着你的源代码长度是56 / (15.22/100) =~= 368个字符

    差29个字符,这可能是源代码行的长度,表示scanner.close();(包括前导空格、两个尾随空格和一个回车/换行符对)

    这与代码的性能无关