## 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

Adil
Learning poster
Posts: 57
Joined: Sun Sep 29, 2002 12:00 pm
Location: in front of the monitor :-)
Contact:
hello.

by the statement, the problemsetter means that the smaller of the inputs (when they are not equal) is always the number of hashmat's soldiers. so you have to decide which input indicates the number of hashmat's soldiers. (this, however is not really needed to solve this prob)

soyoja
Experienced poster
Posts: 106
Joined: Sun Feb 17, 2002 2:00 am
Location: Seoul, South Korea
Contact:
Can I use unsigned integer data type?

I use unsined integer variable, but I got WA...

Anyone give me some hints?

Hisoka
Experienced poster
Posts: 120
Joined: Wed Mar 05, 2003 10:40 am
Location: Indonesia
I think not, because for input you must greater than unsigned int.

for this problem you can use long long int, double, or string (like lendlice).

soyoja
Experienced poster
Posts: 106
Joined: Sun Feb 17, 2002 2:00 am
Location: Seoul, South Korea
Contact:
At last, I got AC this problem. I use char array with my own "Big-Integer"
class. I think this problem fall under the big Integer problem category... ^^
thanks.

Joseph Kurniawan
Experienced poster
Posts: 136
Joined: Tue Apr 01, 2003 6:59 am
Location: Jakarta, Indonesia

### 10055 - Can't use gthe function abs()

With this code:
[c]
#include<stdio.h>
#include<math.h>
long long a,b;
void main(){
while(scanf("%lli %lli",&a,&b)!=EOF){
a-=b;
a=abs(a);
printf("%lli\n",a);
}
}
[/c]
I got WA.

But with this:
[c]
#include<stdio.h>
long long a,b;
void main(){
while(scanf("%lli %lli",&a,&b)!=EOF){
a-=b;
if(a<0) a=-a;
printf("%lli\n",a);
}
}
[/c]

I got AC.

Funny eh??

shamim
A great helper
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Bozeman, Montana, USA
the problem is with the data type.
I think abs can not handle the long long data type.

Joseph Kurniawan
Experienced poster
Posts: 136
Joined: Tue Apr 01, 2003 6:59 am
Location: Jakarta, Indonesia
Oh, I see. I suppose the function abs() can only work on int data type???

UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:
In stdlib.h:
[c]int abs (int n);
long labs (long n);[/c]
If you're using C++ 3.2 (OJ uses 2.95, so this probably won't work here), you also have in cstdlib:
[cpp]long abs (long n);
long long abs (long long n);
long long llabs (long long n);[/cpp]

mafattah
New poster
Posts: 23
Joined: Fri Apr 26, 2002 1:00 am
Location: Cairo, Egypt

### I had the same problem before

I had this same problem before. The problem would happen with int as well as with long long int. I think the reason is that in the standard C++ definition, abs is included in the <stdlib.h> or in <cstdlib>, but in MSVC++, they include it under <math.h>. It seems to be only another incompatibility from VC++.

P.S. I did not try including stdlib and sending to the judge, but that should be the standard.

Master
Learning poster
Posts: 82
Joined: Thu Oct 10, 2002 1:15 pm
Location: St. Johns, Canada
Contact:
If you use abs then you have to include stdlib.h and if the parameter must be integer. And if you use double then use fabs.

In this problem you do not need to use abs or fabs. But you can use a if function.

M H Rasel
CUET Old Sailor

someone
New poster
Posts: 2
Joined: Mon Aug 30, 2004 9:00 am

### 10055

why this don't work i get a WA
[cpp]#include <cstdio>
#include <string>
using namespace std;

char buffer[1000],a[500],b[500];

void uradi();

int main ()
{
while (fgets(buffer,1000,stdin))
{
sscanf(buffer,"%s%s",a,b);
uradi();
};
return 0;
};

void uradi ()
{
char chTemp[2];
int n,x=0;
n=strlen(b);
bool blnBIG=false;
chTemp[0]='\0';

for (int i=0;i<(n-strlen(a-1));i++){buffer='0';}
buffer[n-strlen(a)]='\0';
strcat(buffer,a);
strcpy(a,buffer);
buffer[0]='\0';

for(int i=n-1;i>=0;i--)
{

if (blnBIG){x=-1;blnBIG=false;};
x+=b-'0';
if ((a-'0')>x){x+=10;blnBIG=true;}else blnBIG = false;
x-=a-'0';
sprintf(chTemp,"%i",x);
strcat(buffer,chTemp);
x=0;
};

for(int i=strlen(buffer)-1;i>-1;i--)
{
if (buffer!='0')break; else buffer=0;
};

for (int i=strlen(buffer)-1;i>=0;i--)
{
printf("%c",buffer);
};
printf("\n");
};[/cpp]

hiloshi
New poster
Posts: 20
Joined: Fri Aug 27, 2004 8:15 am
Location: Kanagawa, Japan
hi.

This problem is the simple subtraction problem.
You should only calculate deference between two integer(Not Big-Number).
But be careful for magnitude correlation of two integer.
If not clearly written about an input, you must not assume anything.
Last edited by hiloshi on Mon Aug 30, 2004 5:55 pm, edited 1 time in total.
I hope you can understand my poor English.

Minilek
Learning poster
Posts: 90
Joined: Tue Jul 27, 2004 9:34 am
Location: Cambridge, MA
Contact:
unsigned long long for this problem was enough (conversion specifier %llu).

that might have been overkill, but i knew it would work for sure and just did the first thing that came to my head that i knew would fit. : )

Qbens
New poster
Posts: 5
Joined: Fri Apr 23, 2004 11:37 pm
Location: Poland
Contact:
Hm, I think problem isn't in <<endl; after cout . I submited something that:

Code: Select all

``````
.....

while(!cin.eof())
{
cin>>liczba1>>liczba2;
if(liczba1<liczba2)
{
cout<<(liczba2-liczba1)<<endl;
}

if(liczba1>=liczba2)
{
cout<<(liczba1-liczba2)<<endl;
}
}
``````
and I recived WA. When I changed

Code: Select all

``while(!cin.eof())``

to

Code: Select all

``while(cin>>liczba1>>liczba2)``
I get AC. Maybe someone know where is the problem.
Thanks

coolzero
New poster
Posts: 8
Joined: Fri Nov 12, 2004 11:33 am

### 10055 - Can't understand why WA

[c]
#include <stdio.h>
#include <stdlib.h>

int main() {

long n1=0, n2=0;

while(scanf("%ld %ld\n", &n1, &n2) == 2)
printf("%ld\n", labs(n2-n1));

return 0;

}
[/c]

Input:
10 12
10 14
100 200
12 10

Output:
2
4
100
2

Can anyone help?

Tanks