I submitted two solutions for the elevators and stairs problem,the first one which gives wrong answer and the another one which gives correct answer.please help.And please don’t tell me to change the scanf order and answer in new line as I have tried that also on suggestion by other users when I posted the same question before.Please pleeeease help.
The problem is here-
e = 2*N/V2;
N is an integer, V2 is in integer. Since both data types are int, it will calculate answer in int as well. Read about implicit type casting. One of examples is-
double a= 5/2; //a=2, not 2.5.
double a= (double) 5/2; //a=2.5 now since we type explicitly casted answer to double.
You need to modify expressions for t1 and t2, both.
When you divide two integers, it is an integer division. So an integer division is performed first and so any decimals in the result would be truncated first and then the new result thus obtained would be stored in the left to assignment operator (which in this case e). To overcome this, you can do the following:
Approach 1: (explicitly type cast it as float) which you have already done in your second code.
e = (float)2*n/V2;
Approach 2: Multiply any integer variable in that expression with a float.
e = 2*(1.0)*N/V2;
Approach 3: Declare both variables N and V2 as floats. This way, implicitly, a floating point division would be performed rather than an integer division.
float V2,N;
e = 2*N/V2;
And why do you say not to comment on scanf() order and newline prints? If you change the scanf order, you are literally changing values of variables: if you change the scanf order, v1 will be v2 or N and vice versa. And if you don’t print newline characters in problems where there are multiple test cases, then you are not printing your answers in a way codechef judge is expecting. In which case, even if you solve the problem, your answer would be no better than a person who has not solved it.
Thank you very much.I appreciate that you spent your precious time viewing my solution and pointing out my mistakes.
Sorry but I cannot up vote as I do not have enough reputation points.I really wanted to do so.
Thank you friend.It was really helpful
You can always accept his answer by clicking on “tick in circle” dear.