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

Post Reply
User avatar
shamim
A great helper
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Bozeman, Montana, USA

Post by shamim » Sat Mar 10, 2007 12:10 pm

There is a lot of data in the input file.
So you could speed up the input/output process.
Usually, people use fread and fwrite to speed up I/O.

mnthk
New poster
Posts: 3
Joined: Mon Mar 12, 2007 7:42 pm
Location: Bangladesh
Contact:

10055

Post by mnthk » Tue Mar 13, 2007 11:05 pm

hey!
*use simply double data type
* subtract the low value from the high value always.
Then u get AC
bye
Born to be a programmer!

Zikas
New poster
Posts: 1
Joined: Sun Mar 02, 2008 6:11 pm

10055 Hashmat the brave warrior ( How it could be wrong !! )

Post by Zikas » Sun Mar 02, 2008 6:15 pm

Hi all, I solve this problem and the judge say that something wrong and I can't get it help please

The problem : http://icpcres.ecs.baylor.edu/onlinejud ... roblem=996

My code :

#include<iostream>
using namespace std;
int main()
{
int x, y;
while(cin >> x >> y)
{
cout << y-x << endl;
}
return 0;
}

Thanks

CSEDU_1323
New poster
Posts: 10
Joined: Mon Feb 25, 2008 8:22 pm
Location: Dhaka, Bangladesh.

Post by CSEDU_1323 » Mon Mar 03, 2008 3:53 pm

vice versa x<y or x>y is possible
so D result = greater num - smaller num
&& use long long
--- B HAPPY & KEEP SMILING ------

scott1991
New poster
Posts: 28
Joined: Fri Dec 07, 2007 11:41 pm

Post by scott1991 » Sun Mar 09, 2008 7:59 pm

WHAT AM I DOING WRONG?????

Code: Select all

#include <iostream>

int main()
{
	unsigned int a, s,temp;
	while(scanf("%u%u",&a,&s)==2)
	{
		if(s<a)
		{
		temp=s;
		s=a;
		a=temp;
		}
		printf("%u\n",s-a);
	}
	return 0;
}

User avatar
emotional blind
A great helper
Posts: 383
Joined: Mon Oct 18, 2004 8:25 am
Location: Bangladesh
Contact:

Post by emotional blind » Sun Mar 09, 2008 8:58 pm

scott1991 wrote:WHAT AM I DOING WRONG?????
input can be as large as 2^32, but unsigned integer can hold 2^32-1 at maximum.

lnr
Experienced poster
Posts: 142
Joined: Sat Jun 30, 2007 2:52 pm
Location: Dhaka,Bangladesh

Re: 10055 - Hashmat the Brave Warrior

Post by lnr » Wed Jul 02, 2008 5:02 am

starrynight says
Help!! still GOT WA
int main()
{
long long int num1 ,num2,answer;

while(scanf("%lld %lld",&num1,&num2) != EOF)
{
if(num1 > num2)
{
answer = num1 - num2;
printf("%lld\n",answer);
}
else
{
answer = num2 - num1;
printf("%lld\n",answer);
}

}


return 0;
}

I wrote this in C!
why I still got a WA~?
thx! :o

You can try putting this before the main function as global varibles and also selecting C++ compiler.
long long int num1 ,num2,answer;

Code: Select all

long long int num1 ,num2,answer;
int main()
{
while(scanf("%lld %lld",&num1,&num2) != EOF)
{
if(num1 > num2)
{
answer = num1 - num2;
printf("%lld\n",answer);
}
else
{
answer = num2 - num1;
printf("%lld\n",answer);
}

}


return 0;
}

It may help.

zyxw
New poster
Posts: 24
Joined: Sat Mar 22, 2008 5:49 am
Location: Chennai
Contact:

10055 - RTE in JAVA (SOLVED)

Post by zyxw » Thu Jul 03, 2008 9:39 am

I solved this problem long ago using C++ (which is my usual language).
I am a newbie in Java...
I wanted to use Java in UVa. So i chose a easy problem(10055) to start with.
But... :o
RUNTIME ERROR is the only verdict i'm getting :oops:

I don't think there is any error in algorithm :wink:
So i'm posting in Java forum.
What might be the mistake? Is my method of taking input wrong? If so, what is the correct way??

Here is the code: ( Works fine in my computer )

Code: Select all

Thanks to mf... Got AC...
I am not totally useless, because I can still be used as a bad example :P

zyxw
New poster
Posts: 24
Joined: Sat Mar 22, 2008 5:49 am
Location: Chennai
Contact:

10055 - RTE in JAVA

Post by zyxw » Sun Jul 06, 2008 2:29 pm

Hi.. I had posted in Java forum. But no reply :-?
So i'm posting here...

I solved this problem using C++ (which is my usual language).
I am a newbie in Java...
I wanted to use Java in UVa. So i chose a easy problem(10055) to start with.
But... :o
RUNTIME ERROR is the only verdict i'm getting :oops:

Is my method of taking input wrong?
If so, what is the correct way??

Here is the code: ( Works fine in my computer )

Code: Select all

Code deleted after AC...  Thanks to mf...
Last edited by zyxw on Sun Jul 06, 2008 7:44 pm, edited 1 time in total.
I am not totally useless, because I can still be used as a bad example :P

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: 10055 - Hashmat the Brave Warrior

Post by mf » Sun Jul 06, 2008 4:45 pm

Java's int can hold values in range 2^31 to 2^31-1.
Integer.parseInt(...) will throw an exception if its input lies outside this range, and for some cases in this problem it does. An uncaught exception is then reported to you as a runtime error.

zyxw
New poster
Posts: 24
Joined: Sat Mar 22, 2008 5:49 am
Location: Chennai
Contact:

Re: 10055 - Hashmat the Brave Warrior

Post by zyxw » Sun Jul 06, 2008 5:05 pm

Thanks mf for quick reply...

1) Should i use BigInteger?
2) I am getting RTE for another problem also (10519 - REALLY STRANGE).
I have posted here...
http://online-judge.uva.es/board/viewto ... =30#p95457

Since i'm getting RTE in both programs, i thought there might be some problem with taking input :roll:
I am not totally useless, because I can still be used as a bad example :P

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: 10055 - Hashmat the Brave Warrior

Post by mf » Sun Jul 06, 2008 6:06 pm

You can use BigInteger if you like, but long type (which is a 64-bit integer) would be sufficient and more simple. But you should parse a string to long with Long.parseLong() method.
2) I am getting RTE for another problem also (10519 - REALLY STRANGE).
I have posted here...
viewtopic.php?f=25&t=12321&st=0&sk=t&sd=a&start=30#p95457
I don't know what's wrong there.
Perhaps, there are leading/trailing whitespace on some lines in the input. Try to use java.util.Scanner to parse input, it can take care of these whitespace.

zyxw
New poster
Posts: 24
Joined: Sat Mar 22, 2008 5:49 am
Location: Chennai
Contact:

Thanks a million MF...

Post by zyxw » Sun Jul 06, 2008 6:53 pm

Got AC after 10 RTEs :)

Thanks a million mf.

Initially when i had difficulty in reading input in Java, i followed your suggestion here...
http://online-judge.uva.es/board/viewto ... e02#p73516
Its nice to see the same person helping me now :wink:
Try to use java.util.Scanner to parse input, it can take care of these whitespace.
Can you please show how to use it or can you give some links to read about them?

P.S: Should i remove my code from this thread and from Java forum?
I am not totally useless, because I can still be used as a bad example :P

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: Thanks a million MF...

Post by mf » Sun Jul 06, 2008 7:37 pm

Here's a sample code which shows how to use Scanner:

Code: Select all

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner in = new Scanner(new BufferedReader(
            new InputStreamReader(System.in)));

        while (in.hasNextBigInteger()) {
            java.math.BigInteger n = in.nextBigInteger();
            System.out.printf("%s^2 = %s\n", n, n.multiply(n));  // do something
        }
    }
}
As always, you can find all the documentation about Scanner in its javadocs:
http://java.sun.com/j2se/1.5.0/docs/api ... anner.html

zyxw
New poster
Posts: 24
Joined: Sat Mar 22, 2008 5:49 am
Location: Chennai
Contact:

Re: 10055 - RTE in JAVA (SOLVED)

Post by zyxw » Sun Jul 06, 2008 7:48 pm

After following mf's suggestion, got AC :lol:
http://online-judge.uva.es/board/viewto ... 2e0#p95456
I am not totally useless, because I can still be used as a bad example :P

Post Reply

Return to “Volume 100 (10000-10099)”