How does this code generate the map of India?

后端 未结 2 1165
无人及你
无人及你 2021-01-29 17:04

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

#include 
main()
{
    int a,b,c;
    int count = 1;
    for (b=c=10;a=\"- FIGURE?, UMKC,XYZH         


        
2条回答
  •  渐次进展
    2021-01-29 17:41

    Basically, the string is a run-length encoding of 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.

    Outer for loop

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

    Inner for loop

    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 c goes from 10 to 90, and resets to 10 when the end of line is reached.

    The 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 b is even or odd, or a newline when needed.

提交回复
热议问题