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

Автор memset0c, история, 4 года назад, По-английски

Excuse me, can anyone tell me how to use printf to output long double values?

I tried to use %Lf, but it just output zero when I chose GNU C++11.

However, I got AC when choosing GNU C++17 (64).

Can anyone tell me what should I do? Thank you~

GNU C++11 submission (WA on 1): https://codeforces.com/contest/1209/submission/92331960

GNU C++17 (64) submission (AC): https://codeforces.com/contest/1209/submission/92331832

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

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

Just use GNU C++17 (64)

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

If you're down with cout then,

std::cout<<std::setprecision(10)<<ans<<std::endl;

This worked -> Here

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

    It still doesn't work with GNU C++11.

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

      I have used simple cout in your code with both C++14 and C++11 with outputs 361.569 and 1000 respectively.

      Now I don't think this is possible unless you've used something else that is broken in C++11. I might be wrong.

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

        Only using std::cin & std::cout instead of scanf & printf can it work with GNU C++11.

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

A tangential answer: note that, most of the time, casting to a double would be fine in output, as a relative error of $$$10^{-9}$$$ is allowed.

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

Short answer is, you don't. The implementation used by codeforces's version of C++11 does not implement printf for long doubles.

You can switch to the mingw implementation by either submitting in C++11 with #define __USE_MINGW_ANSI_STDIO 1 before your code, or by submitting in C++17 (64). Be aware though that mingw printf is slow so you are exposing yourself to potential TLEs by doing this.

To be safe, I would either switch to using cout and a more recent version of C++, or use C++11 printf after casting to double.

»
4 года назад, # |
  Проголосовать: нравится +119 Проголосовать: не нравится
This is very off-topic but turns out you can put image as a background for your text on cf