sahasumit288's blog

By sahasumit288, history, 9 years ago, In English

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?

  • Vote: I like it
  • +3
  • Vote: I do not like it

»
9 years ago, # |
Rev. 4   Vote: I like it +10 Vote: I do not like it

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

  • »
    »
    9 years ago, # ^ |
    Rev. 2   Vote: I like it +16 Vote: I do not like it

    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