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:
- the code may be in C or C++
- you may consider using either printf or cout to print the program any thing is fine, printf is recommended.
- if you can also print the #include lines.. (honestly I couldn’t).. then you are the man.
- “almost right” answers might be accepted as long as it RUNS and at least prints 95% of the program!
- Anonymous answers wont be approved, at least provide a nick name and don’t use m8 cause its mine!
- 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
….here you go http://rafb.net/p/xg1zCy64.html
We have a first hacker
First Place
and he included the #include line in the printing (Y) π
guys you can use this site “pointed by Mohamed Samy” to get a link for your code, because wordpress destroys the formatting
http://rafb.net/paste/
We have a second hacker
Mohamed Abdelghani
First Place
A small modification on my code
http://rafb.net/p/rwn8sV36.html
As I added a redundant argument in the previous reply
I do it but I need to add two back slashes “\” to the string before main because of the special characters :@ :@ (the double quotation)
http://rafb.net/p/ul7l1w22.html
To Mohamed Abd El-Mon’em (Harry Potter)
first:
replace
while(str) cout..*str++;
with
cout..str;
only .. it will work then you wont need the tmp varsecond:
try reading about Printf because cout is not that cool
if u use printf you will be able to use the same trick you used with the other characters π
lol frankly i searched for that problem π
http://rafb.net/p/ruQjeU63.html
i don’t know if that what u meant or not
but anyhow i got some knowledge π
To Abdalla (Jaqoup)
well, that was new π .. very good solution in deed π .. i would have never thought about it that way π
First Place
Thanx Fouad
I got it with cout, I thought in printf but I found the same problem
(printf(“%c”,char(34));) the double quotations here also , sure their is
a solution with it but I can’t got it.
http://rafb.net/p/ZxYpnJ55.html
Oh .. this time with the # include line
it’s the first time I know that I can put any thing before # include :d
http://rafb.net/p/I91Lau80.html
To Mohamed Abd El-Monβem (Harry Potter)
Man, that is very very good (Y)
thanks
First Place
I wished I’ve seen it earlier .. LOL
http://rafb.net/p/V8trzY62.html
mesh yenfa3 deh ??
#define STR(str) cout<<#str
we a7ot kol el code fe STR ……ana faker eny shoft el so2al dah fe site 2abl keda bas mesh kont fehemt el 7al
ahoh el 7al ely kan 3ala el site ….bas bardo mesh fahmo π π
http://www.cprogramming.com/challenges/solutions/self_print.html
ah sa7e7 ……momken tab3an a3mel keda a7san π π π
#define STR(str) str cout<<#str
b7es en el code yetnafez π π
#include
using namespace std;
#define STR(str)\
cout <<“int main(){\n” << #str<<“\n}”;\
str
int main()
{
STR(
cout << “this is koko program”;
return 0;)
}
http://rafb.net/p/IihaWI32.html
http://rafb.net/p/ZdynS423.html
Question:
should the output be formatted the same as i wrote in the code ?
i mean the new lines and so on ..
To Tasniem
if you can .. then it would be great .. but we can always write the code in one line π so it matches the output .. can’t we ? π
..
waiting for your solution ..
we have other three winners π
AlaaShaker (wait till you see his solution) π
Hatem Abdelghani
and a 97% right solution form Tecnoyotta which actually is the first to use this method π
thanks guys
we have another young winner ..
Tasniem .. great job (Y)
Pingback: The Shortest.. Solved Again [Problem of the Week] « more about the 8