Monthly Archives: September 2008

The Shortest.. Solved Again [Problem of the Week]

it’s time to show you the solutions submitted to this problem..

I have heard about this problem once from Abdalla Gamal, The acm Guru that taught us every thing we know about acming.. then i found it later mentioned in a book

i recommend that you copy all the code in a cpp, comment it, then un-comment and test each one alone.

so .. first there were solutions by Mohamed Samy, Mohamed Adbelghani, Hatem Adbelghani and Tasniem they used Printf

//m samy
#include <stdio.h>
int main(){char q='"',nl='n',bs='\',p='%';char *s="#include<stdio.h>%cint main(){char q='%c',nl='%cn',bs='%c%c',p='%c';char *s=%c%s%c;printf(s,nl,q,bs,bs,bs,p,q,s,q);}";printf(s,nl,q,bs,bs,bs,p,q,s,q);}

//m adbelghani
#include<stdio.h>
char* printedCode="#include<stdio.h>%cchar* printedCode= %c%s%c;%cvoid main()%c{%cprintf(printedCode,10,34,printedCode,34,10,10,10,10);%c}";
void main()
{
    printf(printedCode,10,34,printedCode,34,10,10,10,10);
}

//hatem
#include<stdio.h>
char prog[]="void main(){printf(_char prog[]=%c%s%c;_,34,prog,34);for(char*s=prog;*s;s++)*s=(*s==95?34:*s);printf(_%s_,prog);}";void main(){printf("char prog[]=%c%s%c;",34,prog,34);for(char*s=prog;*s;s++)*s=(*s==95?34:*s);printf("%s",prog);}

//tasniem
#include <stdio.h>
char *myProg = "#include<stdio.h>%cchar *myProg=%c%s%c;%cvoid main(){printf(myProg,10,34,myProg,34,10,10);}%c";
void main(){printf(myProg,10,34,myProg,34,10,10);}

Then there was Jaqoup and Tecno who both used Macros

//jaqoup
#include <iostream>
using namespace std;
#define ENIUQ(TEMPLATE) cout << TEMPLATE << "(" << #TEMPLATE << ");n}";

void main()
{
    ENIUQ("#include <iostream>nusing namespace std; n#define ENIUQ(TEMPLATE) cout << TEMPLATE <<  "("  << #TEMPLATE << ");\n}; n nvoid main() n{n   ENIUQ");
}

//tecno
#include <iostream>
using namespace std;

#define STR(str)
    cout <<"int main(){nSTR(" << #str<<")n}";
    str
int main()
{
    STR(
        cout << "this is test program";
    return 0; )
}

Tecno’s Solution although not 100% accurate.. is very elegant.

Then a solution by Mohamed Abd El-Mon’em (Harry Potter), he used cout with no Macros which is why i like his solution alot.

//harry potter
char *str="#include <iostream> using namespace std; int main(){cout<<char(99)<<char(104)<<char(97)<<char(114)<<char(32)<<char(42)<<char(115)<<char(116)<<char(114)<<char(61)<<char(34)<<str<<char(34)<<char(59)<<str;}";
#include <iostream>
using namespace std;

int main()
{
    cout<<char(99)<<char(104)<<char(97)<<char(114)<<char(32)<<char(42)<<char(115)<<char(116)<<char(114)<<char(61)<<char(34)<<str<<char(34)<<char(59)<<str;
}

Then i want you to take a look at another two solutions by Alaa Shaker and Tasniem..

//AlaaShaker
#include <iostream>
using namespace std;

int main()
{
    // Write ANYTHING .. ANYWHERE!
    FILE *file = fopen(__FILE__, "r");

    if (!file) return 1;

    for(int ch = fgetc(file); ch != EOF; ch = fgetc(file))
        printf ("%c", ch);
    printf("n");

    fclose(file);

    // Thanks to Abdallah Gamal .. LOL
    return 0;
}


//Tasniem
#include<iostream>
using namespace std;
#include<fstream>

void main ()
{
    ifstream fin(__FILE__);
    char a;
    while( !fin.eof())
    {
        fin.get( a );
        cout<<a;
    }
}

.. shocking .. hah ?! 😀

.. both though totally outside the box.. simple, straight forward .. and even prints comments as they said 😀

Thanks to every one.. 😀 .. i hope you liked that problem

The Shortest (again!) [Problem of the Week]

Once that a blogger says “i am too busy to post and i will start posting regularly once i am free” he never actually becomes free and never it happens that you see a post in his blog again..

so i wont say that .. but since i am busy a bit .. i will leave you with a strange problem this time.. famous among hackers.. its about..

The Shortest Program That Can Print It Self

..ignoring header files..

so lets say you write this program

#include <stdio.h>void main(){}

your program must print to the console this

void main(){} press any key to exit..
 
and off course .. any other letter you add to the code (e.g. the printing code inside the main) must be printed also along with the code
 
Rules and Hints:
  1. the code may be in C or C++
  2. you may consider using either printf or cout to print the program any thing is fine, printf is recommended.
  3. if you can also print the #include lines.. (honestly I couldn’t).. then you are the man.
  4. “almost right” answers might be accepted as long as it RUNS and at least prints 95% of the program!
  5. Anonymous answers wont be approved, at least provide a nick name and don’t use m8 cause its mine!
  6. if you still don’t get the problem, try writing a program that prints “int main(){}” and then look at your code .. and then try to add what you added to the printed “int main(){}” and you will get the problem.
    in other words .. i need to copy the console output and place it in a cpp and run it and then take its output place it in a cpp and .. bla bla