[HackerRank] plusminus

jh Seo·2024년 2월 4일
0

HackerRank

목록 보기
1/15

개요

HackerRank: PlusMinus

Given an array of integers, calculate the ratios of its elements that are positive, negative, and zero. Print the decimal value of each fraction on a new line with places after the decimal.

접근 방식

음수, 0, 양수의 비율을 구하는 문제로 비율은 나눗셈 연산이라 쉽지만
소숫점 아래 자리를 고정하는 걸 remind하는 문제다.

cout<<fixed;
cout.precision(6);

를 사용했다.

전체 코드

void plusMinus(vector<int> arr) {
    float minus=0, zero=0,plus=0;
    int total=arr.size();
    sort(arr.begin(),arr.end());
    for(int elem : arr){
        if(elem<0)minus++;
        else if(elem == 0) zero++;
        else plus++;
    }
    cout<<fixed;
    cout.precision(6);
    cout<<plus/total<<"\n"<<minus/total<<"\n"<<zero/total;
}
profile
코딩 창고!

0개의 댓글