最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - My solution for a Kattis problem (Hitastig) keep returning a Run-Time Error - Stack Overflow

programmeradmin4浏览0评论

I'm trying to solve the Kattis problem: Hitastig

The task is simply to print the highest and lowest temperatures from a given list of temperature values. Everytime I submit my solution I keep failing on test case 26/31 where the only details I get on the error is that my code produced a "Run-Time Error". My solution generates the correct solutions for the sample inputs and it runs fine in my personal IDE.

Is there something blatant I'm missing here? What could be wrong in my code that results in this error?

Any help here would be appreciated.

Thanks in advance.

import java.util.Scanner;

public class hitastig {
    public static void main(String[] args) {
        Scanner myScanner = new Scanner(System.in);
        int n = myScanner.nextInt();
        int maxTemp = Integer.MIN_VALUE;
        int minTemp = Integer.MAX_VALUE;

        for (int i = 0; i < n; i++) {
            int temp = myScanner.nextInt();
            maxTemp = Math.max(maxTemp, temp);
            minTemp = Math.min(minTemp, temp);
        }

        System.out.println(maxTemp + " " + minTemp);
        myScanner.close();
    }
}

I've tried:

  • Handling Large Inputs (Not Likely an Issue, But Good to Check).
  • Handling Edge Case: n = 0 (Empty Input).

What I expected:

  • Fixes potential crash when n = 0 (by checking if (n == 0) return;).

What actually happened:

  • Nothing, the problem still persists.

I'm trying to solve the Kattis problem: Hitastig

The task is simply to print the highest and lowest temperatures from a given list of temperature values. Everytime I submit my solution I keep failing on test case 26/31 where the only details I get on the error is that my code produced a "Run-Time Error". My solution generates the correct solutions for the sample inputs and it runs fine in my personal IDE.

Is there something blatant I'm missing here? What could be wrong in my code that results in this error?

Any help here would be appreciated.

Thanks in advance.

import java.util.Scanner;

public class hitastig {
    public static void main(String[] args) {
        Scanner myScanner = new Scanner(System.in);
        int n = myScanner.nextInt();
        int maxTemp = Integer.MIN_VALUE;
        int minTemp = Integer.MAX_VALUE;

        for (int i = 0; i < n; i++) {
            int temp = myScanner.nextInt();
            maxTemp = Math.max(maxTemp, temp);
            minTemp = Math.min(minTemp, temp);
        }

        System.out.println(maxTemp + " " + minTemp);
        myScanner.close();
    }
}

I've tried:

  • Handling Large Inputs (Not Likely an Issue, But Good to Check).
  • Handling Edge Case: n = 0 (Empty Input).

What I expected:

  • Fixes potential crash when n = 0 (by checking if (n == 0) return;).

What actually happened:

  • Nothing, the problem still persists.
Share Improve this question asked Mar 11 at 11:37 Nikolas KolicNikolas Kolic 133 bronze badges 2
  • 1 "integers -10¹⁸ < ai < 10¹⁸" ... try inputting a very large number (e.g. 10¹⁷) and see what happens – teapot418 Commented Mar 11 at 11:55
  • Where in the universe can you find a temperature of -10¹⁸? – k314159 Commented Mar 11 at 15:03
Add a comment  | 

1 Answer 1

Reset to default 0

The problem specifies "integers -10¹⁸ < ai < 10¹⁸" which is beyond the range of int (int goes up to 2*10⁹-ish). It seems to fit into long, so you can try to change the type from int to long and the reading of input accordingly. Or there is always BigInteger.

发布评论

评论列表(0)

  1. 暂无评论