C语言 这段代码是如何生成印度地图的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3533348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:14:51  来源:igfitidea点击:

How does this code generate the map of India?

cobfuscation

提问by narayanpatra

This code prints the map of India. How does it work?

此代码打印印度地图。它是如何工作的?

#include <stdio.h>
main()
{
    int a,b,c;
    int count = 1;
    for (b=c=10;a="- FIGURE?, UMKC,XYZHello Folks,\
    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;
}

采纳答案by bta

The long string is simply a binary sequence converted to ASCII. The first forstatement makes bstart out at 10, and the [b+++21]after the string yields 31. Treating the string as an array, offset 31 is the start of the "real" data in the string (the second line in the code sample you provided). The rest of the code simply loops through the bit sequence, converting the 1's and 0's to !'s and whitespace and printing one character at a time.

长字符串只是转换为 ASCII 的二进制序列。第一条for语句b从 10 开始,在[b+++21]字符串之后产生 31。将字符串视为数组,偏移量 31 是字符串中“真实”数据的开始(您提供的代码示例中的第二行)。其余的代码只是简单地遍历位序列,将 1 和 0 转换为 ! 和空格,并一次打印一个字符。

Less obfuscated version:

较少混淆的版本:

#include "stdio.h"
int main (void) {
    int a=10, b=0, c=10;
    char* bits ="TFy!QJu ROo TNn(ROo)SLq SLq ULo+UHs UJq TNn*RPn/QPbEWS_JSWQAIJO^NBELPeHBFHT}TnALVlBLOFAkHFOuFETpHCStHAUFAgcEAelclcn^r^r\tZvYxXyT|S~Pn SPm SOn TNn ULo0ULo#ULo-WHq!WFs XDt!";
    a = bits[b];
    while (a != 0) {
        a = bits[b];
        b++;
        while (a > 64) {
            a--;
            if (++c == 'Z') {
                c /= 9;
                putchar(c);
            } else {
                putchar(33 ^ (b & 0x01));
            }
        }
    }
    return 0;
}

The strangeclever part is in the putcharstatements. Take the first putchar. ASCII 'Z'is 90 in decimal, so 90 / 9 = 10 which is a newline character. In the second, decimal 33 is ASCII for '!'. Toggling the low-order bit of 33 gives you 32, which is ASCII for a space. This causes !to be printed if bis odd, and a blank space to be printed if bis even. The rest of the code is simply there to walk the "pointer" athrough the string.

巧的部分在putchar报表。拿第一个putchar。ASCII'Z'是十进制的 90,所以 90 / 9 = 10 这是一个换行符。在第二个中,十进制 33 是​​ ASCII 的'!'. 切换 33 的低位给你 32,这是一个空格的 ASCII。这导致!如果b是奇数则打印,如果是偶数则打印空白b。其余的代码只是用来遍历a字符串的“指针” 。

回答by interjay

Basically, the string is a run-length encodingof the image: Alternating characters in the string say how many times to draw a space, and how many times to draw an exclamation mark consecutively. Here is an analysis of the different elements of this program:

基本上,字符串是图像的游程编码:字符串中的交替字符表示绘制空格的次数,以及连续绘制感叹号的次数。以下是对该计划不同要素的分析:

The encoded string

编码后的字符串

The first 31 characters of this string are ignored. The rest contain instructions for drawing the image. The individual characters determine how many spaces or exclamation marks to draw consecutively.

此字符串的前 31 个字符将被忽略。其余部分包含绘制图像的说明。单个字符决定要连续绘制多少个空格或感叹号。

Outer for loop

外部 for 循环

This loop goes over the characters in the string. Each iteration increases the value of bby one, and assigns the next character in the string to a.

这个循环遍历字符串中的字符。每次迭代都会将 的值增加b1,并将字符串中的下一个字符分配给a

Inner for loop

内部for循环

This loop draws individual characters, and a newline whenever it reaches the end of line. The number of characters drawn is a - 64. The value of cgoes from 10 to 90, and resets to 10 when the end of line is reached.

此循环绘制单个字符,并在到达行尾时绘制换行符。绘制的字符数是a - 64。的值c从 10 到 90,并在到达行尾时重置为 10。

The putchar

putchar

This can be rewritten as:

这可以改写为:

++c;
if (c==90) {       //'Z' == 90
    c = 10;        //Note: 10 == '\n'
    putchar('\n');
}
else {
    if (b % 2 == 0)
        putchar('!');
    else
        putchar(' ');
}

It draws the appropriate character, depending on whether bis even or odd, or a newline when needed.

它绘制适当的字符,取决于b是偶数还是奇数,或者在需要时换行。