【问题标题】:HackerRank says ~ no response on stdout ~HackerRank 说~stdout 没有反应~
【发布时间】:2016-08-20 11:50:22
【问题描述】:

所以我在 HackerRank (Project Euler Q1) link here 上解决了这个问题,并使用了以下代码来解决它

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */

    int T = 0;
    ArrayList<Integer> N = new ArrayList<Integer>();
    String[] Input = args;
    if  (args.length>1){
        T = Integer.parseInt(args[0]);
        if(T<1||T>Math.pow(10,6))
            System.exit(0);
    }
    if (args.length-1 == T){
        for (int i=1;i<args.length;i++){
            N.add(Integer.parseInt(args[i]));
            int sum3=0,sum5=0,sum15=0;
            int count=0;
            while (count<N.get(i-1)){
                sum3+=count;
                count+=3;
            }
            count =0;
            while (count<N.get(i-1)){
                sum5+=count;
                count+=5;
            }
            count =0;
            while (count<N.get(i-1)){
                sum15+=count;
                count+=15;
            }
            N.set(i-1,(sum3+sum5-sum15));
        }
    }
    for(int j=0;j<N.size();j++)
        System.out.println(N.get(j));
    }
}

这在 IDE 上给了我以下输出:

23
2318

输入时:

2
10
100

这与 HackerRank 上的预期输出相匹配,但是当我在网站上使用此代码时,它显示:

输入(标准输入)

2
10
100

你的输出(标准输出)

~ no response on stdout ~

预期输出

23
2318

编译器消息

Wrong Answer

我可以观察到的一件事是我无法从 HackerRank 的循环中打印任何内容。解决办法是什么?

【问题讨论】:

  • 您通过args(命令行参数)而不是STDIN(标准输入)获取参数。

标签: java output stdout


【解决方案1】:

您没有按照 HackerRank 想要的方式从 STDIN 读取数据。而不是从 args 获取输入,您应该这样做:

  Scanner sc = new Scanner(System.in);
  int numberOfTestCases = sc.nextInt();

等等。

【讨论】:

    【解决方案2】:

    我刚刚运行了这段代码,编译好并提交了。

    import java.io.*;
    
    public class Solution{
    
        public static void main(String[] args) throws IOException   
       {
    
            //Input
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            int T = Integer.parseInt(br.readLine());
            int[] N = new int[T];
            for(int t = 0; t < T; N[t++] = Integer.parseInt(br.readLine())){
            }
            br.close();
            br = null;
    
            //Solve
            long[] V = new long[T];
            for(int t = 0; t < T; ++t){
                int n = N[t] - 1;
                V[t] = 3*nSum(n/3) + 5*nSum(n/5) - 15*nSum(n/15);
            }
    
            //Print
            StringBuilder sb = new StringBuilder();
            for(int t = 0; t < T; sb.append(V[t++]).append("\n")){
            }
            System.out.print(sb);
        }
    
        public static long nSum(int n){
            long v = n;
            return (v*v + v) >> 1;
        }
    }
    

    【讨论】:

    • 你为什么多了一个{}
    • 不是代码的一部分。如果了解如何包含代码,请放在 {code goes here} 的两个括号之间
    • 好的.. 已修复。您需要选择代码块并按 Ctrl + K
    猜你喜欢
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-30
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    相关资源
    最近更新 更多