# Fun C++ Problem

Show 50 post(s) from this thread on one page
Page 2 of 5 First 12345 Last
• October 18th, 2006, 09:05 AM
NMTop40
Re: Fun C++ Problem
I have 3 solutions. Took me a few minutes. Didn't actually time it.
• October 18th, 2006, 09:43 AM
Paul McKenzie
Re: Fun C++ Problem
I have all three.

Regards,

Paul McKenzie
• October 18th, 2006, 09:57 AM
exterminator
Re: Fun C++ Problem
So I ask dcjr84, do we bring the solutions in open or do you want some more time? :D

I can't hold any longer... I will put them all in my next post here ... :D :D :D ;)
• October 18th, 2006, 10:02 AM
miteshpandey
Re: Fun C++ Problem

Is replacing the same character twice valid?
• October 18th, 2006, 10:02 AM
laitinen
Re: Fun C++ Problem
me neither, I only get 2!
• October 18th, 2006, 10:07 AM
cilu
Re: Fun C++ Problem
Quote:

Originally Posted by laitinen
me neither, I only get 2!

Try to think outside the box. ;)
• October 18th, 2006, 10:10 AM
Paul McKenzie
Re: Fun C++ Problem
A hint to the third solution, if it's the one I think some here are having a problem with, is "do the math".

Regards,

Paul McKenzie
• October 18th, 2006, 10:14 AM
miteshpandey
Re: Fun C++ Problem
Now, I think I have three. Not sure unless someone posts the solutions :D :D hehe.....
• October 18th, 2006, 10:34 AM
Andrea_Rossini
Re: Fun C++ Problem
got 2.......
• October 18th, 2006, 10:38 AM
Andrea_Rossini
Re: Fun C++ Problem
got 3 ! !
• October 18th, 2006, 10:48 AM
sszd
Re: Fun C++ Problem
Got all three!!! Took me a few seconds to get the first one. Then, my mind went completely blank (started thinking too hard for some difficult trick) and it took me almost 20 minutes to get the second:o. But then, following that, I almost immediately got the third:D.
• October 18th, 2006, 11:04 AM
Andrea_Rossini
Re: Fun C++ Problem
Got a 4th solution......
but it's a cheat based on solution 1.........
and works wrong too :lol:
• October 18th, 2006, 11:59 AM
ranqin
Re: Fun C++ Problem
Got two in a few minutes. But the third one took me 30 minutes!!!
• October 18th, 2006, 12:13 PM
RoboTact
Re: Fun C++ Problem
Code:

```#include <windows.h> #include <limits> #include <iostream> #include <fstream> #include <string> using namespace std; bool exists(const char* fname) {     ifstream fin(fname);     return fin.is_open(); } bool check_out(const char* fname) {     ifstream out(fname);     for(int i=0;i<20;i++)     {         int c = out.get();         if(c!='x')             return false;         c = out.get();         if(c!='\n')             return false;     }     int end = out.get();     if(!out.eof()) return false;     return true; } void run_with_timeout(char* cmdline) {     PROCESS_INFORMATION info;     ZeroMemory(&info,sizeof(info));     STARTUPINFO si;     ZeroMemory(&si,sizeof(si));     if(!CreateProcess(0,cmdline,0,0,0,DETACHED_PROCESS,0,0,&si,&info))         return;     WaitForSingleObject(info.hProcess,1000);     TerminateProcess(info.hProcess, 1); } bool test_code(const char* code) {     const char* src_name = "test_code.cpp";     ofstream src(src_name);     src<<"#include <fstream>\n";     src<<"#include <iostream>\n";     src<<"using std::endl;\n";     src<<"int main() { \n";     src<<"std::ofstream cout(\"out.txt\");\n";     src<<code;     src<<"}\n";     src.close();     if(exists("a.exe"))         system("del a.exe");     string cmd = string() + "g++ " + src_name + " 2> log.txt";     system(cmd.c_str());     if(!exists("a.exe"))         return false;         run_with_timeout("a.exe");     bool res = check_out("out.txt");     if(exists("out.txt"))         system("del out.txt");     return res; } void log_success(ostream& str, const char* code, int i, char c) {     str<<endl;     str<<c<<" at "<<i<<":"<<endl;     str<<code;     str<<endl; } int main() {     char code[] =         "int i, n = 20;\n"         "for (i=0; i<n; i--)\n"         "{\n"         "cout << \"x\" << endl;\n"         "}\n";     ofstream success_log("success_log.txt");     for(int i=0, n=strlen(code);i<n;i++)     {         cout<<i<<" of "<<n<<"..."<<endl;         for(char c = 32;c<127;c++)         {             cout<<c;             char old = code[i];             code[i] = c;             if(test_code(code))             {                 log_success(success_log, code, i, c);                 log_success(cout, code, i, c);             }             code[i] = old;         }         cout<<endl;     }     return 0; }```
:rolleyes:

Edit: v0.02 <- bugfix
• October 18th, 2006, 12:39 PM
exterminator
Re: Fun C++ Problem
OMG!!! :eek:

RoboTact, for how long did you not have a good sleep? It was just a small puzzle.. :eek: :D
Show 50 post(s) from this thread on one page
Page 2 of 5 First 12345 Last