c - if statement and puts() -


recently have across solution on codeforces site. couldn't understand condition in if statement , condition of ternary operator in question can please me ?

#include<stdio.h>  int main(void) {     int a[1000]={0},ans,k;     while((k=getchar())!='\n'){       if(!a[k]){           ans++;           a[k]=1;       }     }     puts(ans&1 ? "ignore him!":"chat her!");     return 0; } 

it's bizarre piece of code, i'm not sure entirely why it's easy enough understand technical viewpoint (i.e., what does).

the while loop gets series of characters standard input and, each unique one, adds 1 ans variable. expression !a[k] true if a[k] 0 (they're initialised state).

when condition true, ans incremented , a[k] set 1, meaning more characters of value not affect outcome (the condition won't ever true again).

in terms of if statement, expression ans&1 true if ans odd (has lower-order bit set).

so appears tells ignore people have odd number of unique characters in names , talk others. of course, whole thing falls apart since don't initialise ans, meaning can arbitrary value (not zero) , program therefore pretty able tell whatever wants.

once you've fixed little issue, i'll happy chat further - paxdiablo has 8 unique characters. should warn in advance though, i'm not "her" :-)


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

c# - Retrieve google contact -

javascript - How to insert selected radio button value into table cell -