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

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

Edit: Problem fixed. It turns out that I output the answer in a separate function before I input and output the file, which causes the problem. Thanks for everyone that helps :)

Edit: I did write output file

Here

I've been doing some past USACO Gold problems lately, but I have met some troubles regarding submission. Although my program outputs the correct answer, it keeps telling me that the answer is incorrect. I've met these kind of troubles before, so I thought its just a format issue. But no matter how I adjust the way I output the answer, it doesn't work.

For example, I have tried cout << ans << "\n"; cout << ans << endl; cout << ans << " "; cout << ans; but non of them works.

Here are two screenshots:

USACO 2015 G DEC USACO 2016 G JAN

Can someone please help me with that? I just don't want to run into these kind of troubles during real contest lol. Thanks!

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

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

You need to write the output to a file, not stdout.

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

    That’s what I did. If I didn’t, the error message would be “Your output file missing”

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

      No you didn't. Notice how right after "Your output file" it has nothing, because you didn't print anything to a file. Instead it says "Your program wrote this on STANDARD OUTPUT: x". Because you wrote to standard output.

      Put this at the top:

      	if (fopen("FILENAME.in", "r")) {
      		freopen("FILENAME.in", "r", stdin);
      		freopen("FILENAME.out", "w", stdout);
      	}
      
      • »
        »
        »
        »
        4 года назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        hmmm but I have submitted other problems using the same setting (you can see me update), am I missing something?

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

        Oh yeah I realized in both cases, I output the answer in a function in front of the main function where I said ifstream ...

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

          In general, this pattern isn't great. You're shadowing the global std::cin variable with your own local variable named cin. There are a few things that could be better:

          1. freopen, as suggested above; you can even conditionally only freopen when the input file exists (that's the if (fopen) part).
          2. Declare your ifstream and ofstream globally, ideally with a different name (fin and fout?). Then, just remember to use the new names.
          3. You could make a function void go(istream& in, ostream& out) and put your code there; then, use in and out, and pick whether to use cin/cout or an ifstream/ofstream when you call it in main.
          • »
            »
            »
            »
            »
            »
            4 года назад, # ^ |
              Проголосовать: нравится 0 Проголосовать: не нравится

            Thanks! This might be a stupid question, but I thought if I used ios::sync_with_stdio(false), it prohibits redirection with freopen. How am I supposed to both use freopen and fast input using cin?

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

              No, those are essentially unrelated. freopen is a lower level change than the cin/cout streams.

              However, if you set sync_with_stdio, then do any I/O via cin/cout, and then you freopen afterwards, I think things may fall out of sync.

              Also, sync_with_stdio shouldn't be necessary after you freopen, so you could put that in the else branch.

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

                Can you explain why sync_with_stdio shouldn't be necessary after a freopen?

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

                  I might be wrong, but I think flushing or buffering is configured to be nicer with file IO.

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

Auto comment: topic has been updated by MVP_Harry (previous revision, new revision, compare).

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

Auto comment: topic has been updated by MVP_Harry (previous revision, new revision, compare).