When applying for a job as a programmer, I encountered an interesting problem with the following content:
“Write a program that displays numbers from 1 to 100. In this case, instead of numbers that are multiples of three, the program should display the word Fizz, and instead of numbers that are multiples of five, the word Buzz. If the number is a multiple of fifteen, the program should display the word FizzBuzz. The task may seem obvious, but you need to get the most simple and beautiful solution. "
By the simplest and most beautiful solution, I mean the most compact algorithm that uses the least amount of conditional operators and comparison operations. Such an algorithm will have the smallest temporal complexity, and we will strive for it.
')
Apparently, under the obvious solution is supposed to use in the cycle of four conditional statements. The first one checks the multiplicity of the number three, the second five, the third fifteen, and the fourth one displays the number in case of non-fulfillment of the first three conditions. However, after the first reading of the task, there is a desire to get rid of the condition of checking for multiplicity of fifteen, since the words Fizz and Buzz are chosen in such a way that, if the conditions of multiplicity three and five are fulfilled simultaneously, FizzBuzz can be derived. So you can do with three conditions.
#include <iostream> using namespace std; int main() { for(int i=1;i<101;i++) { if(i%3==0) cout<<"Fizz"; if(i%5==0) cout<<"Buzz"; if(i%3!=0 && i%5!=0) cout<<i; cout<<endl; } }
This code has already three conditional operators and four comparison operations.
This task is good because its formulation makes you think about the algorithm that displays the text on the screen when the multiplicity conditions are met, in order to get rid of the excess condition for outputting FizzBuzz. But this path is wrong, and going through it to reduce the number of conditions is very difficult, if at all possible.
When all the ideas based on the previous method end, the idea of using strings comes to mind. For example, using strings, you can get rid of one comparison operation.
#include <iostream> #include <string> using namespace std; int main() { for(int i=1;i<101;i++) { string str=""; if(i%3==0) str="Fizz"; if(i%5==0) str+="Buzz"; if(str=="") str=to_string(i); cout<<str+'\n'; } }
This solution to the problem is the most common on the network, but in my opinion the solution is still lacking elegance and simple, and this is what we should strive for according to the condition of the problem.
But what if you try not to add the necessary text to the line, but rather to remove the text from the “Number of FizzBuzz” line?
#include <iostream> #include <string> using namespace std; int main() { for(int i=1;i<101;i++) { string n=to_string(i), f = "Fizz", b="Buzz"; if(i%3!=0) f=""; else n=""; if(i%5!=0) b=""; else n=""; cout<<n+f+b+"\n"; } }
The resulting algorithm contains only two comparison operations and two conditional operators.
I have not found similar methods for solving this problem in the network. I hope this material will be able to help someone in finding employment or in solving similar problems.