TOJ1072 outputs the C program itself

Source: Internet
Author: User

Output as its own C program

Crazy FT.

Http://acm.tongji.edu.cn/people/ps/showproblem.php? Problem_id = 1072

ACM has such a question!

So I searched on google, and there was ......

The skills of this type of program are the use of the string definition for the ASCII value 34 and 10, that is, the quotation marks and carriage return characters:

1.
Char * f = "main () {putchar ('C'); putchar ('H'); putchar ('A'); putchar ('R '); putchar ('*'); putchar ('F'); putchar ('='); putchar (34); printf (f); putchar (34); putchar ('; '); putchar (10); printf (f); putchar (10) ;}"; main () {putchar ('C'); putchar ('H '); putchar ('A'); putchar ('R ');
Putchar ('*'); putchar ('F'); putchar ('='); putchar (34); printf (f); putchar (34); putchar ('; '); putchar (10); printf (f); putchar (10 );}

2.
Char * f = "char * f =; main () {f [7] = 0; printf (f); putchar (34); f [7] = ';'; printf (f); putchar (34); f [8] = 0; printf (& f [7]); f [8] = 'M'; putchar (10 ); printf (& f [8]); putchar (10) ;}"; main () {f [7] = 0; printf (f); putchar (34 ); f [7] = '; printf (f); putchar (34); f [8] = 0; printf (& f [7]); f [8] = 'M'; putchar (10); printf (& f [8]); putchar (10 );}

3.
Char * f = "char * f = % c % s % c; % cmain () {printf (f, 34, f, 34,10, 10);} % c ";
Main () {printf (f, 34, f, 34,10, 10 );}

4.
Char a = '"'; char B = '/N'; char c =' // '; char * f =" char a =' % C '; char B = '% cn'; char c =' % c % C'; % cchar * f = % c % s % c; % cmain () {& printf (f, a, c, B, a, f, a, B, B) ;}% c "; main () {printf (f, a, c, c, c, B, a, f, a, B, B );}

5.
Char * lines [] = {"char * lines [] = {", "% c % s % c", "0};", "main () {"," int idx; "," puts (lines [0]); "," for (idx = 0; lines [idx]! = 0; idx ++) {"," printf (lines [1], 34, lines [idx], 34, ',', 10 );",
"}", "Puts (lines [2]);", "for (idx = 3; lines [idx]! = 0; idx ++) {"," puts (lines [idx]); ","} ","} ", 0 };
Main () {int idx; puts (lines [0]); for (idx = 0; lines [idx]! = 0; idx ++) {printf (lines [1], 34, lines [idx], 34, ',', 10);} puts (lines [2]); for (idx = 3; lines [idx]! = 0; idx ++) {puts (lines [idx]) ;}}

I picked the simplest one. After reading this, I modified it:

# Include <stdio. h>
Char * f = "# include <stdio. h> % cchar * f = % c % s % c; % cint main () {printf (f, 10, 34, f, 34,10, 10); return 0 ;} % c ";
Int main () {printf (f, 10, 34, f, 34, 10, 10); return 0 ;}

Haha, AC, but be careful when sticking it to the Submit box. Don't miss the last carriage return.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.