스터디사이트 : https://www.hackerrank.com



JAVA 알고리즘 양수, 음수, 0 파악하여 각 갯수를 비율로 출력하기



※ 코딩실습- 조건


배열의 크기를 입력받은 값의 갯수만큼 정의해서 값을 넣는다.

값이 

양수일때

음수일때

0일때 

를 각각 카운트해서, 아래처럼 계산한다.


 (양수 or 음수 or 0일때의 카운트)  /  (입력받은 값의 갯수) 



입력

6

-4 3 -9 0 4 1


출력

0.500000

0.333333

0.166667


코딩 시도 1

System.out.printf() 를 사용해서 소수점 여섯자리로 고정시켜서 출력을 하려 해보았다..

그런데 아무 내용도 출력이 안된다..

이유는,

아래에 나온 %.6f 라는 것은,

float형으로 소수점 6자리를 출력하라는 포멧 지정자인데,

나는 바보같이 int형 변수를 그곳에 넣어버린 것이었다..


# 참고 - JAVA 포멧 지정자 : 

http://goo.gl/wnxXcg


public static void main(String[] args) {

        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */

        Scanner scan = new Scanner(System.in);

        // 입력자리수를 저장하는 변수

        int count = scan.nextInt();

        // 입력자리수 크기를 가진 int배열 정의

        int[] n = new int[count];

        // 음수, 양수, 0 값을 가진 값을 각각 count시키는 int변수 정의

        int positive=0;

        int negative=0;

        int zero=0;

        // 입력자리수만큼 반복하여 처리.

        for(int i=0; i<count; i++){

            n[i] = scan.nextInt();

            System.out.println("n[i] : " + n[i]);

            if(n[i] > 0){

                positive++;

            }else if(n[i] < 0){

                negative++;

            }else{

                zero++;

            } /* end if*/

        } /* end for*/

            System.out.printf("%.6f", positive);

            System.out.printf("%.6f", negative);

            System.out.printf("%.6f", zero);

} /* end main() */

결과 : 

n[i] : -4

n[i] : 3

n[i] : -9

n[i] : 0

n[i] : 4

n[i] : 1




코딩 시도 2

바보같은 실수 한번 했다..

float형으로 형변환을 해서 연산을 해서 잘 출력 되긴 했으나,

칸 내리기를 안했던 것이다. ㅋㅋㅋ


System.out.printf("%.6f", (float)positive/count);

System.out.printf("%.6f", (float)negative/count);

System.out.printf("%.6f", (float)zero/count);

 0.5000000.3333330.166667




코딩 시도 3 & 결과

System.out.printf() 에다가 "%.6f" 에다가 "%.6f\n" 처럼 \n 을 추가하면 된다!!


public static void main(String[] args) {

        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */

        Scanner scan = new Scanner(System.in);

        // 입력자리수를 저장하는 변수

        int count = scan.nextInt();

        // 입력자리수 크기를 가진 int배열 정의

        int[] n = new int[count];

        // 음수, 양수, 0 값을 가진 값을 각각 count시키는 int변수 정의

        int positive=0;

        int negative=0;

        int zero=0;

        // 입력자리수만큼 반복하여 처리.

        for(int i=0; i<count; i++){

            n[i] = scan.nextInt();

            System.out.println("n[i] : " + n[i]);

            if(n[i] > 0){

                positive++;

            }else if(n[i] < 0){

                negative++;

            }else{

                zero++;

            } /* end if*/

        } /* end for*/

            System.out.printf("%.6f\n", (float)positive/count);

            System.out.printf("%.6f\n", (float)negative/count);

            System.out.printf("%.6f\n", (float)zero/count);

} /* end main() */

결과 : 

0.500000

0.333333

0.166667


+ Recent posts