Created
May 8, 2019 05:40
-
-
Save zeraf29/afe53f397bef3413ca751dc8e540bade to your computer and use it in GitHub Desktop.
파라미터로 넘어온 배열 안의 값의 양수,음수, 0의 %비율(소수점 6자리까지)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Complete the plusMinus function below. | |
| static void plusMinus(int[] arr) { | |
| int result = 0; | |
| for(int i=0; i<arr.length; i++){ | |
| result += ( (arr[i]>0) ? (arr.length+1) : ( (arr[i]<0) ? (1) : 0 ) ); | |
| } | |
| //plus number | |
| System.out.println(String.format( "%.6f",(result/(arr.length+1))/(double)(arr.length))); | |
| //minus number | |
| System.out.println( String.format( "%.6f",(result%(arr.length+1))/(double)(arr.length))); | |
| //zero | |
| System.out.println(String.format( "%.6f",(arr.length-(result/(arr.length+1))-(result%(arr.length+1)))/(double)(arr.length))); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
result / (배열사이즈+1)의 몫이 양수 개수(나누는 값에 맞추어 값 증가)
result % (배열사이즈+1)의 나머지가 음의 개수(음의 경우는 1씩 더하였음. 나누는 값보다 작을 경우 증가한 1만큼이 나머지 값이 됨. 배열 전체가 음 일 수 있으므로 배열사이즈+1의 값을 한 것임)
배열사이즈 - 몫 - 나머지 = 0의 개수