admin管理员组文章数量:1404336
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.
- 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
1 Answer
Reset to default 0The 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
.
本文标签: javaMy solution for a Kattis problem (Hitastig) keep returning a RunTime ErrorStack Overflow
版权声明:本文标题:java - My solution for a Kattis problem (Hitastig) keep returning a Run-Time Error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744798274a2625706.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论