给定生成印度地图的混淆代码,请说明其工作方式。
以下代码在执行后会生成印度地图-
#include "stdio.h"
int main()
{
int a, b, c;
for (b=c=10; a="Hello!Welcome to GeeksForGeeks.\
TFy!QJu ROo TNn(ROo)SLq SLq ULo+\
UHs UJq TNn*RPn/QPbEWS_JSWQAIJO^\
NBELPeHBFHT}TnALVlBLOFAkHFOuFETp\
HCStHAUFAgcEAelclcn^r^r\\tZvYxXy\
T|S~Pn SPm SOn TNn ULo0ULo#ULo-W\
Hq!WFs XDt!" [b+++21]; )
for (; a-- > 64 ; )
putchar ( ++c=='Z' ? c = c/ 9:33^b&1);
return 0;
}
上面的代码是混淆代码(即人类难以理解的代码)的典型示例。
它是如何工作的?
基本上,字符串是印度地图的行程编码。字符串中的交替字符存储连续绘制空格的次数,以及连续绘制感叹号的次数。
这是对该程序不同元素的分析–
编码的字符串
"Hello!Welcome to GeeksForGeeks."
"TFy!QJu ROo TNn(ROo)SLq SLq ULo+UHs UJq TNn*RPn/QPbEWS_JSWQAIJO^NBELPeHBFHT}TnALVlBL"
"OFAkHFOuFETpHCStHAUFAgcEAelclcn^r^r\\tZvYxXyT|S~Pn SPm SOn TNn ULo0ULo#ULo-WHq!WFs XDt!";
注意[b +++ 21]在编码字符串的末尾。由于b +++ 21等于(b ++ + 21),其结果为31(10 + 21),因此该字符串的前31个字符将被忽略,并且不做任何贡献。其余的编码字符串包含用于绘制地图的指令。各个字符确定要连续绘制多少个空格或感叹号。
外循环
这个循环越过字符串中的字符。每次迭代将b的值加1,然后将字符串的下一个字符分配给a。
内循环
此循环绘制单个字符,并在到达行尾时绘制换行符。考虑一下这个putchar语句
putchar(++c=='Z' ? c = c/9 : 33^b&1);
由于“Z”代表ASCII 90号,第90/9会给我们10这是一个字符。十进制33是ASCII,表示“!”。切换33的低阶位可得到32,即空格的ASCII码。这引起 !如果b为奇数,则打印;如果b为偶数,则打印空白。
下面是上面代码的不太混淆的版本–
C
// C program to print map of India
#include
int main()
{
int a = 10, b = 0, c = 10;
// The encoded string after removing first 31 characters
// Its individual characters determine how many spaces
// or exclamation marks to draw consecutively.
char* str = "TFy!QJu ROo TNn(ROo)SLq SLq ULo+UHs UJq "
"TNn*RPn/QPbEWS_JSWQAIJO^NBELPeHBFHT}TnALVlBL"
"OFAkHFOuFETpHCStHAUFAgcEAelclcn^r^r\\tZvYxXyT|S~Pn SPm "
"SOn TNn ULo0ULo#ULo-WHq!WFs XDt!";
while (a != 0)
{
// read each character of encoded string
a = str[b++];
while (a-- > 64)
{
if (++c == 90) // 'Z' is 90 in ascii
{
// reset c to 10 when the end of line is reached
c = 10; // '\n' is 10 in ascii
// print newline
putchar('\n'); // or putchar(c);
}
else
{
// draw the appropriate character
// depending on whether b is even or odd
if (b % 2 == 0)
putchar('!');
else
putchar(' ');
}
}
}
return 0;
}
Java
// Java program to print map of India
class GFG
{
public static void main(String[] args)
{
int a =10, b = 0, c = 10;
// The encoded string after removing first 31 characters
// Its individual characters determine how many spaces
// or exclamation marks to draw consecutively.
String s1="TFy!QJu ROo TNn(ROo)SLq SLq ULo+UHs UJq TNn*RPn/QP,\n"
+ "bEWS_JSWQAIJO^NBELPeHBFHT}TnALVlBLOFAkHFOuFETpHCStHAUFAgcEAelc,\n"
+ "lcn^r^r\\tZvYxXyT|S~Pn SPm SOn TNn ULo0ULo#ULo-WHq!WFs XDt!";
// read each character of encoded string
a=s1.charAt(b);
while (a != 0)
{
if (b < 170)
{
a = s1.charAt(b);
b++;
while (a-- > 64)
{
if (++c=='Z')
{
c/=9;
System.out.print((char)(c));
}
else
System.out.print((char)(33 ^ (b & 0x01)));
}
}
else
break;
}
}
}
Python3
# Python3 program to print map of India
a = 10
b = 0
c = 10
# The encoded string after removing first
# 31 characters. Its individual characters
# determine how many spaces or exclamation
# marks to draw consecutively.
s = ("TFy!QJu ROo TNn(ROo)SLq SLq ULo+UHs"
" UJq TNn*RPn/QPbEWS_JSWQAIJO^NBELPe"
"HBFHT}TnALVlBLOFAkHFOuFETpHCStHAUFA"
"gcEAelclcn^r^r\\tZvYxXyT|S~Pn SPm S"
"On TNn ULo0ULo#ULo-WHq!WFs XDt!")
# Read each character of encoded string
a = ord(s[b])
while a != 0:
if b < 170:
a = ord(s[b])
b += 1
while a > 64:
a -= 1
c += 1
if c == 90:
c = c // 9
print(end = chr(c))
else:
print(chr(33 ^ (b & 0X01)), end = '')
else:
break
# The code is contributed by aayush_chouhan
C#
// C# program to print map of India
using System;
class GFG
{
public static void Main()
{
int a = 10, b = 0, c = 10;
// The encoded string after removing first 31 characters
// Its individual characters determine how many spaces
// or exclamation marks to draw consecutively.
string s1 = "TFy!QJu ROo TNn(ROo)SLq SLq ULo+UHs UJq TNn*RPn/QP,\n"
+ "bEWS_JSWQAIJO^NBELPeHBFHT}TnALVlBLOFAkHFOuFETpHCStHAUFAgcEAelc,\n"
+ "lcn^r^r\\tZvYxXyT|S~Pn SPm SOn TNn ULo0ULo#ULo-WHq!WFs XDt!";
// read each character of encoded string
a = s1[b];
while (a != 0)
{
if (b < 170)
{
a = s1[b];
b++;
while (a-- > 64)
{
if (++c == 'Z')
{
c/=9;
Console.Write((char)(c));
}
else
Console.Write((char)(33 ^ (b & 0x01)));
}
}
else
break;
}
}
}
//This code is contributed by vt_m.
PHP
64)
{
if (++$c==90)
{
$c=floor($c/9);
echo chr($c);
}
else
printf(chr(33 ^ ($b & 0x01)));
}
}
else
break;
}
// note: ord() function convert the
// characters into its Ascii value
//this code is contributed by mits
?>
输出:
参考: http : //stackoverflow.com/questions/3533348/how-does-this-code-generate-the-map-of-india
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。