Initial commit.

This commit is contained in:
David Vereb 2020-12-03 11:43:51 -05:00
commit a7280c44a3
3 changed files with 86 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*~
a.out

2
Makefile Normal file
View File

@ -0,0 +1,2 @@
a.out: main.cpp
clang++ -O0 -g -std=c++14 main.cpp

82
main.cpp Normal file
View File

@ -0,0 +1,82 @@
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib> // atoi
class Rate
{
public:
Rate(int rate)
: base_rate(rate), min_rate(rate)
{ }
/*
* This is the rate given to you by the user
*/
int BaseRate() const { return base_rate; }
/*
* This is the least common multiple when compared to all the other rates
*/
int MinimumRate() const { return min_rate; }
/*
* This tells you how many you'd need to build to achieve the minimum rate
*/
int QuantityToBuild() const
{
// TODO(dev): assert !(min_rate % rate), otherwise this shit's broken or not yet calculated
return min_rate / base_rate;
}
/*
* This is used to step up to the next rate
*/
void Increment() { min_rate += base_rate; }
private:
const int base_rate;
int min_rate; // needs to be incremented/calculated in the program loop
};
int main(int argc, char *argv[])
{
if(argc < 3)
{
std::cout << "Usage: ./" << argv[0] << " rate1 rate2 [...] rateN" << std::endl;
return -1;
}
std::vector<Rate> rates;
// Add all command line parameters (rates) to the vector:
for(auto i = 1; i < argc; ++i)
rates.push_back(Rate(atoi(argv[i])));
// NOTE(dev): Run through all (2+) rates to determine the least common multiple.
// Increment each rate until they all come out to the same value
bool difference_found;
do
{
difference_found = false;
for(auto &rate : rates)
{
for(auto &other : rates) // sure it checks against itself, but this isn't optimized sooo
{
if(rate.MinimumRate() < other.MinimumRate())
{
difference_found = true;
while(rate.MinimumRate() < other.MinimumRate())
rate.Increment();
}
}
}
} while(difference_found);
for(auto rate : rates)
std::cout << rate.QuantityToBuild() << " * " << rate.BaseRate() << " == "
<< rate.MinimumRate() << std::endl;
return 0;
}