如何在Java 8中获取编程竞赛的输入?

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

How to take inputs in Java8 for Coding Competitions?

问题

以下是翻译好的部分:

我一直在练习竞技编程,但令我非常惊讶的是,尽管我的代码在本地IDE(如Eclipse、BlueJ或Netbeans)中运行得非常好,但有些代码却出现了运行时错误。

我使用了Scanner类或BufferedReader类与StringTokenizer来从一行中获取多个输入。但是,运行时错误仍然存在。
实际上,在竞技编程中,我认为我的代码所获取的输入是null,而不是一个值。这就是为什么Integer.parseInt(br.readLine())会抛出格式异常。

我想知道如何在Java 8中进行输入:一行中获取多个输入和一行中获取一个输入。

下面我分享了一段代码,在我的本地IDE上运行正常,但在Google Kick-start IDE中抛出了运行时错误:

(接下来是代码部分,但由于您要求不翻译代码部分,所以我无法提供具体的翻译内容。)

问题:
Isyana知道了她当地主题公园连续N天的游客人数。第i天的游客人数是Vi。如果一个条件都满足,那么这一天就是创纪录的:1. 当天的游客人数严格大于前面每天的游客人数。2. 它要么是最后一天,要么当天的游客人数严格大于后面一天的游客人数。注意,第一天也可能是创纪录的一天!

请帮助Isyana找出创纪录的天数。

输入:
输入的第一行给出了测试用例的数量T。接下来是T个测试用例。每个测试用例以一个包含整数N的行开始,第二行包含N个整数,第i个整数是Vi。

样例输入:
4
8
1 2 0 7 2 0 2 0
6
4 8 15 16 23 42
9
3 1 4 1 5 9 2 6 5
6
9 9 9 9 9 9

输出:
Case #1: 2
Case #2: 1
Case #3: 3
Case #4: 0

它抛出了运行时错误!帮帮我!
在Google Kick-Start中没有关于问题所在的消息。

对于在线IDE,显示以下错误消息:

Exception in thread "main" java.lang.NullPointerException
at java.util.StringTokenizer.(StringTokenizer.java:199)
at java.util.StringTokenizer.(StringTokenizer.java:236)
at RecordBreaking$FastReader.next(RecordBreaking.java:23)
at RecordBreaking$FastReader.nextInt(RecordBreaking.java:35)
at RecordBreaking.main(RecordBreaking.java:65)

英文:

I have been practicing competitive coding and to my utter surprise, some codes are getting Runtime Error though my code runs perfectly well in local IDE like Eclipse, BlueJ, or Netbeans.
I used Scanner class or BufferedReader class with StringTokenizer to take multiple inputs from single line. But, the runtime error prevails.
Actually, in Competitive Coding, I think the input my code is taking is null, not a value. That is why Integer.parseInt(br.readLine()) throws Format exception.

I want to know how to take inputs in java8: single line multiple inputs and single line one input.

Below I am sharing a code that runs fine on my local IDE but throws a runtime error in Google Kick-start IDE:

/*recordbreaking problem of kickstart group D*/
    import java.util.*;
    import java.io.*;
    class RecordBreaking
    {
    static class FastReader 
    { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastReader() 
        { 
            br = new BufferedReader(new
                     InputStreamReader(System.in)); 
        } 
  
        String next() 
        { 
            while (st == null || !st.hasMoreElements()) 
            { 
                try
                { 
                    st = new StringTokenizer(br.readLine()); 
                } 
                catch (IOException  e) 
                { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        } 
  
        int nextInt() 
        { 
            return Integer.parseInt(next()); 
        } 
  
        long nextLong() 
        { 
            return Long.parseLong(next()); 
        } 
  
        double nextDouble() 
        { 
            return Double.parseDouble(next()); 
        } 
  
        String nextLine() 
        { 
            String str = ""; 
            try
            { 
                str = br.readLine(); 
            } 
            catch (IOException e) 
            { 
                e.printStackTrace(); 
            } 
            return str; 
        } 
    }
    public static void main(String args[])
    {
    FastReader s=new FastReader();
    int t=s.nextInt();
    int tans[] = new int[t];
    int n,c,sum=0;
    int v[] = new int[10000];
    for(int i=0;i<t;i++)
    {
        c=0;
        n=s.nextInt();
        for(int j=0;j<n;j++)
        {
            v[i]=s.nextInt();
        }
        for(int m=0;m<n;m++)
        {
            for(int q=0;q<m;q++)
                sum=sum+v[q];
            if((m==(n-1)||m==0)||(v[m+1]<v[m])&&(sum<v[m]))
                c++;
            sum=0;    
        }
        System.out.println(c);
    }
    
    
    }
}

Problem
Isyana is given the number of visitors at her local theme park on N consecutive days. The number of visitors on the i-th day is Vi. A day is record breaking if it satisfies both of the following conditions:
The number of visitors on the day is strictly larger than the number of visitors on each of the previous days.
Either it is the last day, or the number of visitors on the day is strictly larger than the number of visitors on the following day.
Note that the very first day could be a record breaking day!

Please help Isyana find out the number of record breaking days.

Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a line containing the integer N. The second line contains N integers. The i-th integer is Vi.

Sample Inputs
Sample

Input
 
 
4
8
1 2 0 7 2 0 2 0
6
4 8 15 16 23 42
9
3 1 4 1 5 9 2 6 5
6
9 9 9 9 9 9

**Output**
  
Case #1: 2
Case #2: 1
Case #3: 3
Case #4: 0

It throws Runtime error! Help!
There is no such message in Google Kick-Start regarding where the problem lies.

For online IDE: the following error message is displayed:

Exception in thread "main" java.lang.NullPointerException
	at java.util.StringTokenizer.<init>(StringTokenizer.java:199)
	at java.util.StringTokenizer.<init>(StringTokenizer.java:236)
	at RecordBreaking$FastReader.next(RecordBreaking.java:23)
	at RecordBreaking$FastReader.nextInt(RecordBreaking.java:35)
	at RecordBreaking.main(RecordBreaking.java:65)

答案1

得分: 1

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

问题在于你的代码中的类名在Google KickStart中请将类名使用 public class Solution同时我运行了你的代码得到了 WA可能是指 Wrong Answer),以下是我的代码

import java.util.*;
public class Solution {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for(int loop = 1; loop <= T; loop++) {
            int N = sc.nextInt();
            int arr[] = new int[N + 7];
            for(int i = 0; i < N; i++) {
                arr[i] = sc.nextInt();
            }
            int max = -1;
            int ans = 0;
            for(int i = 0; i < N - 1; i++) {
                //if(arr[i] > max) max = arr[i];
                if(arr[i] > max) {
                    if(arr[i] > arr[i + 1]) ans++;
                    max = arr[i];
                }
            }
            if(arr[N - 1] > max) ans++;
            System.out.println("Case #" + loop + ": " + ans);
        }
    }
}
希望对你有所帮助如果你发现正确并接受了这个答案请点赞

注意:以上内容仅为翻译的代码部分,不包含问题描述和其他信息。

英文:

The problem with your code is your class name. On Google KickStart use class name as public class Solution. Also I ran your code and gives WA so this is my code.

import java.util.*;
public class Solution {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for(int loop=1; loop&lt;=T; loop++){
           int N = sc.nextInt();
           int arr[] = new int[N+7];
            for(int i=0; i&lt;N; i++){
                arr[i] = sc.nextInt();
            }
            int max = -1;
            int ans = 0;
            for(int i=0; i&lt;N-1; i++){
                //if(arr[i]&gt;max) max = arr[i];
                if(arr[i]&gt;max ) {
                    if(arr[i]&gt;arr[i+1])ans++;
                    max =arr[i];
                }
            }
            if(arr[N-1]&gt;max) ans++;
            System.out.println(&quot;Case #&quot; + loop+&quot;: &quot;+ans);
        }
    }
}

Hope it is helpfull!! Do upvote if you find it correct and accept it.

答案2

得分: 0

我不能百分之百确定问题,但我会分享我通常的做法。标准的 Scanner 应该可以工作,也许在这种情况下还有其他问题,但我不认为可以从这些信息中得出结论。

Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
    int n = scanner.nextInt();
    int[] numbers = new int[n];
    for (int j = 0; j < n; j++) {
        numbers[j] = scanner.nextInt();
    }
}

这对我总是有效的,但我不能确定。抱歉,不能提供更多帮助,但我也不确定其他人是否能够。

英文:

I can't say I am %100 sure of the problem, but I'll share what I usually do. Standard Scanner should work, maybe there is another problem in this case but I don't believe it is possible to say from this information.

Scanner scanner = new Scanner(System.in);
int t= scanner.nextInt();
for (int i = 0; i &lt; t; i++) {
    int n = scanner.nextInt();
    int[] numbers = new int[n];
    for (int j = 0; j &lt; n; j++) {
        numbers[j] = scanner.nextInt();
    }
}

This always works for me but again I can't know for certain. Sorry, won't be able to help more but then again I'm not sure if anyone else can.

huangapple
  • 本文由 发表于 2020年8月17日 14:46:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63445844.html
匿名

发表评论

匿名网友

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

确定