-1

this code is the solution to this problem https://codeforces.com/problemset/problem/136/A in codeforces .... I understand the problem and I solved it but I don't understand this solution.. please someone explain to me this code

int n,a[101],i,j;

main()
{
    n||scanf("%d",&n);
    i-n&&(scanf("%d",a),a[*a]=++i,main(),printf("%d ",a[++j]));
}
  • Welcome to Stack Overflow! Please try to ask specific questions. What specific parts of this code do you not understand? What have you tried to understand so far? – GraphicsMuncher Sep 13 '20 at 20:05
  • 1
    Please see [“Explain X to me” questions: How to react?](https://meta.stackoverflow.com/questions/271468/explain-x-to-me-questions-how-to-react) for why you need to narrow what you do not understand. I do not think the title you've chosen makes this Question easy to find from a search. – Scratte Sep 14 '20 at 07:27
  • 1
    Don't bother with understanding this solution. It is obfuscation and horribly slow recursion on top of that. Looks like "code golf" and not a real program. – Lundin Sep 14 '20 at 09:39
  • 1
    Also, `main()` instead of `int main()` with a function that lacks `return 0;` is invalid in any form of C. In C90, it would invoke undefined behavior if the caller (OS) used the result, in C99 and beyond it won't compile. – Lundin Sep 14 '20 at 09:42

1 Answers1

0

Due to short-circuit evaluation of the operators || and &&, the code you posted is equivalent to the following:

int n,a[101],i,j;

main()
{
    if ( n == 0 )
    {
        scanf( "%d", &n );
    }

    if ( i - n != 0 )
    {
        scanf( "%d", a );

        i++;
        a[*a] = i;

        main(); //recursive function call

        j++;
        printf( "%d ", a[j] );
    }
}

In order to understand how this code works, all you have to do is run it line by line in a debugger, while monitoring the values of all variables.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39