10035 - Primary Arithmetic
Moderator: Board moderators
why wrong answer????
Can anybody plz tell me whats wrong with my code????I didnt find anything wrong with output.But why wrong answer???
#include<stdio.h>
#include<string.h>
int toint(char s);
long addstr(char s[],char t[]);
int main(void)
{
char s[100];
char t[100];
int i;
long result;
while(scanf("%s %s",s,t)==2)
{
if((strlen(s)==1 && strlen(t)==1) && (s[0]=='0'&&t[0]=='0'))
break;
result=addstr(s,t);
if(result==0)
printf("No carry operation.\n");
else
printf("%ld carry operations.\n",result);
for(i=0;i<100;i++)
{
s=t='\0';
}
}
return 0;
}
long addstr(char s[],char t[])
{
long lenS,lenT,carry,carry_count,i,j,temp,temp1,temp2;
lenS=strlen(s);
lenT=strlen(t);
for(i=lenS-1,j=lenT-1,carry=0,carry_count=0;i>=0||j>=0;i--,j--)
{
if(carry)
++carry_count;
if(i>=0)
temp1=toint(s);
if(j>=0)
temp2=toint(t[j]);
temp=temp1+temp2+carry;
carry=temp/10;
}
while(carry!=0)
{
if(carry)
++carry_count;
carry/=10;
}
return carry_count;
}
int toint(char s)
{
return s-'0';
}
#include<stdio.h>
#include<string.h>
int toint(char s);
long addstr(char s[],char t[]);
int main(void)
{
char s[100];
char t[100];
int i;
long result;
while(scanf("%s %s",s,t)==2)
{
if((strlen(s)==1 && strlen(t)==1) && (s[0]=='0'&&t[0]=='0'))
break;
result=addstr(s,t);
if(result==0)
printf("No carry operation.\n");
else
printf("%ld carry operations.\n",result);
for(i=0;i<100;i++)
{
s=t='\0';
}
}
return 0;
}
long addstr(char s[],char t[])
{
long lenS,lenT,carry,carry_count,i,j,temp,temp1,temp2;
lenS=strlen(s);
lenT=strlen(t);
for(i=lenS-1,j=lenT-1,carry=0,carry_count=0;i>=0||j>=0;i--,j--)
{
if(carry)
++carry_count;
if(i>=0)
temp1=toint(s);
if(j>=0)
temp2=toint(t[j]);
temp=temp1+temp2+carry;
carry=temp/10;
}
while(carry!=0)
{
if(carry)
++carry_count;
carry/=10;
}
return carry_count;
}
int toint(char s)
{
return s-'0';
}
-
- New poster
- Posts: 4
- Joined: Fri May 15, 2009 2:36 pm
Re: 10035 - Primary Arithmetic
Have tested a very big amount of variants but can't still find a bug...
Please point the variant it worcs incorrectly)))
THANK YOU VERY MUCH!!!
Please point the variant it worcs incorrectly)))
THANK YOU VERY MUCH!!!
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main(void)
{
short int t1,t2,i,j,t,S=0;
unsigned short int k,m,z=0;
unsigned char a=0;
char s[25],*s1="0 0";
FILE *fin,*fout;
fin=fopen("in.txt","r");
fout=fopen("out.txt","w");
fgets(s,24,fin);
while(strcmp(s,s1)!=0)
{
if (S==1) fprintf(fout,"%c",'\n');
S=1;
for (i=0;s[i]!=' ';i++);
a=0;
z=0;
i--;
j=strlen(s)-1-1;
t=strlen(s)-(i+1)-1-1;
if (i+1>t) k=i+1; else k=t;
for (m=0;m<k;m++,i--,j--,t--)
{
if (i<0) t1=0;
else t1=s[i]-48;
if (t-1<0)t2=0;
else t2=s[j]-48;
if (t1+t2+a>9)
{z++; a=1;}
else a=0;
}
if (z==0) fprintf(fout,"%s","No carry operation.");
else if (z==1) fprintf(fout,"%s","1 carry operation.");
else fprintf(fout,"%i %s",z,"carry operations.");
fgets(s,24,fin);
}
close (fin);
close(fout);
return 0;
}
Re: 10035 - Primary Arithmetic
Well, this is wrong:
You're supposed to read from standard input and write to standard output. Learn about I/O redirection.
And this:
close() closes file descriptors. For FILE* objects you must use fclose().
And this, in more ways than one:
I'd use:
And this:
Every line of a text file, including the last one, must be terminated by '\n' on Unix systems.
'\n' is a line terminator, not delimiter as it used to be on MS-DOS.
Code: Select all
FILE *fin,*fout;
fin=fopen("in.txt","r");
fout=fopen("out.txt","w");
And this:
Code: Select all
close (fin);
close(fout);
And this, in more ways than one:
Code: Select all
char s[25],*s1="0 0";
...
fgets(s,24,fin);
while(strcmp(s,s1)!=0)
Code: Select all
char str1[100], str2[100];
while(scanf("%s %s", str1, str2) == 2) { ... }
Code: Select all
if (S==1) fprintf(fout,"%c",'\n');
S=1;
'\n' is a line terminator, not delimiter as it used to be on MS-DOS.
-
- New poster
- Posts: 4
- Joined: Fri May 15, 2009 2:36 pm
Re: 10035 - Primary Arithmetic
Thank you very much for your answer, but I've posted only here a variant with file input/output. I used stadart I/O to post the programm to judge..)) I thought it would be easier for you to test my programm with file I/O...))
Is there any found incorrect answers?
So, the variant with standart I/O:
Is there any found incorrect answers?
So, the variant with standart I/O:
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main(void)
{
short int t1,t2,i,j,t;
unsigned short int k,m,z=0;
unsigned char a=0;
char s[25],*s1="0 0";
gets(s);
while(strcmp(s,s1)!=0)
{
for (i=0;s[i]!=' ';i++);
a=0;
z=0;
i--;
j=strlen(s)-1-1;
t=strlen(s)-(i+1)-1-1;
if (i+1>t) k=i+1; else k=t;
for (m=0;m<k;m++,i--,j--,t--)
{
if (i<0) t1=0;
else t1=s[i]-48;
if (t-1<0)t2=0;
else t2=s[j]-48;
if (t1+t2+a>9)
{z++; a=1;}
else a=0;
}
if (z==0) printf("%s","No carry operation.\n");
else if (z==1) printf("%s","1 carry operation.\n");
else printf("%i %s",z,"carry operations.\n");
gets(s);
}
return 0;
}
Re: 10035 - Primary Arithmetic
Well, when I run your code on this input:
I get this output from it, which certainly doesn't look right:
(If you can't reproduce this, I'd suggest you try to compile and run your program under Linux, e.g. get Knoppix or Ubuntu livecd - they should have gcc)
Also, your code doesn't work when there are any leading whitespace, trailing whitespace or more than one space between the numbers. In most problems on this judge your programs are supposed to tolerate such extra whitespace.
You don't have to memorize the whole ASCII table. In C/C++, character literals represent the integer ASCII value of their character, so just type '0' instead 48.
Code: Select all
123 456
555 555
123 594
1 990
990 1
1 999
999 1
0 0
Code: Select all
No carry operation.
2 carry operations.
1 carry operation.
2 carry operations.
No carry operation.
2 carry operations.
No carry operation.
Nope, I find it's a lot easier when I get to specify exactly what files I want my program to read from and write to on the command line via I/O redirection, then when I have to fix hardcoded filenames in them and recompile.Slava_Edelev wrote:I thought it would be easier for you to test my programm with file I/O...))
Also, your code doesn't work when there are any leading whitespace, trailing whitespace or more than one space between the numbers. In most problems on this judge your programs are supposed to tolerate such extra whitespace.
else t1=s-48;
You don't have to memorize the whole ASCII table. In C/C++, character literals represent the integer ASCII value of their character, so just type '0' instead 48.
-
- New poster
- Posts: 4
- Joined: Fri May 15, 2009 2:36 pm
Re: 10035 - Primary Arithmetic
THANK YOU VERY MUCH!!!!!!!!!!!!! I get AC:)))))))))))))))))))
Your comments are very instructive, I know they will certainly help me also in other problems!:)
I don't know why our answers are different (but maybe it depends with instead of , as you've said).
The bug in my code was connected with whitespaces, thank you very much, that you mentioned it!)))
By the way, I'm fron Russia too))) The Bonch-Bruyevich St.Petersburgh State University of Telecommunications)))
P.S.: if it is possible, could you help me with problem Bridge 10037, I've posted in the theme, but nobody answered...
http://online-judge.uva.es/board/viewto ... e377e6c499
Your comments are very instructive, I know they will certainly help me also in other problems!:)
I don't know why our answers are different (but maybe it depends with
Code: Select all
t1=s[i]-48;
Code: Select all
t1=s[i]-'0'
The bug in my code was connected with whitespaces, thank you very much, that you mentioned it!)))
By the way, I'm fron Russia too))) The Bonch-Bruyevich St.Petersburgh State University of Telecommunications)))
P.S.: if it is possible, could you help me with problem Bridge 10037, I've posted in the theme, but nobody answered...
http://online-judge.uva.es/board/viewto ... e377e6c499
10035 - Primary Arithmetic
can anybody help me?????This is my code below
#include<stdio.h>
#include<string.h>
int main()
{
int m,n,carry,c,i,x;
char s[10],r[10];
while(scanf("%s %s",s,r)!=EOF)
{
if(s[0]=='0' && r[0]=='0')
{
break;
}
m=strlen(s);
n=strlen(r);
if(m>n)
{
i=n;
}
else
{
i=m;
}
carry=0;
c=0;m--;n--;
for(i=i;i>=1;i--)
{
x=(s[m]-'0')+(r[n]-'0');
carry=x/10;
if(carry==1)
{
c++;
}
if(i==1 && carry==1)
{
if(m>n && s[m-1]=='9')
{
for(m=m-1;m>=0;m--)
{
x=carry+(s[m]-'0');
carry=x/10;
if(carry==0)
{
break;
}
if(carry==1)
{
c++;
}
}
}
if(n>m && r[n-1]=='9')
{
for(n=n-1;n>=0;n--)
{
x=carry+r[n]-'0';
carry=x/10;
if(carry==0)
{
break;
}
if(carry==1)
{
c++;
}
}
}
}
m--;n--;
}
if(c==0 || c==1)
{
if(c==1)
{
printf("%d carry operation.\n",c);
}
else
{
printf("No carry operation.\n");
}
}
else
{
printf("%d carry operations.\n",c);
}
}return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
int m,n,carry,c,i,x;
char s[10],r[10];
while(scanf("%s %s",s,r)!=EOF)
{
if(s[0]=='0' && r[0]=='0')
{
break;
}
m=strlen(s);
n=strlen(r);
if(m>n)
{
i=n;
}
else
{
i=m;
}
carry=0;
c=0;m--;n--;
for(i=i;i>=1;i--)
{
x=(s[m]-'0')+(r[n]-'0');
carry=x/10;
if(carry==1)
{
c++;
}
if(i==1 && carry==1)
{
if(m>n && s[m-1]=='9')
{
for(m=m-1;m>=0;m--)
{
x=carry+(s[m]-'0');
carry=x/10;
if(carry==0)
{
break;
}
if(carry==1)
{
c++;
}
}
}
if(n>m && r[n-1]=='9')
{
for(n=n-1;n>=0;n--)
{
x=carry+r[n]-'0';
carry=x/10;
if(carry==0)
{
break;
}
if(carry==1)
{
c++;
}
}
}
}
m--;n--;
}
if(c==0 || c==1)
{
if(c==1)
{
printf("%d carry operation.\n",c);
}
else
{
printf("No carry operation.\n");
}
}
else
{
printf("%d carry operations.\n",c);
}
}return 0;
}
10035 - Primary Arithmetic
What's the problem with my following code. I am getting WA.Please help me.
Code: Select all
#include<stdio.h>
int main(){
long m,n,i,j,c,l;
while(scanf("%lld %lld",&m,&n)==2 && (m || n)){
c=0;
l=0;
for(i=m,j=n;i!=0 || j!=0;i/=10,j/=10){
if(i%10+j%10+c>9){
c=1;
l++;
}
}
if(l==0) printf("No carry operation.\n",l);
else if(l==1) printf("1 carry operation.\n");
else printf("%lld carry operations.\n",l);
}
return 0;
}
Re: 10035 - Primary Arithmetic
p.cc: In function ‘int main()’:
p.cc:6: warning: format ‘%lld’ expects type ‘long long int*’, but argument 2 has type ‘long int*’
p.cc:6: warning: format ‘%lld’ expects type ‘long long int*’, but argument 3 has type ‘long int*’
p.cc:17: warning: too many arguments for format
p.cc:19: warning: format ‘%lld’ expects type ‘long long int’, but argument 2 has type ‘long int’
And you never clear the carry flag (variable c) after it is set to 1.
p.cc:6: warning: format ‘%lld’ expects type ‘long long int*’, but argument 2 has type ‘long int*’
p.cc:6: warning: format ‘%lld’ expects type ‘long long int*’, but argument 3 has type ‘long int*’
p.cc:17: warning: too many arguments for format
p.cc:19: warning: format ‘%lld’ expects type ‘long long int’, but argument 2 has type ‘long int’
And you never clear the carry flag (variable c) after it is set to 1.
-
- New poster
- Posts: 4
- Joined: Mon Jun 08, 2009 11:52 pm
Re: 10035 - Primary Arithmetic
i cant find any problm of my code bt it gives WA... it seems right.. plz help..
Code: Select all
Code deleted
Last edited by farhan_iut on Wed Jun 23, 2010 10:54 pm, edited 1 time in total.
Re: 10035 - Primary Arithmetic
I am a new programmer. Can any 1 help me ? I am getting wa in the problem 10035 -Primary Arithmetic.
Here is my code.
Here is my code.
Code: Select all
#include<stdio.h>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<cctype>
#include<cmath>
using namespace std;
int ar1[11]={0},ar2[11]={0},h;
void arr1(long long x)
{
long long b=0;
int a,p;
memset( ar1, 0, sizeof( ar1) );
while(x)
{
a=(int)x%10;
x/=10;
ar1[b]=a;
b++;
}
for(p=b;p<11;p++)
{
ar1[p]=-1;
}
}
void arr2(long long y)
{
long long d=0;
int c;
memset( ar2, 0, sizeof( ar2) );
while(y)
{
c=(int)y%10;
y/=10;
ar2[d]=c;
d++;
}
}
int main()
{
long long n,m,t,i,cry=0,h,k;
while(scanf("%lld %lld",&n,&m)==2)
{
cry=0;
if(n==0 && m==0)
{
break;
}
if(m>n)
{
t=n;
n=m;
m=t;
}
arr1(n);
arr2(m);
for(i=0;(ar1[i])>=0;i++)
{
if((ar1[i]+ar2[i])>=10)
{
cry++;
k=1+i;
h=ar1[k];
h++;
ar1[k]=h;
}
}
if(cry==0)
{
printf("No carry operation.\n");
}
else
{
printf("%lld carry operations.\n",cry);
}
}
return 0;
}
-
- New poster
- Posts: 2
- Joined: Mon Apr 12, 2010 5:51 pm
Re: 10035 - Primary Arithmetic
I don't know why I get wrong answer
Code: Select all
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int main()
{
int men, i, cont=0;
char num1[10], num2[10];
bool carry=false;
cin>>num1>>num2;
while(strcmp(num1,"0")!=0 and strcmp(num2,"0")!=0)
{
reverse(num1, num1+strlen(num1));
reverse(num2, num2+strlen(num2));
if(strlen(num1)>strlen(num2))
men=strlen(num2);
else
men=strlen(num1);
for(i=0; i<men; i++)
{
if(carry==true)
{if(((num1[i]-48)+(num2[i]-48))+1>9)
{
carry=true;
cont++;
}
else
carry=false;
}
else
{
if(((num1[i]-48)+(num2[i]-48))>9)
{carry=true;
cont++;
}
}
}
if(cont==0)
cout<<"No carry operation."<<endl;
else if(cont==1)
cout<<"1 carry operation."<<endl;
else
cout<<cont<<" carry operations."<<endl;
cont=0;
carry=false;
cin>>num1>>num2;
}
return 0;
}
10035 - primary arithmetic
can anyone please help me with the code below..
i can't understand why am i getting WA
//primary arithmetic
#include <stdio.h>
int main()
{
unsigned long int a,b;
int carry;
while(scanf("%lu %lu",&a,&b)==2)
{
if(a==0 && b==0)
break;
carry=0;
while((a%10)||(b%10))
{
if((a%10+b%10)>9)
carry++;
a/=10;
b/=10;
}
if(carry==0)
printf("No carry operation.\n");
else if(carry==1)
printf("%d carry operation.\n",carry);
else
printf("%d carry operations.\n",carry);
}
return 0;
}
i can't understand why am i getting WA
//primary arithmetic
#include <stdio.h>
int main()
{
unsigned long int a,b;
int carry;
while(scanf("%lu %lu",&a,&b)==2)
{
if(a==0 && b==0)
break;
carry=0;
while((a%10)||(b%10))
{
if((a%10+b%10)>9)
carry++;
a/=10;
b/=10;
}
if(carry==0)
printf("No carry operation.\n");
else if(carry==1)
printf("%d carry operation.\n",carry);
else
printf("%d carry operations.\n",carry);
}
return 0;
}
Re: 10035 - primary arithmetic
13 pages of discussions here: http://acm.uva.es/board/viewtopic.php?f=9&t=4057
Another reminder: Don't create a new thread for a problem that already exists! Make your post in an existing thread.
Another reminder: Don't create a new thread for a problem that already exists! Make your post in an existing thread.
-
- New poster
- Posts: 4
- Joined: Tue Sep 21, 2010 4:17 pm
- Location: Islamic University, Kushtia
- Contact:
Re: 10035 - primary arithmetic
Please help me. My program is not give right ans.[code#include<stdio.h>
int main()
{
long int n,m,a[100]={0},b[100]={0},i,h,c,d,t,r,k,l=1;
while(scanf("%ld%ld",&n,&m)!=EOF)
{
if(n==0&&m==0)
break;
c=0;
for(i=0;n!=0;i++)
{
a=n%10;
n=n/10;
c=c+1;
}
d=0;
for(i=0;m!=0;i++)
{
b=m%10;
m=m/10;
d=d+1;
}
if(c>d)
t=c;
else
t=d;
h=0;
k=0;
for(i=0;i<t;i++)
{
r=0;
r=a+b+k;
if(r>9)
{
h=h+1;
k=1;
}
}
if(h==0)
printf("No carry operation.\n");
else
if(h==1)
printf("1 carry operation.\n");
else
printf("%ld carry operations.\n",h);
}
return 0;
}][/code]
int main()
{
long int n,m,a[100]={0},b[100]={0},i,h,c,d,t,r,k,l=1;
while(scanf("%ld%ld",&n,&m)!=EOF)
{
if(n==0&&m==0)
break;
c=0;
for(i=0;n!=0;i++)
{
a=n%10;
n=n/10;
c=c+1;
}
d=0;
for(i=0;m!=0;i++)
{
b=m%10;
m=m/10;
d=d+1;
}
if(c>d)
t=c;
else
t=d;
h=0;
k=0;
for(i=0;i<t;i++)
{
r=0;
r=a+b+k;
if(r>9)
{
h=h+1;
k=1;
}
}
if(h==0)
printf("No carry operation.\n");
else
if(h==1)
printf("1 carry operation.\n");
else
printf("%ld carry operations.\n",h);
}
return 0;
}][/code]