Advertisement
VergeoPaw

Vergeo Valentino Gunawan 9.A - HackerRank (Compare the Triplets)

Mar 5th, 2021 (edited)
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. string ltrim(const string &);
  6. string rtrim(const string &);
  7. vector<string> split(const string &);
  8.  
  9. // Complete the compareTriplets function below.
  10. vector<int> compareTriplets(vector<int> a, vector<int> b) {
  11.     vector<int> Win(2);
  12.     int j;
  13.     Win[0] = 0;
  14.     Win[1] = 0;
  15.     for (j =0; j < 3;j+=1) {
  16.         if (a[j] > b[j]) {
  17.             Win[0] = Win[0] +1;
  18.         } else if (a[j] < b[j]){
  19.             Win[1] = Win[1] + 1;
  20.         }
  21.     }
  22.     return Win;
  23.  
  24. }
  25.  
  26. int main()
  27. {
  28.     ofstream fout(getenv("OUTPUT_PATH"));
  29.  
  30.     string a_temp_temp;
  31.     getline(cin, a_temp_temp);
  32.  
  33.     vector<string> a_temp = split(rtrim(a_temp_temp));
  34.  
  35.     vector<int> a(3);
  36.  
  37.     for (int i = 0; i < 3; i++) {
  38.         int a_item = stoi(a_temp[i]);
  39.  
  40.         a[i] = a_item;
  41.     }
  42.  
  43.     string b_temp_temp;
  44.     getline(cin, b_temp_temp);
  45.  
  46.     vector<string> b_temp = split(rtrim(b_temp_temp));
  47.  
  48.     vector<int> b(3);
  49.  
  50.     for (int i = 0; i < 3; i++) {
  51.         int b_item = stoi(b_temp[i]);
  52.  
  53.         b[i] = b_item;
  54.     }
  55.  
  56.     vector<int> result = compareTriplets(a, b);
  57.  
  58.     for (int i = 0; i < result.size(); i++) {
  59.         fout << result[i];
  60.  
  61.         if (i != result.size() - 1) {
  62.             fout << " ";
  63.         }
  64.     }
  65.  
  66.     fout << "\n";
  67.  
  68.     fout.close();
  69.  
  70.     return 0;
  71. }
  72.  
  73. string ltrim(const string &str) {
  74.     string s(str);
  75.  
  76.     s.erase(
  77.         s.begin(),
  78.         find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
  79.     );
  80.  
  81.     return s;
  82. }
  83.  
  84. string rtrim(const string &str) {
  85.     string s(str);
  86.  
  87.     s.erase(
  88.         find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
  89.         s.end()
  90.     );
  91.  
  92.     return s;
  93. }
  94.  
  95. vector<string> split(const string &str) {
  96.     vector<string> tokens;
  97.  
  98.     string::size_type start = 0;
  99.     string::size_type end = 0;
  100.  
  101.     while ((end = str.find(" ", start)) != string::npos) {
  102.         tokens.push_back(str.substr(start, end - start));
  103.  
  104.         start = end + 1;
  105.     }
  106.  
  107.     tokens.push_back(str.substr(start));
  108.  
  109.     return tokens;
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement