Skip to content

Instantly share code, notes, and snippets.

@YaLTeR
Last active August 29, 2015 14:07
Show Gist options
  • Save YaLTeR/d3abecce6faddad6a663 to your computer and use it in GitHub Desktop.
Save YaLTeR/d3abecce6faddad6a663 to your computer and use it in GitHub Desktop.
Distributes the given score over the given number of days.
#include <algorithm>
#include <iostream>
#include <random>
const int MIN_SCORE_PER_DAY = 5;
const int MAX_SCORE_PER_DAY = 10;
int main()
{
std::random_device rd;
std::mt19937 gen(rd());
while (true)
{
int days, score;
std::cout << "Enter the number of days, followed by the total score: ";
std::cin >> days >> score;
if ((days == 0) && (score == 0))
{
return 0;
}
else if ((days <= 0) || (score <= 0))
{
std::cout << "Error: days and score must be greater than 0!\n\n";
continue;
}
// For .5 score distribution: first multiply the total by two,
// then divide the results by two.
score *= 2;
for (; days; --days)
{
int max_score = days * (MAX_SCORE_PER_DAY * 2),
min_score = days * (MIN_SCORE_PER_DAY * 2);
if (score > max_score)
{
std::cout << "Error: not enough days to distribute the score!";
break;
}
else if (score < min_score)
{
std::cout << "Error: not enough score to distribute over the days!";
break;
}
std::uniform_int_distribution<> distr(
std::max(MIN_SCORE_PER_DAY * 2, score - max_score + MAX_SCORE_PER_DAY * 2),
std::min(MAX_SCORE_PER_DAY * 2, score - min_score + MIN_SCORE_PER_DAY * 2));
int result = distr(gen);
score -= result;
std::cout << (result / 2.0) << " ";
}
std::cout << "\n\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment