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

Автор DarkZero, 10 лет назад, По-английски

In CodeForces #243 Div 2 B (something to do with mirroring), I submitted a solution (number is 6543367), and on the 10th test it gives 2 with GNU C++ 4.7 compiler, but on my own computer (Windows 8, gdb 6.8) it gives the correct answer 1.

And then I tried to switch to Microsoft VC++ 2010, and got AC.....

I checked my code twice and did not discover any hazard parts. Is there anyone could explain why? Thanks!

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

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

I've edited your solution a bit. 6543599

Problem in

double tmp = log((double)(n/x)) / log(2.0);
	if( tmp != int(tmp))
		return false;
  • »
    »
    10 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Thank you very much for your attention! Does the solution imply that I should avoid using "log" when compiling with GNU C++?

    • »
      »
      »
      10 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Code that looks like:

      double a;
      if(a != int(a))
      

      usually bad idea (imho).

      Look at this 6543990

      • »
        »
        »
        »
        10 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        Thank you, I will use

        double tmp;
        if( tmp != round(tmp) )
        

        instead.

        Learnt a lesson today, thank you!

        • »
          »
          »
          »
          »
          10 лет назад, # ^ |
            Проголосовать: нравится +31 Проголосовать: не нравится

          It's also a bad idea. You should avoid using operators == and != together with floating points variables. For example, instead of the code you've provided in the last comment one can write the following:

          double a;
          if (abs(a - round(a)) > eps) ...
          

          Here eps should be small enough, e.g. 10 - 9. Actually, its value depends on the problem you are solving.

          • »
            »
            »
            »
            »
            »
            10 лет назад, # ^ |
              Проголосовать: нравится 0 Проголосовать: не нравится

            Thank you, I have known this trick but in that round I was too lazy to write in that way, and it came back to me = =

  • »
    »
    10 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    (Deleted)