#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;
}
}
#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.
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??
#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;
}
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????
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!
#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;
}
#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;
}
technobug wrote:How fast is your D+C algorithm? What is its idea?
My original program runs in 1.150 seconds
Main idea was already posted by Eduard
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.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman