Skip to content

Instantly share code, notes, and snippets.

@dillmo
Created March 29, 2014 00:03
Show Gist options
  • Save dillmo/9845561 to your computer and use it in GitHub Desktop.
Save dillmo/9845561 to your computer and use it in GitHub Desktop.
Solve the Global Debt Code Challeng solution
#include<iostream>
#include<cstring>
#include<vector>
#include<sstream>
#include<cstdlib>
using namespace std;
int main() {
vector<string> input, countries, output;
vector<double> results;
string last_val;
int i, j, k;
cout << "Input\n";
do {
getline(cin, last_val);
if(last_val != "") {
input.push_back(last_val);
countries.push_back(last_val.substr(0, 2));
}
} while(last_val != "");
for(j = 0; j < countries.size(); j++) {
results.push_back(0);
for(k = 0; k < input.size(); k++) {
if(input[k].substr(0, 2) == countries[j]) {
results[j] += atof(input[k].substr((input[k].find(countries[j]) + 3),
(input[k].find(',', input[k].find(countries[j])) -
input[k].find(countries[j]))).c_str());
} else {
results[j] += atof(input[k].substr((input[k].find(countries[j], 3) + 3),
(input[k].find(',', input[k].find(countries[j])) -
input[k].find(countries[j]))).c_str());
}
}
}
for(j = 0; j < input.size(); j++) {
for(k = 0; k < countries.size(); k++) {
if(input[j].substr(0, 2) != countries[k]) {
results[j] -= atof(input[j].substr((input[j].find(countries[k]) + 3),
(input[j].find(',', input[k].find(countries[k])) -
input[j].find(countries[j]))).c_str());
}
}
}
for(i = 0; i < countries.size(); i++) {
stringstream strstream;
strstream << countries[i] << ":" << results[i];
output.push_back(strstream.str().c_str());
}
cout << "Output:\n";
for(i = 0; i < output.size(); i++) {
cout << output[i] << '\n';
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment