Skip to content

Instantly share code, notes, and snippets.

@peaceman
Created April 13, 2011 07:00
Show Gist options
  • Save peaceman/917099 to your computer and use it in GitHub Desktop.
Save peaceman/917099 to your computer and use it in GitHub Desktop.
/*
* File: Main.cpp
* Author: peaceman
*
* Created on 13. April 2011, 07:55
*/
#include <conio.h>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <iterator>
template <typename T>
std::string toStr(T value) {
std::stringstream convert;
convert << value;
return convert.str();
}
int strToInt(std::string value) {
int returnValue;
std::stringstream convert;
convert << value;
convert >> returnValue;
return returnValue;
}
std::vector<int> getResult(std::vector<int> itemPrizes, int credit) {
std::vector<int>::iterator firstIter, secondIter;
for (firstIter = itemPrizes.begin(); firstIter < itemPrizes.end(); firstIter++) {
for (secondIter = itemPrizes.begin(); secondIter < itemPrizes.end(); secondIter++) {
if (*firstIter + *secondIter == credit && firstIter != secondIter) {
std::vector<int> result;
result.push_back(firstIter - itemPrizes.begin() + 1);
result.push_back(secondIter - itemPrizes.begin() + 1);
std::sort(result.begin(), result.end());
return result;
}
}
}
}
int main(int argc, char** argv) {
std::ifstream inputFile("C:/input.txt", std::fstream::in);
std::ofstream outputFile("C:/output.txt", std::fstream::out);
if (inputFile.fail() || outputFile.fail()) {
std::cout << "Konnte eine der beiden Dateien nicht oeffnen" << std::endl;
getch();
exit(1);
}
int nrOfTestCases = 0;
std::string actualLine;
std::getline(inputFile, actualLine);
nrOfTestCases = strToInt(actualLine);
for (int i = 0; i < nrOfTestCases; i++) {
int credit, nrOfItems;
std::vector<int> itemPrizes;
//read credit
std::getline(inputFile, actualLine);
credit = strToInt(actualLine);
//read nrOfItems
std::getline(inputFile, actualLine);
nrOfItems = strToInt(actualLine);
//read itemPrizes
std::getline(inputFile, actualLine);
std::string token;
std::stringstream ss(actualLine);
while (std::getline(ss, token, ' ')) {
itemPrizes.push_back(strToInt(token));
}
std::vector<int> result = getResult(itemPrizes, credit);
outputFile << "Case #" << i + 1 << ": " << result.at(0) << " " << result.at(1) << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment