10055 - Hashmat the Brave Warrior

All about problems in Volume 100. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

HenryGale
New poster
Posts: 1
Joined: Tue Dec 13, 2011 8:16 am

Re: 10055 Getting TLE

Post by HenryGale » Tue Dec 13, 2011 8:23 am

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.)

Khaldu
New poster
Posts: 2
Joined: Wed Mar 21, 2012 3:54 pm

Re: 10055 - Hashmat the Brave Warrior

Post by Khaldu » Thu Mar 29, 2012 5:35 am

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;

}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10055 - Hashmat the Brave Warrior

Post by brianfry713 » Fri Mar 30, 2012 6:58 pm

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!

fadying
New poster
Posts: 1
Joined: Mon Apr 02, 2012 1:05 pm

Re: 10055 - Hashmat the Brave Warrior

Post by fadying » Mon Apr 02, 2012 1:15 pm

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);
		}
	}
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10055 - Hashmat the Brave Warrior

Post by brianfry713 » Mon Apr 02, 2012 8:46 pm

JAVA is slower than C.
Check input and AC output for thousands of problems on uDebug!

cse.mehedi
New poster
Posts: 36
Joined: Sun Mar 18, 2012 8:18 am

Re: 10055 Getting TLE

Post by cse.mehedi » Thu Apr 12, 2012 2:34 pm

use Scanner cin = new Scanner(System.in);
you will get AC.

renatov
New poster
Posts: 20
Joined: Fri Sep 21, 2012 6:33 am

Re: 10055 - Hashmat the Brave Warrior

Post by renatov » Fri Sep 21, 2012 6:41 am

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.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10055 - Hashmat the Brave Warrior

Post by brianfry713 » Fri Sep 21, 2012 9:51 pm

Use long long.
Check input and AC output for thousands of problems on uDebug!

renatov
New poster
Posts: 20
Joined: Fri Sep 21, 2012 6:33 am

Re: 10055 - Hashmat the Brave Warrior

Post by renatov » Sat Sep 22, 2012 12:04 am

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;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10055 - Hashmat the Brave Warrior

Post by brianfry713 » Tue Sep 25, 2012 1:33 am

Print the absolute value of the difference.
Check input and AC output for thousands of problems on uDebug!

renatov
New poster
Posts: 20
Joined: Fri Sep 21, 2012 6:33 am

Re: 10055 - Hashmat the Brave Warrior

Post by renatov » Fri Sep 28, 2012 8:22 pm

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.

Scorvus
New poster
Posts: 4
Joined: Sun Apr 21, 2013 8:19 pm

Re: 10055 - Hashmat the Brave Warrior

Post by Scorvus » Sun Apr 21, 2013 8:25 pm

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;
}


brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10055 - Hashmat the Brave Warrior

Post by brianfry713 » Mon Apr 22, 2013 11:39 pm

Your code doesn't print anything.

Instead try something like:
while(scanf("%lld %lld",&h,&e) == 2) {
Check input and AC output for thousands of problems on uDebug!

Scorvus
New poster
Posts: 4
Joined: Sun Apr 21, 2013 8:19 pm

Re: 10055 - Hashmat the Brave Warrior

Post by Scorvus » Tue Apr 23, 2013 4:41 pm

what this line do?

while(scanf("%lld %lld",&h,&e) == 2) {

i'm sorry, i'm new with this thing.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10055 - Hashmat the Brave Warrior

Post by brianfry713 » Wed Apr 24, 2013 12:11 am

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.
Check input and AC output for thousands of problems on uDebug!

Post Reply

Return to “Volume 100 (10000-10099)”