Page 10 of 12
Re: 10055 Getting TLE
Posted: Tue Dec 13, 2011 8:23 am
by HenryGale
Same thing happened to me, so I had to substitute Scanner by a more efficient input method.
Also you may find
this discussion interesting and useful. (Warning! It does contain multiple solutions to the 10055 problem.)
Re: 10055 - Hashmat the Brave Warrior
Posted: Thu Mar 29, 2012 5:35 am
by Khaldu
Why i getting time limit exceeded??? i use integer,,long,,long long all data types....
#include<stdio.h>
int main()
{
long long a,b,c;
for(;;)
{
scanf("%lld %lld",&a,&b);
if(a<b)
{ c=b-a;
printf("%lld\n",c);
}
if(a>b)
{ c=a-b;
printf("%lld\n",a-b);
}
}
return 0;
}
Re: 10055 - Hashmat the Brave Warrior
Posted: Fri Mar 30, 2012 6:58 pm
by brianfry713
You don't have an exit condition. The loop will run forever waiting on more input from scanf.
Re: 10055 - Hashmat the Brave Warrior
Posted: Mon Apr 02, 2012 1:15 pm
by fadying
Why does this simply problem take
2.2s in
java and take only
0.06s in
ANSI C? I don't understand..
Code: Select all
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long a, b;
while (in.hasNextLong()) {
a = in.nextLong();
b = in.nextLong();
if (b > a)
System.out.println(b-a);
else
System.out.println(a-b);
}
}
}
Re: 10055 - Hashmat the Brave Warrior
Posted: Mon Apr 02, 2012 8:46 pm
by brianfry713
JAVA is slower than C.
Re: 10055 Getting TLE
Posted: Thu Apr 12, 2012 2:34 pm
by cse.mehedi
use Scanner cin = new Scanner(System.in);
you will get AC.
Re: 10055 - Hashmat the Brave Warrior
Posted: Fri Sep 21, 2012 6:41 am
by renatov
Hey guys, I'm receiving a "
Wrong Answer" when I submit, but the program is running just fine in Code Blocks (WinXP). I can even input 2^32 (4294967296) and everything still works. Could someone please help?
Code: Select all
accepted;
ps: just use "long long".
Re: 10055 - Hashmat the Brave Warrior
Posted: Fri Sep 21, 2012 9:51 pm
by brianfry713
Use long long.
Re: 10055 - Hashmat the Brave Warrior
Posted: Sat Sep 22, 2012 12:04 am
by renatov
brianfry713 wrote:Use long long.
I changed to long long and it doesn't work either, although it works perfectly in my computer. Could somebody help me, please? Here is the code:
Code: Select all
#include <stdio.h>
int main()
{
long long int lldMenor;
long long int lldMaior;
while( scanf("%lld %lld", &lldMenor, &lldMaior) != EOF )
printf("%lld\n", lldMaior - lldMenor);
return 0;
}
Re: 10055 - Hashmat the Brave Warrior
Posted: Tue Sep 25, 2012 1:33 am
by brianfry713
Print the absolute value of the difference.
Re: 10055 - Hashmat the Brave Warrior
Posted: Fri Sep 28, 2012 8:22 pm
by renatov
It's working now, thanks!
ps: %lld didn't work on Windows (I had to use %I64d to test), but it worked for UVA's gcc.
Re: 10055 - Hashmat the Brave Warrior
Posted: Sun Apr 21, 2013 8:25 pm
by Scorvus
what's wrong with my code? i got WA with this
Code: Select all
#include<stdio.h>
int main()
{
long long int h,e;
while(!EOF)
{
scanf("%lld %lld",&h,&e);
if(h<e)
printf("%lld\n",e-h);
else
printf("%lld\n",h-e);
}
return 0;
}
Re: 10055 - Hashmat the Brave Warrior
Posted: Mon Apr 22, 2013 11:39 pm
by brianfry713
Your code doesn't print anything.
Instead try something like:
while(scanf("%lld %lld",&h,&e) == 2) {
Re: 10055 - Hashmat the Brave Warrior
Posted: Tue Apr 23, 2013 4:41 pm
by Scorvus
what this line do?
while(scanf("%lld %lld",&h,&e) == 2) {
i'm sorry, i'm new with this thing.
Re: 10055 - Hashmat the Brave Warrior
Posted: Wed Apr 24, 2013 12:11 am
by brianfry713
http://www.cplusplus.com/reference/cstdio/EOF/
Your code:
while(!EOF)
Never enters that loop because it evaluates to:
while(!-1) or while(0)
http://www.cplusplus.com/reference/cstdio/scanf/
On success, scanf returns the number of items of the argument list successfully filled. So this code:
while(scanf("%lld %lld",&h,&e) == 2) {
Will continue reading h and e until the end of the input.