-
Compare the Triplets해커랭크(HackerRank) 2018. 8. 19. 17:49반응형
1. 문제
2. 알고리즘
키워드 - 구현
* 회고
문제가 조금 이상 함.
A, B 배열에 원소에서 크기를 비교 하여 A 가 크면 alice 에 1 포인트를 적산하고
B 가 크면 Bob 에게 1 포인트를 적산 하여 출력
3. 코드
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107#include <bits/stdc++.h>using namespace std;vector<string> split_string(string);/** Complete the solve function below.*/vector<int> solve(int a0, int a1, int a2, int b0, int b1, int b2) {/** Write your code here.*/vector<int> arr, brr, crr;arr.push_back(a0);arr.push_back(a1);arr.push_back(a2);brr.push_back(b0);brr.push_back(b1);brr.push_back(b2);int aWin = 0;int bWin = 0;for(int i = 0; i< 3; i ++) {if(arr[i] > brr[i])aWin++;if(arr[i] < brr[i])bWin++;}crr.push_back(aWin);crr.push_back(bWin);return crr;}int main(){ofstream fout(getenv("OUTPUT_PATH"));string a0A1A2_temp;getline(cin, a0A1A2_temp);vector<string> a0A1A2 = split_string(a0A1A2_temp);int a0 = stoi(a0A1A2[0]);int a1 = stoi(a0A1A2[1]);int a2 = stoi(a0A1A2[2]);string b0B1B2_temp;getline(cin, b0B1B2_temp);vector<string> b0B1B2 = split_string(b0B1B2_temp);int b0 = stoi(b0B1B2[0]);int b1 = stoi(b0B1B2[1]);int b2 = stoi(b0B1B2[2]);vector<int> result = solve(a0, a1, a2, b0, b1, b2);for (int result_itr = 0; result_itr < result.size(); result_itr++) {fout << result[result_itr];if (result_itr != result.size() - 1) {fout << " ";}}fout << "\n";fout.close();return 0;}vector<string> split_string(string input_string) {string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {return x == y and x == ' ';});input_string.erase(new_end, input_string.end());while (input_string[input_string.length() - 1] == ' ') {input_string.pop_back();}vector<string> splits;char delimiter = ' ';size_t i = 0;size_t pos = input_string.find(delimiter);while (pos != string::npos) {splits.push_back(input_string.substr(i, pos - i));i = pos + 1;pos = input_string.find(delimiter, i);}splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));return splits;}cs 반응형'해커랭크(HackerRank)' 카테고리의 다른 글
Left Rotation (0) 2019.03.12 Simple Array Sum (0) 2018.08.19 A Very Big Sum (0) 2018.08.19 Diagonal Difference (0) 2018.08.19 Plus Minus (0) 2018.08.19