输出为本身的C程序
狂FT.
http://acm.tongji.edu.cn/people/ps/showproblem.php?problem_id=1072
ACM竟然有这样的题目!
于是到google上搜了一下,果然有……
这类程序的技巧在于字符串定义时对ASCII为34和10即引号和回车符的妙用:
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,c,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%c%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]);}}
挑了其中最简单的一个,看懂以后,修改了一下:
#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;}
呵呵,AC了,不过粘到Submit框里的时候还是要小心,别把最后一个回车漏掉。