-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtemperatures.java
More file actions
53 lines (40 loc) · 1.2 KB
/
temperatures.java
File metadata and controls
53 lines (40 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.util.*;
import java.io.*;
import java.math.*;
class Solution {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
in.nextLine();
String TEMPS = in.nextLine();
if(N == 0)
System.out.println("0");
else if(N == 1)
System.out.println(TEMPS);
else{
int n = TEMPS.length(), k;
int tab [] = new int [N];
String str = TEMPS;
for(int i = 0; i < N-1; i++){
k = str.indexOf(" ");
tab[i] = Integer.valueOf(str.substring(0, k));
str = str.substring(k+1);
}
tab[N-1] = Integer.valueOf(str);
nearestToZero(tab);
}
}
public static void nearestToZero( int tab[]){
int n = tab.length;
//int result = 0;
for(int j = 1; j < n; j++){
if(Math.abs(tab[0]) > Math.abs(tab[j]))
tab[0] = tab[j];
if(Math.abs(tab[0]) == Math.abs(tab[j])){
if(String.valueOf(tab[0]).contains("-"))
tab[0] = tab[j];
}
}
System.out.println(tab[0]);
}
}