Skip to content

Instantly share code, notes, and snippets.

@jesyspa
Forked from sochoa/chocolate_feast.cpp
Last active August 29, 2015 13:57
Show Gist options
  • Save jesyspa/9604833 to your computer and use it in GitHub Desktop.
Save jesyspa/9604833 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
#include <vector>
int main() {
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
for (std::string line; std::getline(std::cin, line);) {
std::istringstream iss(line);
int money, price_per_chocolate, num_wrappers_per_free_piece;
iss >> money >> price_per_chocolate >> num_wrappers_per_free_piece;
if (!iss)
continue;
// Just copy the constraints literally.
if (money < 2 || money > 100'000)
continue;
if (price_per_chocolate < 1 || money < price_per_chocolate)
continue;
if (num_wrappers_per_free_piece < 2 || num_wrappers_per_free_piece > money)
continue;
int num_chocolates = 0;
while (money >= price_per_chocolate) {
money -= price_per_chocolate;
num_chocolates++;
if (num_chocolates % num_wrappers_per_free_piece == 0) {
num_chocolates++;
}
}
std::cout << num_chocolates << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment