forked from sowon-dev/AlgorithmStudy_Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlusMinus.java
More file actions
31 lines (28 loc) ยท 852 Bytes
/
PlusMinus.java
File metadata and controls
31 lines (28 loc) ยท 852 Bytes
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
package hackerrank;
import java.util.Arrays;
public class PlusMinus {
static void plusMinus(int[] arr) {
//์ฃผ์ด์ง n๊ฐ์ ์ซ์ ์ค ์์, ์์, 0์ ๋น์จ์ ๊ตฌํ๋ ๋ฌธ์ ์ด๋ค.
//์ด ๋น์จ์ ํฉ์ 1์ด๋ค.
int cntPlus = 0;
int cntMinus = 0;
for(int a : arr){
if(a > 0){
cntPlus++;
}else if( a < 0){
cntMinus++;
}
}
System.out.printf("%.6f%n", (double) cntPlus/arr.length);
System.out.printf("%.6f%n", (double) cntMinus/arr.length);
System.out.printf("%.6f%n", (double) (arr.length-cntPlus-cntMinus)/arr.length);
//System.out.println(Arrays.toString(arr));
}
public static void main(String[] args) {
int[] arr1 = {-4, 3, -9, 0, 4, 1};
plusMinus(arr1);
System.out.println("ans: 0.500000\n"
+ "0.333333\n"
+ "0.166667");
}
}