Question:
Salute to all dummies and gurus, developers and freelancers, progers and coders! But this is not about the classification and hierarchy of our brother. It's about an Olympiad problem that I haven't been able to solve for a week now. I know for sure: the solution is something simple and ingenious – but what kind of stone did it hide under? So, harsh hashcode users, I ask you for help in solving this problem!
Task: Programming language: С++. The program should have 1 single output function that can only write, i.e. cannot delete text, but only insert new one. However, it works in the following way
Executable code:
func( "1" );
Conclusion:
(1)
[1]
Executable code:
func( "1" );
func( "2" );
Conclusion
(1)
(2)
[2]
[1]
Executable code:
func( "1" );
func( "2" );
func( "qwer" );
Conclusion
(1)
(2)
(qwer)
[qwer]
[2]
[1]
I'm sure the problem is easy to solve, but I have no other options than to still buffer the tail, change it, and replace the old tail with the new one. But then you have to use deletion of characters.
PS The task is not quite an Olympiad, this task was long ago in some powerful article on alternative programming where several examples were considered, but I don’t remember the code for implementing such a task. And I can't find the article either. At most without deleting characters, it is impossible to write.
Answer:
Executable code:
func("1"); func("2"); func("qwer");
Conclusion
(1) (2) (qwer) [qwer] [2] [1]
that is, you need to print in the usual and reverse order????
as I understand it, you can only call the same function … (if so, then the solution is below)
- make a
func
class and store std::vector<std::string>
in it - make the func(string) constructor with the duplicate written to the vector and displayed on the screen
- in the destructor, output the vector in reverse order
UPD:: well, it will also be "nice" to overload "operator ()" so as not to spoil the constructors