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?
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3831 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | gamegame | 3386 |
10 | ksun48 | 3373 |
# | User | Contrib. |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
7 | adamant | 156 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | Dominater069 | 153 |
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?
Name |
---|
printf("%.0lf\n", ans) prints round(ans), but if you write (int)ans it will be floor(ans)
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
Thanks morass.It helps me a lot.