0

Recently I have some across this solution on codeforces site. I couldn't understand the condition in the if statement and also the condition of the ternary operator in this question can someone please help me with this ?

#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 WITH HER!");
    return 0;
}
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
  • Try this [link](http://www.hermetic.ch/cfunlib/ast_amp2.htm) for `&` operator – Abhi May 15 '15 at 07:36
  • 2
    I've guessed that this is [tag:c] - but I shouldn't have had to. If I guessed wrong, please remove that tag and add the correct tag for your language. – Damien_The_Unbeliever May 15 '15 at 07:36
  • @Damien_The_Unbeliever , The code is in C. You guessed correctly. – Spikatrix May 15 '15 at 07:46
  • Answer of first part here:http://stackoverflow.com/questions/30194598/what-is-meaning-of-expression-array-nameindex-in-c/30194632#30194632 – Spikatrix May 15 '15 at 07:48
  • For the second part, look up on the bitwise AND operator. – Spikatrix May 15 '15 at 07:51
  • 1
    @CoolGuy - the point is, there are a plethora of languages and just because there are a lot of strong signals here that say "C" to me, I don't like to presume that there's not another C-like language out there that I don't know of and that happens (for whatever reason) to have completely different semantics. That's why, despite my having added the tag, I always ask the OPs to confirm that the re-tagging is correct. – Damien_The_Unbeliever May 15 '15 at 07:54

2 Answers2

3

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

The while loop gets a series of characters from standard input and, for each unique one, adds one to the ans variable. The expression !a[k] will be true if a[k] is zero (they're all initialised to this state).

When that condition is true, ans is incremented and a[k] is set to 1, meaning any more characters of that value will not affect the outcome (the condition won't ever be true again).

In terms of the if statement, the expression ans&1 will be true if ans is odd (has its lower-order bit set).

So it appears it tells you to ignore people who have an odd number of unique characters in their names and talk to the others. Of course, the whole thing falls apart since you don't actually initialise ans, meaning it can be an arbitrary value (not necessarily zero) and that this program is therefore pretty much able to tell you whatever it wants.

Once you've fixed that little issue, I'll be happy to chat further - paxdiablo has eight unique characters. I should warn you in advance though, I'm not a "HER" :-)

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
1
int a[1000] = {0};

This is a way to fill a[1000] with 0s.

If an array is partially initialized, elements that are not initialized receive the value 0 of the appropriate type (https://stackoverflow.com/a/2589751/3235496).


if (!a[k])

checks if k is an "already seen character", thus skipping further increments of ans.


x & 1

Finds if x is even or odd (checking the last bit). You can take a look at What is the fastest way to find if a number is even or odd? for further details.

So

puts(ans&1 ? "IGNORE HIM!" : "CHAT WITH HER!");

prints IGNORE HIM when ans is odd.

Community
  • 1
  • 1
manlio
  • 18,345
  • 14
  • 76
  • 126