Saturday, January 4, 2014

10055: Hashmat the brave warrior

It's been awhile since I last tried to code for uVA online judge. Probably more than a year. But for some reason, I finally mustered the courage to try to code again. This time I tried using online resources to learn from them. One of the more popular resources is uHunt which enables users to find problems that are easy. In this way, by solving easy problems, one can solve more, and not get that frustrated. I know the feeling of being unable to get an accepted answer. Everytime you submit, you get "Wrong answer" response. It is just so painful. So I tried using uHunt to see if indeed it does suggest easier problems to solve. uHunt, by the way, has an address of:

The first problem uHunt suggested to me was 10055 Hashmat the brave warrior. It looks very easy, because all you have to do is subtract two numbers and get the absolute value of the difference. But ironically, it is very deceptive. I had several wrong answer submissions and I could not decipher what was wrong. It happens that this problem is a play on technicality. The problem states "The input numbers are not greater than 2^32." This probably means that plain integer data types won't be able to handle the integer storage. I used unsigned int, but I got wrong answer. Then I tried unsigned long int, still wrong. I also tried unsigned long long int, and still I got wrong answer. It was crazy and definitely a play on technicality. Then I checked the forums and noticed that I had to use %lld instead of %d in my sscanf() and printf() functions. It was a terrible experience because I felt that it was supposed to be a very easy problem. I ended up having more wrong answer submissions for this problem than probably any other problems in the past. And this problem was supposed to be one of the easier ones. Tsk, tsk.

Anyway, here's my code:


int main() {
  char line[100];
  unsigned long long int a, b, c;

  while (fgets(line, 100, stdin)) {
    sscanf(line, "%lld %lld", &a, &b);

    if (a > b) {
      c = a - b;
    else {
      c = b - a;

    printf("%lld\n", c);
  return 0;

No comments:

Post a Comment