10055 - Hashmat the Brave Warrior
Moderator: Board moderators
-
- 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??
[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??
-
- Experienced poster
- Posts: 136
- Joined: Tue Apr 01, 2003 6:59 am
- Location: Jakarta, Indonesia
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.
P.S. I did not try including stdlib and sending to the judge, but that should be the standard.
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]
[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]
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.
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.
Hm, I think problem isn't in <<endl; after cout . I submited something that:
and I recived WA. When I changed
to
I get AC. Maybe someone know where is the problem.
Thanks
Code: Select all
.....
while(!cin.eof())
{
cin>>liczba1>>liczba2;
if(liczba1<liczba2)
{
cout<<(liczba2-liczba1)<<endl;
}
if(liczba1>=liczba2)
{
cout<<(liczba1-liczba2)<<endl;
}
}
Code: Select all
while(!cin.eof())
to
Code: Select all
while(cin>>liczba1>>liczba2)
Thanks
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
#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