Page 11 of 12

Re: 10055 - Hashmat the Brave Warrior

Posted: Wed Apr 24, 2013 3:36 pm
by Scorvus
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) {

Re: 10055 - Hashmat the Brave Warrior

Posted: Wed Apr 24, 2013 11:30 pm
by brianfry713
yes

10055 - Hashmat the Brave Warrior - Query

Posted: Sun May 26, 2013 6:16 am
by bonsu
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.

Re: 10055 - Hashmat the Brave Warrior

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

Re: 10055 - Hashmat the Brave Warrior

Posted: Wed May 29, 2013 11:59 am
by alina12
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??

Re: 10055 - Hashmat the Brave Warrior

Posted: Wed May 29, 2013 11:50 pm
by brianfry713

10005 : Hashmat army : WA

Posted: Sun Jun 02, 2013 8:24 pm
by ron2794
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;
}

Re: 10005 : Hashmat army : WA

Posted: Tue Jun 11, 2013 12:42 am
by brianfry713
Try using llabs from cstdlib instead.

Re: 10055 - Hashmat the Brave Warrior

Posted: Tue Nov 05, 2013 9:27 am
by Roseoop
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????

Re: 10055 - Hashmat the Brave Warrior

Posted: Tue Nov 05, 2013 9:16 pm
by brianfry713
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.

Hashmat the Brave Warrior

Posted: Thu Jan 02, 2014 2:54 pm
by A.D.S
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.

Re: Hashmat the Brave Warrior

Posted: Wed Jan 15, 2014 12:52 am
by brianfry713
Try using llabs()

Re: 10055 - Hashmat the Brave Warrior

Posted: Tue Oct 07, 2014 10:22 am
by richatibrewal
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;
}

Re: 10055 - Hashmat the Brave Warrior

Posted: Tue Oct 07, 2014 10:40 am
by lighted
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

Re: 10055 - Hashmat the Brave Warrior

Posted: Thu Oct 09, 2014 10:23 am
by richatibrewal
But my code got accepted in 0.072 s. Plz tell a solution so that it takes minimum time to run.