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

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

Re: 10055 - Hashmat the Brave Warrior

Post by Scorvus » Wed Apr 24, 2013 3:36 pm

It works! thanks a lot :D
btw if i want to read only 1 input until the end of input, does the code look like this?
while(scanf("%lld",&e) == 1) {

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 11:30 pm

yes
Check input and AC output for thousands of problems on uDebug!

bonsu
New poster
Posts: 3
Joined: Fri May 24, 2013 5:55 pm

10055 - Hashmat the Brave Warrior - Query

Post by bonsu » Sun May 26, 2013 6:16 am

This code passes

Code: Select all

#include <iostream>
#include <cstdlib>

using namespace std;

int main ()
{
    long long a, b; 
    while(cin >> a >> b)
    {
        if (cin.eof())
           return 0;
       cout << abs(a-b) << endl;
    }
}

This code doesn't

Code: Select all

#include <iostream>
#include <cmath>

using namespace std;

int main ()
{
    long long a, b; 
    while(cin >> a >> b)
    {
        if (cin.eof())
           return 0;
       cout << abs(a-b) << endl;
    }
}
Does anybody know why? I think it may be because the implementation of abs() differs between cstdlib and cmath.

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 May 29, 2013 12:13 am

Yes, see:
http://www.cplusplus.com/reference/cstdlib/abs/
http://www.cplusplus.com/reference/cmath/abs/
http://www.cplusplus.com/reference/cstdlib/llabs/

You could use llabs() instead to make sure you get a long long return value and not a double.
Try input:

Code: Select all

4294967296 0
Output should be 4294967296
Check input and AC output for thousands of problems on uDebug!

alina12
New poster
Posts: 1
Joined: Wed May 29, 2013 11:57 am

Re: 10055 - Hashmat the Brave Warrior

Post by alina12 » Wed May 29, 2013 11:59 am

HI,Guys
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...
RUNTIME ERROR is the only verdict i'm getting

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

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 May 29, 2013 11:50 pm

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

ron2794
New poster
Posts: 4
Joined: Thu May 23, 2013 7:13 pm

10005 : Hashmat army : WA

Post by ron2794 » Sun Jun 02, 2013 8:24 pm

i am embarassed to get a WA on such an easy question but i don't know why . please check following :

Code: Select all

#include <iostream>
#include <cmath>
using namespace std;

int main(int argc, char *argv[]){
	long long int a,b;
	while(cin >> a >> b){
		cout << abs(b- a) << endl;
	}
	return 0;
}

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

Re: 10005 : Hashmat army : WA

Post by brianfry713 » Tue Jun 11, 2013 12:42 am

Try using llabs from cstdlib instead.
Check input and AC output for thousands of problems on uDebug!

Roseoop
New poster
Posts: 1
Joined: Tue Nov 05, 2013 9:20 am

Re: 10055 - Hashmat the Brave Warrior

Post by Roseoop » Tue Nov 05, 2013 9:27 am

I want to ask that the last sentence in the question "Hashmat's soldier number is never greater than his opponent"!!! we can just calculate the result of opponent number - soldier number .But why we have to calculate the abs of the result????

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 Nov 05, 2013 9:16 pm

The input contains two integer numbers in every line. These two numbers in each line denotes the number of soldiers in Hashmat's army and his opponent's army or vice versa.
Check input and AC output for thousands of problems on uDebug!

A.D.S
New poster
Posts: 1
Joined: Thu Jan 02, 2014 2:46 pm

Hashmat the Brave Warrior

Post by A.D.S » Thu Jan 02, 2014 2:54 pm

hi
i'm new to programming contest.
what's wrong with my code:

Code: Select all

#include<stdio.h>
#include<stdlib.h>
int main()
{
	long long int num1;
	long long int num2;
	while(scanf("%lld %lld",&num1,&num2) !=EOF)
	printf("%lld\n",abs(num2-num1));
	return 0;
}
the UVA's gcc returns wrong answer.

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

Re: Hashmat the Brave Warrior

Post by brianfry713 » Wed Jan 15, 2014 12:52 am

Try using llabs()
Check input and AC output for thousands of problems on uDebug!

richatibrewal
New poster
Posts: 49
Joined: Mon Jun 16, 2014 7:40 pm

Re: 10055 - Hashmat the Brave Warrior

Post by richatibrewal » Tue Oct 07, 2014 10:22 am

Hii, The following codefor problem no 10055 has been accepted with runtime of 0.072s. How can i improve this code??

Code: Select all

#include<cstdio>
using namespace std;
int main()
{
    long long int a,b;
    while(scanf("%lld%lld",&a,&b)==2)
    {
        if(a>b)
            printf("%lld\n",a-b);
        else
            printf("%lld\n",b-a);
    }
    return 0;
}

lighted
Guru
Posts: 585
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 10055 - Hashmat the Brave Warrior

Post by lighted » Tue Oct 07, 2014 10:40 am

filigabriel wrote:
technobug wrote:How fast is your D+C algorithm? What is its idea?
My original program runs 8) in 1.150 seconds

Main idea was already posted by Eduard :D

I've managed for optimization, and now runs in 0.771. Showing that scanf is slow.
mf wrote:You can use functions fread() and read() to grab large blocks of input into your own buffers, then parse manually it. This way you'll avoid the overhead of calling scanf() millions times.
It will approximately save you 0.500-0.600 sec. on this problem.
When input is big we can parse input manually to optimize reading time.
You can submit your code several times. :D
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman

richatibrewal
New poster
Posts: 49
Joined: Mon Jun 16, 2014 7:40 pm

Re: 10055 - Hashmat the Brave Warrior

Post by richatibrewal » Thu Oct 09, 2014 10:23 am

But my code got accepted in 0.072 s. Plz tell a solution so that it takes minimum time to run.

Post Reply

Return to “Volume 100 (10000-10099)”