Created
July 31, 2017 23:44
-
-
Save TheBuzzSaw/b86a33a0ef913d12efa5fef1bd64bfba to your computer and use it in GitHub Desktop.
FizzBuzz with minimized redundant code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using namespace std; | |
| struct Pair | |
| { | |
| int multiple; | |
| const char* word; | |
| }; | |
| int main(int argc, char** argv) | |
| { | |
| Pair pairs[] = { | |
| {3, "Fizz"}, | |
| {5, "Buzz"}, | |
| {7, "Baz"}}; | |
| for (int i = 1; i <= 100; ++i) | |
| { | |
| bool outputValue = true; | |
| for (auto pair : pairs) | |
| { | |
| if (!(i % pair.multiple)) | |
| { | |
| cout << pair.word; | |
| outputValue = false; | |
| } | |
| } | |
| if (outputValue) cout << i; | |
| cout << '\n'; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment