我在Quora上偶然发现了这段代码。
#include<stdio.h>
main(){
int $[]
={0x69, 0154,107,
'e',0x79,0157, 117,'v',0x6a}
,_,__;_=__^__;__=_;while(_<(-
(~(1<<3))+3)){(_==1<<1||_==
-(~(1<<3))||_==11)?putchar
(*($+(1>>1))):putchar(*(
__++ +$)),(_==1>>1||
_==1<<2||_==(1<<3
)-1)?putchar
(' '):1;
_++;
}
}
该节目的输出为i like you viji
。它很动人,但很神秘。所以我用indent格式化它以获得更好的主意。
main ()
{
int $[] = { 0x69, 0154, 107,
'e', 0x79, 0157, 117, 'v', 0x6a
}
, _, __;
_ = __ ^ __;
__ = _;
while (_ < (-(~(1 << 3)) + 3))
{
(_ == 1 << 1 || _ ==
-(~(1 << 3)) || _ == 11) ? putchar
(*($ + (1 >> 1))) : putchar (*(__++ + $)), (_ == 1 >> 1 ||
_ == 1 << 2 || _ == (1 << 3) - 1) ? putchar
(' ') : 1;
_++;
}
}
现在它不那么动人,但仍然有点神秘。
那么,任何人都可以解释这段代码如何设法打印i like you viji
吗?
更新
为变量$
,_
和__
以及扩展的三元运算符提供了更好的名称:
int a[] = { 0x69, 0154, 107, 'e', 0x79, 0157, 117, 'v', 0x6a }, x, y;
x = y ^ y;
y = x;
while (x < (-(~(1 << 3)) + 3))
{
if (x == 1 << 1 || x == -(~(1 << 3)) || x == 11)
putchar (*(a + (1 >> 1)));
else
{
putchar (*(y++ + a));
if (x == 1 >> 1 || x == 1 << 2 || x == (1 << 3) - 1)
putchar (' ');
else
1;
}
x++;
}
答案 0 :(得分:15)
重写代码给出:
int arr1[] = { 'i', 'l', 'k', 'e', 'y', 'o', 'u', 'v', 'j'};
int i0, i1; // Moved to new line instead of using ,
i0 = 0; // i0 = i1 ^ i1;
i1 = i0;
while (i0 < 12) // All strange constant like (1<<3) recalculated
{
if (i0 == 2 || i0 == 9 || i0 == 11) // "? :" replaced by if
{
putchar(*arr1);
}
else
{
putchar (*(arr1 + i1));
++i1;
if (i0 == 0 || i0 == 4 || i0 == 7)
{
putchar(' ');
}
}
i0++;
}
1)重新格式化线条,删除不必要的空格,插入空格以便于阅读
main()
{
int $[] = {0x69, 0154,107, 'e',0x79,0157, 117,'v',0x6a} , _, __;
_ = __^__;
__ = _;
while(_ < (-(~(1<<3))+3))
{
(_ == 1<<1 || _ == -(~(1<<3)) || _ == 11) ?
putchar(*($ + (1>>1))) :
putchar(*(__++ +$)), (_ == 1 >> 1 || _ == 1<<2 || _ == (1<<3)-1) ?
putchar(' ') : 1;
_++;
}
}
2)重命名变量,即$到arr1,_到i0和__到i1
main()
{
int arr1[] = {0x69, 0154,107, 'e',0x79,0157, 117,'v',0x6a} , i0, i1;
i0 = i1^i1;
i1 = i0;
while(i0 < (-(~(1<<3))+3))
{
(i0==1<<1 || i0== -(~(1<<3)) || i0 == 11) ?
putchar(*(arr1+(1>>1))) :
putchar(*(i1++ +arr1)), (i0 == 1 >> 1 || i0 == 1<<2 || i0 == (1<<3)-1) ?
putchar(' ') : 1;
i0++;
}
}
3)使用if语句而不是?: 这包括将逗号线分成两行。
main()
{
int arr1[] = {0x69, 0154,107, 'e',0x79,0157, 117,'v',0x6a} , i0, i1;
i0=i1^i1;
i1=i0;
while(i0 < (-(~(1<<3))+3))
{
if (i0 == 1<<1 ||i0== -(~(1<<3)) || i0 == 11)
{
putchar(*(arr1+(1>>1)));
}
else
{
putchar(*(i1++ +arr1));
if (i0 == 1 >> 1 || i0 == 1<<2 || i0 == (1<<3)-1)
{
putchar(' ');
}
else
{
1; // This does nothing so it can be removed
}
}
i0++;
}
}
4)重新计算数字常数以获得更好的值 实例
0x69与'i'
相同1&lt;&lt; 1与2
相同- (〜(1 <&lt; 3))与9
相同i1 ^ i1与0相同
1>&gt; 1与0
相同main()
{
int arr1[] = { 'i', 'l', 'k', 'e', 'y', 'o', 'u', 'v', 'j'} , i0, i1;
i0 = 0;
i1 = i0;
while(i0 < 12)
{
if (i0 == 2 || i0 == 9 || i0 == 11)
{
putchar(*(arr1));
}
else
{
putchar(*(i1++ +arr1));
if (i0 == 0 || i0 == 4 || i0 == 7)
{
putchar(' ');
}
}
i0++;
}
}
5)一些小的最后清理
main()
{
int arr1[] = { 'i', 'l', 'k', 'e', 'y', 'o', 'u', 'v', 'j'}; // Move i0 and
// i1 to nextt line
int i0, i1;
i0 = 0;
i1 = i0;
while(i0 < 12)
{
if (i0 == 2 || i0 == 9 || i0 == 11)
{
putchar(*arr1);
}
else
{
putchar(*(arr1 + i1)); // Splitted into two lines
++i1;
if (i0 == 0 || i0 == 4 || i0 == 7)
{
putchar(' ');
}
}
i0++;
}
}
现在代码很容易阅读。