Temperatures
import java.util.*;
import java.io.*;
import java.math.*;
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
class Solution {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int N = in.nextInt(); // the number of temperatures to analyse
// System.err.println("N : "+ N);
in.nextLine();
int temperatures = 0;
if(N > 0 ){
String TEMPS = in.nextLine(); // the N temperatures expressed as integers ranging from -273 to 5526
System.err.println(TEMPS);
String[] arr_str = TEMPS.split(" ");
// System.err.println("size : " + arr_str.length);
int[] arr_number = new int[N];
int temp = 0;
for(int i = 0 ; i < N; i++){
arr_number[i]= Integer.parseInt(arr_str[i]);
// System.err.println("arr_number["+i+"] : " + arr_number[i]);
//0<= N <1000 0보단 크고 1000보단 작다.
if(arr_number[i] > 0){
// System.err.println("arr_number["+i+"] : " + arr_number[i]);
// System.err.println("[i] : " + i);
if(arr_number[temp] < 0){
temp=i;
}
if(arr_number[temp] >= arr_number[i]){
temp=i;
temperatures = arr_number[temp];
}
}
}
}
// Write an action using System.out.println()
// To debug: System.err.println("Debug messages...");
System.out.println(temperatures);
}
}
75%
Statement
In this exercise, you have to analyze records of temperature to find the closest to zero.
Sample temperatures. Here, -1 is the closest to 0.
Write a program that prints the temperature closest to 0 among input data.
INPUT:
Line 1: N, the number of temperatures to analyse
Line 2: The N temperatures expressed as integers ranging from -273 to 5526
OUTPUT:
Display 0 (zero) if no temperature is provided
Otherwise, display the temperature closest to 0, knowing that if two numbers are equally close to zero, positive integer has to be considered closest to zero (for instance, if the temperatures are -5 to 5, then display 5)
CONSTRAINTS:
0 ≤ N < 10000
EXAMPLE:
Input
5
1 -2 -8 4 5
Output
1
Download the files provided in the test script:
Simple test case: in.txt out.txt
Complex test case: in.txt out.txt
No temperature: in.txt out.txt
Result is correct with a simple data set: {7 5 9 1 4} -> 1 (500 pts)
It works with -273 alone (250 pts)
It works with 5526 alone (250 pts)
It works when inputs contains only negative numbers: : {-15 -7 -9 -14 -12} -> -7 (250 pts)
When two temperatures are as close to 0, then the positive wins: {15 -7 9 14 7 12} -> 7 (750 pts)
It works with two negative temperatures that are equal: {-10 -10} -> -10 (250 pts)
The solution displays 0 if no temperature (250 pts)
Readability Analysis
Avoid really long methods.
Deeply nested if..then statements are hard to read
Deeply nested if..then statements are hard to read