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

Автор sahasumit288, история, 8 лет назад, По-английски

UVA -1177

I need to convert a double number to an integer and print it. Let ans is that double number. Now,if I write


printf("%0.lf\n",&ans);

I got accepted. But instead if write

cout<<(int)ans<<"\n";

I got wrong answer.

Can anyone explain it?

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

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

printf("%.0lf\n", ans) prints round(ans), but if you write (int)ans it will be floor(ans)

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

    Hello,

    well it is not exactly "ceil" .. printf has the property it rounds it to NEAREST integer (in this case), so it is more like "(int)(ans+0.49999...)"

    You can take look here here to see the behaviour