Skip to content

Instantly share code, notes, and snippets.

@zeraf29
Created May 8, 2019 05:40
Show Gist options
  • Select an option

  • Save zeraf29/afe53f397bef3413ca751dc8e540bade to your computer and use it in GitHub Desktop.

Select an option

Save zeraf29/afe53f397bef3413ca751dc8e540bade to your computer and use it in GitHub Desktop.
파라미터로 넘어온 배열 안의 값의 양수,음수, 0의 %비율(소수점 6자리까지)
// 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)));
}
@zeraf29
Copy link
Author

zeraf29 commented May 8, 2019

  1. 양수의 경우: 배열 사이즈+1의 값을 result 변수에 저장
  2. 음수: +1
  3. 0: +0

result / (배열사이즈+1)의 몫이 양수 개수(나누는 값에 맞추어 값 증가)
result % (배열사이즈+1)의 나머지가 음의 개수(음의 경우는 1씩 더하였음. 나누는 값보다 작을 경우 증가한 1만큼이 나머지 값이 됨. 배열 전체가 음 일 수 있으므로 배열사이즈+1의 값을 한 것임)
배열사이즈 - 몫 - 나머지 = 0의 개수

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment