给出一个生成印度地图的模糊代码,解释它的工作原理。 以下代码在执行时生成印度地图——
上面的代码是一个典型的 模糊代码 i、 e.人类难以理解的代码。 它是如何工作的? 基本上,字符串是印度地图的游程编码。字符串中的交替字符存储连续绘制空格和感叹号的次数。 以下是对该项目不同要素的分析—— 编码字符串
"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 <iostream> using namespace std; 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; // '' is 10 in ascii // print newline putchar ( '' ); // 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; } // This code is contributed by SHUBHAMSINGH10. |
C
// C program to print map of India #include <stdio.h> 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; // '' is 10 in ascii // print newline putchar ( '' ); // 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," + "bEWS_JSWQAIJO^NBELPeHBFHT}TnALVlBLOFAkHFOuFETpHCStHAUFAgcEAelc," + "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," + "bEWS_JSWQAIJO^NBELPeHBFHT}TnALVlBLOFAkHFOuFETpHCStHAUFAgcEAelc," + "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
<?php // PHP Implementation 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. $s1 = "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!" ; $a =ord( $s1 [ $b ]); while ( $a != 0) { if ( $b < 170) { $a = ord( $s1 [ $b ]); $b ++; while ( $a -- > 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 本文由 阿迪蒂亚·戈尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。