Блог пользователя Re_cursive

Автор Re_cursive, 12 лет назад, По-английски
#include <cstdio>

int main()
{
  int num_P, T, sum, Total = 0, *Avail, ind = 0, end = 0;
  bool MadePair = false;
  
  for (scanf("%d", &num_P), Avail = new int[num_P], sum = 0; sum < num_P; ++sum, MadePair = false)
  {
    scanf("%d", &T);
    
    if ((4 - T) > 0) {
      end = ind;
      
      while (Avail[--end]){
	if ((Avail[end] - T) >= 0){
	  Avail[end] -= T;
	  MadePair = true;
	  break;
	}
      }
      
      if (!MadePair) { //Skips this entire block {}
	++Total;
	Avail[ind++] = 4 - T;
      }
    }
    else if ((4 -T) == 0)
      ++Total;
  }
  
  printf("%d\n", Total);
  
  return 0;
}

The judge skips the if statement I pointed out. I know it does this because I ran a test on this site and and using some print statements, I saw that it did not print anything or do anything inside the if-statement.

It works for me and I'm sure anyone who sees the code would know that it should work. It might just be that the editor I'm using appends some hidden characters to the code because that was the case in my last blog. In that case, if someone sees these hidden characters, can they point it out or explain why the compiler is skipping the if statement?

Thanks

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
12 лет назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

Bugs in compiler are very rare. So it's your fault, not compiler's.

For example, the entire if ((4 - T) > 0) { branch won't be executed on '5 1 2 4 3 3'.

Why do you write so strange unreadable code? 4 > T is much better than (4 - T) > 0.

»
12 лет назад, # |
Rev. 3   Проголосовать: нравится +1 Проголосовать: не нравится

At first time when the program step into if ((4 - T) > 0)
end = 0, and Avail[--end] turns to Avail[-1], so you corrupt some memory and the behaviour of your program become unpredictable.