[Hacker Rank] PlusMinus

HyunDong Lee·2021년 1월 6일
0

Preparing For CodingTest

목록 보기
1/22
post-thumbnail

_우연히 코딩테스트 준비를 하다가 해커랭크라는 사이트에 접하게 되었다. 하루에 쉬운 문제는 많게는 5개씩, 어려운 문제는 분석 및 블로그 업로그까지 2 ~ 3개 까지만 진행 해보려고 한다.

PlusMinus

Print the ratios of positive, negative and zero values in the array. Each value should be printed on a separate line with digits after the decimal. The function should not return a value.

Input:
5
1 1 -1 0 -2
output:
0.500000
0.333333
0.166667

문제 접근 방식

그냥 기본문제 풀이이기 때문에 특별한 방식은 없었다..!

#include <bits/stdc++.h>
#include <iostream>
#include <vector>

using namespace std;

vector<string> split_string(string);

// Complete the plusMinus function below.
void plusMinus(vector<int> arr) {

    int n; cin >> n;
    int number;
    vector<int> num;
    vector<double> proportion;
    proportion.push_back(0);
    proportion.push_back(0);
    proportion.push_back(0);
    
    for(int i = 0;i < arr.size();i++){
        if(arr[i] > 0) proportion[0]++;
        else if(arr[i] < 0) proportion[1]++;
        else if(arr[i] == 0) proportion[2]++;
    }
       
    proportion[0] = proportion[0]/arr.size();    
    proportion[1] = proportion[1]/arr.size();    
    proportion[2] = proportion[2]/arr.size();    
    for(int i = 0;i < 3;i++){
        cout << fixed;
        cout.precision(6);
        cout << proportion[i] << endl;
    }

}

0개의 댓글