10055 - Hashmat the Brave Warrior
Moderator: Board moderators
Re: 10055 Getting TLE
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.)
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
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;
}
#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;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10055 - Hashmat the Brave Warrior
You don't have an exit condition. The loop will run forever waiting on more input from scanf.
Check input and AC output for thousands of problems on uDebug!
Re: 10055 - Hashmat the Brave Warrior
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);
}
}
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10055 - Hashmat the Brave Warrior
JAVA is slower than C.
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 36
- Joined: Sun Mar 18, 2012 8:18 am
Re: 10055 Getting TLE
use Scanner cin = new Scanner(System.in);
you will get AC.
you will get AC.
Re: 10055 - Hashmat the Brave Warrior
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".
Last edited by renatov on Sun Sep 30, 2012 3:46 am, edited 2 times in total.
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10055 - Hashmat the Brave Warrior
Use long long.
Check input and AC output for thousands of problems on uDebug!
Re: 10055 - Hashmat the Brave Warrior
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:brianfry713 wrote:Use long long.
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;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10055 - Hashmat the Brave Warrior
Print the absolute value of the difference.
Check input and AC output for thousands of problems on uDebug!
Re: 10055 - Hashmat the Brave Warrior
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.
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
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;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10055 - Hashmat the Brave Warrior
Your code doesn't print anything.
Instead try something like:
while(scanf("%lld %lld",&h,&e) == 2) {
Instead try something like:
while(scanf("%lld %lld",&h,&e) == 2) {
Check input and AC output for thousands of problems on uDebug!
Re: 10055 - Hashmat the Brave Warrior
what this line do?
while(scanf("%lld %lld",&h,&e) == 2) {
i'm sorry, i'm new with this thing.
while(scanf("%lld %lld",&h,&e) == 2) {
i'm sorry, i'm new with this thing.
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10055 - Hashmat the Brave Warrior
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.
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.
Check input and AC output for thousands of problems on uDebug!