Training/HackerRank

[C++][Warmup] Mini-Max Sum

FATKITTY 2020. 8. 3. 03:23
반응형

https://www.hackerrank.com/challenges/mini-max-sum/problem

 

 

#include <bits/stdc++.h>

using namespace std;

vector<string> split_string(string);

// Complete the miniMaxSum function below.
void miniMaxSum(vector<int> arr) {
    int arrsize = 5;
    long totalsum = 0, max = 0, min = 0;
    vector<long> sum(5);
    
    for (int i = 0; i < arrsize; i++) totalsum += arr[i];
    for (int j = 0; j < arrsize; j++) sum[j] = totalsum - arr[j];
    
    sort(sum.begin(), sum.end());
    min = sum.front(); max = sum.back();

    cout << min << ' ' << max << endl;
}

int main()
{
    string arr_temp_temp;
    getline(cin, arr_temp_temp);

    vector<string> arr_temp = split_string(arr_temp_temp);

    vector<int> arr(5);

    for (int i = 0; i < 5; i++) {
        int arr_item = stoi(arr_temp[i]);

        arr[i] = arr_item;
    }

    miniMaxSum(arr);

    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;
}

 

Testcase

Input

256741038 623958417 467905213 714532089 938071625

Output
2063136757 2744467344

 

반응형