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

Автор atulcf24, 2 года назад, По-английски

adding floating point numbers in sqrt make them more accuracte

i submitted a solution containing

 int sq = sqrt(sum);
            if (sq * sq == sum && sq % 2 == 1) cnt++;

although this seems perfectly fine, it doesn't pass the test case listed below

`14'

'1 10 10 100 1 1 10 1 10 2 10 2 10 1`

this must return 3 as the first,fourth and last number fit the condition but returns 2

the issue with it is that sqrt works well with floating point numbers this code fixes the rounding issues

long long sq = (long long)sqrt(sum + 0.5); // Add 0.5 to handle rounding issues
if (sq * sq == sum && sq % 2 == 1) cnt++;

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

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

use sqrtl for numbers that exceeds the int limit for higher precision