Código para Generar el Mapa de India (Con Explicación)

Dado un código ofuscado que genera el mapa de la India, explique su funcionamiento. El siguiente código cuando se ejecuta genera el mapa de India.

#include "stdio.h"

int main()

ding Posts

int a, b, c;

for (b-c-18; a="Hello! Welcome to Geeks ForGeeks.\

TFy!QJu ROo TNn (ROO) SLq SLq ULO+

UHS UJq TNn*RPn/QPbEWS_JSWQAIJO^\

NBELPEHBFHT}TnALVIBLOFAKHFOUFETp\

HCSTHAUFAgcEAelclcn^r^r\\tZvYXXy\

T|S~Pn SPM SOn TNn ULOGULO#ULO-W\

er menu

et

Hq!WFs XDt!" [b+++21]; )

ents 850

dot menu

gs

for (; a-- > 64; )

putchar (++C == 'Z' ? c = c/ 9:33^b&1);

ar Ads Widget

ub menu

et

return 0;

se menu

}

El código anterior es un ejemplo típico de código ofuscado, es decir, código que es difícil de entender para los humanos.

¿Como funciona?

Básicamente, la string es una codificación de longitud de ejecución del mapa de la India. Los caracteres alternos en la string almacenan cuántas veces dibujar un espacio y cuántas veces dibujar un signo de exclamación consecutivamente. Aquí hay un análisis de los diferentes elementos de este programa:

La string codificada

"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!";

Observe [b+++21] al final de la string codificada. Como b+++21 es equivalente a (b++ + 21) que se evaluará como 31 (10 + 21), los primeros 31 caracteres de esta string se ignoran y no contribuyen a nada. La string codificada restante contiene instrucciones para dibujar el mapa. Los caracteres individuales determinan cuántos espacios o signos de exclamación dibujar de forma consecutiva.

Bucle for exterior: este bucle repasa los caracteres de la string. Cada iteración aumenta el valor de b en uno y asigna el siguiente carácter de la string a a.
Bucle for interno: este bucle dibuja caracteres individuales y una nueva línea cada vez que llega al final de la línea. Considere esta declaración putchar 

putchar(++c=='Z' ? c = c/9 : 33^b&1);

Como ‘Z’ representa el número 90 en ASCII, 90/9 nos dará 10, que es un carácter de nueva línea. El decimal 33 es ASCII para ‘!’. Alternar el bit de orden inferior de 33 le da 32, que es ASCII para un espacio. ¡Esto causa! para imprimir si b es impar, y un espacio en blanco para imprimir si b es par. 

A continuación se muestra una versión menos ofuscada del código anterior:

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;        // '\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;
}
 
// 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;        // '\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

<?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
?>

Producción: 

Console printing India's Map

Este artículo es una contribución de Aditya Goel . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks. Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *