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

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

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
  • Проголосовать: не нравится

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

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

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

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

    • »
      »
      »
      5 лет назад, скрыть # ^ |
      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);
      	}
      
      • »
        »
        »
        »
        5 лет назад, скрыть # ^ |
         
        Проголосовать: нравится 0 Проголосовать: не нравится

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

      • »
        »
        »
        »
        5 лет назад, скрыть # ^ |
         
        Проголосовать: нравится 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 ...

        • »
          »
          »
          »
          »
          5 лет назад, скрыть # ^ |
           
          Проголосовать: нравится 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.
»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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

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

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