Skip to content

Instantly share code, notes, and snippets.

@magicsih
Last active May 30, 2017 07:21
Show Gist options
  • Save magicsih/e4058701292bc1d74a69b532361f0676 to your computer and use it in GitHub Desktop.
Save magicsih/e4058701292bc1d74a69b532361f0676 to your computer and use it in GitHub Desktop.
Algorithm: Dog and chick
//============================================================================
// Name : DogAndChick.cpp
// Author : magicsih
// Version :
// Copyright :
// Description : How many dogs and chicks in C++, Ansi-style
//============================================================================
#include <iostream>
#include <fstream>
using namespace std;
int main() {
std::ifstream fin;
fin.open("input.txt");
int sumOfAnimals, sumOfLegs;
char buf[5];
string result;
while(fin >> sumOfAnimals >> sumOfLegs) {
if(sumOfAnimals == 0 && sumOfLegs == 0) {
break;
}
if(sumOfAnimals > 1000 || sumOfLegs > 4000) {
result.append("INPUT ERROR!\n");
continue;
}
int dogs = (sumOfLegs - 2 * sumOfAnimals) / 2;
int cheeks = sumOfAnimals - dogs;
if(dogs < 0 || cheeks < 0) {
result.append("0\n");
continue;
}
_itoa_s(dogs, buf, 10);
result.append(buf);
result.append(" ");
_itoa_s(cheeks, buf, 10);
result.append(buf);
result.append("\n");
}
fin.close();
std::ofstream fout;
fout.open("output.txt");
fout << result.c_str();
fout.close();
return 0;
}
@magicsih
Copy link
Author

input

25 80
15 10
1500 2000
0 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment