10055 - Hashmat the Brave Warrior
Moderator: Board moderators
Re: 10055 - Hashmat the Brave Warrior
It works! thanks a lot
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) {
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) {
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10055 - Hashmat the Brave Warrior
yes
Check input and AC output for thousands of problems on uDebug!
10055 - Hashmat the Brave Warrior - Query
This code passes
This code doesn't
Does anybody know why? I think it may be because the implementation of abs() differs between cstdlib and cmath.
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;
}
}
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;
}
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10055 - Hashmat the Brave Warrior
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:Output should be 4294967296
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
Check input and AC output for thousands of problems on uDebug!
Re: 10055 - Hashmat the Brave Warrior
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??
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??
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10055 - Hashmat the Brave Warrior
See:
http://uva.onlinejudge.org/index.php?op ... &Itemid=30
http://uva.onlinejudge.org/index.php?op ... &Itemid=31
http://uva.onlinejudge.org/index.php?op ... &Itemid=30
http://uva.onlinejudge.org/index.php?op ... &Itemid=31
Check input and AC output for thousands of problems on uDebug!
10005 : Hashmat army : WA
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;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10005 : Hashmat army : WA
Try using llabs from cstdlib instead.
Check input and AC output for thousands of problems on uDebug!
Re: 10055 - Hashmat the Brave Warrior
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????
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10055 - Hashmat the Brave Warrior
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!
Hashmat the Brave Warrior
hi
i'm new to programming contest.
what's wrong with my code:
the UVA's gcc returns wrong answer.
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;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: Hashmat the Brave Warrior
Try using llabs()
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 49
- Joined: Mon Jun 16, 2014 7:40 pm
Re: 10055 - Hashmat the Brave Warrior
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
filigabriel wrote:My original program runs in 1.150 secondstechnobug wrote:How fast is your D+C algorithm? What is its idea?
Main idea was already posted by Eduard
I've managed for optimization, and now runs in 0.771. Showing that scanf is slow.
When input is big we can parse input manually to optimize reading time.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.
You can submit your code several times.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
-
- New poster
- Posts: 49
- Joined: Mon Jun 16, 2014 7:40 pm
Re: 10055 - Hashmat the Brave Warrior
But my code got accepted in 0.072 s. Plz tell a solution so that it takes minimum time to run.