You are not logged in. Please login at to post your questions!


[closed] Slow Trains (BUG4EVER 2017) Weird Issue

Here are two of my submissions, exactly same, one in C and the other in C++. The submission in C++ gives an AC verdict while the submission in C gives a WA verdict. The code was originally written in C. Can anyone please point out the error? I'm unable to find the problem because the two submissions are EXACTLY SAME. (only iostream line was different)

Solution 1

Solution 2


asked 03 Sep '17, 23:39

swetankmodi's gravatar image

6★swetankmodi ♦♦
accept rate: 15%

closed 02 Oct '17, 22:19

That's weird.

(04 Sep '17, 09:48) c_utkarsh5★

I am also having the same problem...would be grateful if someone could figure it out.. solution link:

(04 Sep '17, 10:09) viralivora4★

Quite weird actually. Are you sure that TCs werent changed mid way?

(04 Sep '17, 12:28) vijju123 ♦♦5★

nope the TC's were same here's the link to the problem:

(05 Sep '17, 20:52) viralivora4★

yeah, TC's were same @vijju123 as most of the people's solution are 100% similar to mine. It's just if and else lol. Wasted 2 hours figuring out error in my logic lol

(06 Sep '17, 22:44) swetankmodi ♦♦6★

What was your error? What difference was in C and C++ code?

(06 Sep '17, 22:56) vijju123 ♦♦5★

the competition wasn't fair though

(06 Sep '17, 23:54) viralivora4★

no error no difference

(09 Sep '17, 23:10) swetankmodi ♦♦6★
showing 5 of 8 show all

The question has been closed for the following reason "The question is answered, right answer was accepted" by swetankmodi 02 Oct '17, 22:19

Well, got the error guys. You're declaring variables inside the loop which sometimes don't work well in C Language. Look at the solution if declared outside your code works fine in C. Solution


answered 10 Sep '17, 20:59

vaibhavagni's gravatar image

accept rate: 60%

edited 11 Sep '17, 08:35

If you dont know the cause, how did you conclude :p

(10 Sep '17, 21:31) vijju123 ♦♦5★

Simply, iterating through all the possibilities where an error should occur and that was the line which was causing the error when declared outside, works like a charm.

(11 Sep '17, 00:05) vaibhavagni2★

Well, now I got the cause. That's a standard difference. In C99, the rules are the same as in C++. Default GCC uses C89 with some extensions. For C89, you must declare all of your variables at the beginning of a scope block.

(11 Sep '17, 00:13) vaibhavagni2★

Thanks, never knew that it makes a difference nowadays. I had read that nowadays it doesn't matter :\ Maybe I was wrong lol. I'll declare everything global now :P

(15 Sep '17, 01:22) swetankmodi ♦♦6★

Most of the compiler use C99 but still some Online judges use C89 as gcc. Declaring global will also help you to use more space for allocation. Better to use C++, it also has some inbuilt implementations to make the job easy.

(16 Sep '17, 20:01) vaibhavagni2★

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 03 Sep '17, 23:39

question was seen: 437 times

last updated: 02 Oct '17, 22:19