Please read the new rule regarding the restriction on the use of AI tools. ×

IbrahimElmourchidi's blog

By IbrahimElmourchidi, history, 6 years ago, In English

hello Every one ! i solved this problem https://codeforces.me/problemset/problem/785/A using c11 and after i solved the problem i saw others submissions to check if some one have a better solution and so i found a submission i cannot understand can some one please help , this is it :


#include <stdio.h> int t, n; main() { scanf("%d\n", &n); while (n--) { char s[9]; gets(s); t += "4!8D6<"[*s%7]-48; // this the line i cannot understand } printf("%d\n", t); }

and that was my submission :

#include<stdio.h>
int main()
{
    int a,i=0,c=0;
    scanf("%d",&a);
    char b[13];
    gets(b);
    for(i=0;i<a;i++)
    {
        gets(b);
        c+=( b[0]=='I'?20:b[0]=='C'?6:b[0]=='O'?8:b[0]=='D'?12:4 );
    }
    printf("%d",c);
}
»
6 years ago, # |
  Vote: I like it +23 Vote: I do not like it

"string-literal"[index] is the character of the "string-literal" at position index, nothing fancy here.

Other than that, a character can be used as its ASCII code.
So, when used as numbers, "4!8D6<" is short for {52, 33, 56, 68, 54, 60}.

Similarly, *s%7 is the ASCII code of the first character of the respective string modulo 7.
As an example, when s = "Tetrahedron", the code of *s = s[0] = 'T' is 84, and 84 % 7 = 0.
So the answer is {52, 33, 56, 68, 54, 60}[84 % 7] - 48 = 4.