10035 - Primary Arithmetic

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

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

Try giving 111 and 799 as input.
Ami ekhono shopno dekhi...
HomePage

rezaee
New poster
Posts: 8
Joined: Sun Dec 10, 2006 8:30 pm

10035 help me plz

Post by rezaee »

#include <iostream.h>
int main(void)
{int unsigned long a,b;
int s,z;
int unsigned long q[10],d[10];

while(cin>>a>>b)
{if((a==0)&&(b==0))return 0;
else{for(int i=1;i<=9;i++)
{q=0;d=0;}

for(int j=9;j>=1;j--)

{{q[j]=a%10;a=a/10;}{d[j]=b%10;b=b/10;}}
s=0;

for(int k=9;k>=1;k--)

{z=q[k]+d[k];
q[k-1]=q[k-1]+z/10;
if(z>=10)s++;
}
if(s!=0)cout<<s<<" "<<"carry operation."<<endl;
else cout<<"No carry operation."<<endl;}}


}


:oops: plz help[/b]

gislan
New poster
Posts: 1
Joined: Thu Nov 30, 2006 4:41 pm

Post by gislan »

rezaee, your code doesn't pass sample input.

Your output:
No carry operation.
3 carry operation.
1 carry operation.
Sample output:
No carry operation.
3 carry operations.
1 carry operation.
You need plural form of 'operation' if number of operations is more than 1.[/code]

max566
New poster
Posts: 3
Joined: Fri Dec 22, 2006 5:16 pm

10035 WA

Post by max566 »

please tell me where is wrong
I have tried many times,but I didn't find wrong.
Thx

=============================================
#include <iostream>

using namespace std;

int test(int n1, int n2)
{
int times=0;
int r_n1,r_n2;
while(n1!=0 || n2!=0)
{
r_n1 = n1 % 10;
r_n2 = n2 % 10;
n1/=10;
n2/=10;
if(r_n1+r_n2 >=10)
{
++times;
n1+=1;
}
}
return times;
}

int main()
{
int num1,num2;
while(cin >> num1 >> num2)
{
if(num1==0 && num2==0)
break;
int times=test(num1,num2);
if(times==0)
cout << "No carry operation." << endl;
else if(times==1)
cout << "1 carry operation." << endl;
else
cout << times << " carry operations." << endl;
}
return 0;
}

helloneo
Guru
Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea

Post by helloneo »

..
Last edited by helloneo on Sun Dec 21, 2008 5:27 pm, edited 1 time in total.

max566
New poster
Posts: 3
Joined: Fri Dec 22, 2006 5:16 pm

Post by max566 »

thank you
it's can get AC

helloneo
Guru
Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea

Post by helloneo »

..
Last edited by helloneo on Sun Dec 21, 2008 5:27 pm, edited 1 time in total.

Waddle
New poster
Posts: 22
Joined: Thu Jan 25, 2007 3:54 pm
Location: Taiwan
Contact:

10035 Output Limit Exceeded

Post by Waddle »

#include <string.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
char num1[12],num2[12];
int a[12],b[12],ans[12] ,carry[12], size,i=0,k,m,n,o;
while(1)
{
gets(num1);
gets(num2);

if ( num1[0]==48&&num2[0]==48 )
break;

m=strlen(num1);
n=strlen(num2);
if(m>n)
size=m+1;
else
size=n+1;

for(i=0;i<size;i++)
{
a[i]=0;
b[i]=0;
ans[i]=0;
carry[i]=0;
}
i=m-1;
for(k=size-1;i>=0;k--)
{
a[k]=num1[i]-48;
i--;
}
i=n-1;

for(k=size-1;i>=0;k--)
{
b[k]=num2[i]-48;
i--;
}
o=0;
for(k=size-1;k>=0;k--)
{
if(( a[k]+b[k] )/10>=1)
{
o++;
}
carry[k-1]=( a[k]+b[k] )/10;
ans[k]=( a[k]+b[k] )%10+carry[k];
}

if(o>1)
printf("%d carry operations.\n",o);
else if(o==1)
printf("1 carry operation.\n");
else
printf("No carry operation.\n");
}

return 0;
}





Help me , please!

shamim
A great helper
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Bozeman, Montana, USA

Post by shamim »

Code: Select all

while(1) 
{ 
gets(num1); 
gets(num2);
Your program will never stop taking inputs even when the end of file is reached.

Change it to:

Code: Select all

while ( gets(num1) != NULL )
gets(num2);

Waddle
New poster
Posts: 22
Joined: Thu Jan 25, 2007 3:54 pm
Location: Taiwan
Contact:

I'm sorry to say it didn't work.

Post by Waddle »

But I have this:

if( num1[0]==48&&num2[0]==48 )
break;

if i followed what you say, it became "Compile Error".

rio
A great helper
Posts: 385
Joined: Thu Sep 21, 2006 5:01 pm
Location: Kyoto, Japan

Post by rio »

Did you run your code with your PC?
Each line of input contains two unsigned integers less than 10 digits
gets() reads one line.

ADD: Don't open a new thread if it already exist for the problem.

pushpit.saxena
New poster
Posts: 4
Joined: Mon Jan 22, 2007 10:05 pm
Location: Allahabad
Contact:

plzzzzzzz help I m getting WA

Post by pushpit.saxena »

code removed after got AC....
Happy Coding!!!
Pushpit Saxena
B-Tech@IIIT-A

S.M. Arifuzzaman
New poster
Posts: 10
Joined: Sun Feb 11, 2007 1:01 pm
Location: [CSE,BUET],DHAKA,BANGLADESH.

10035

Post by S.M. Arifuzzaman »

// here is my code. whats the wrong??????????????????????

#include<stdio.h>
#include<string.h>



void main()

{
freopen("in.txt","r",stdin);
freopen("ot.txt","w",stdout);
char st1[10]={0},st2[10]={0};

while(scanf("%s%s",st1,st2)==2&&(strcmp(st1,"0")||strcmp(st2,"0")))
{

int carry=0,sum=0,total=0,flag=1;
int len1=strlen(st1),len2=strlen(st2);
if(len2>len1)
flag=0;


for(int k=len1-1;k>=0&&flag;k--)
{
len2--;
if(len2>=0)
{
sum=(st1[k]-48)+(st2[len2]-48)+carry;
if(sum>=10)
{

carry=sum/10;
total++;

}



}

else
{
sum=(st1[k]-48)+carry;
if(sum>=10)
{

carry=sum/10;
total++;

}

}


}



for(int p=len2-1;p>=0&&!flag;p--)
{
len1--;
if(len1>=0)
{
sum=(st2[p]-48)+(st1[len1]-48)+carry;
if(sum>=10)
{

carry=sum/10;
total++;

}



}

else
{
sum=(st2[p]-48)+carry;
if(sum>=10)
{

carry=sum/10;
total++;

}

}



}

if(total==0)
{

printf("No carry operation.\n");


}
else if(total==1)

printf("1 carry operation.\n");


else
printf("%d carry operations.\n",total);

}

}
S. M. Arifuzzaman......
He dreamXtreme..................
But good4nothing................................

shamim
A great helper
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Bozeman, Montana, USA

Post by shamim »

Disable smilie tags when posting codes.

Rushow
New poster
Posts: 14
Joined: Sat Oct 14, 2006 4:09 pm
Location: Dhaka,Bangladesh

10035-Primary Arithmetic

Post by Rushow »

I can't understand what is the wrong actually I did in this code. Please help me . My code is as follows:

Code: Select all

/* p10035 */
/* Primary Arithmetic */

#include<stdio.h>
#include<math.h>
void main()
{
	int n1,n2,r1,r2,s,sum; 
	while(scanf("%d%d",&n1,&n2)!=EOF)
	{
		if(n1==0&&n2==0)
			break;
		s=0;
		while((n1>0)||(n2>0))
		{
			r1=n1%10;
			n1=n1/10;
			r2=n2%10;
			n2=n2/10;
			sum=r1+r2;
			if(sum>9)
				s++;

		}
		if(s==0)
			printf("No carry operation.\n");
		else if(s==1)
			printf("1 carry operation.\n");
		else
			printf("%d carry operations.\n",s);
	}
}
		
		
// wrong ans
	
	

Post Reply

Return to “Volume 100 (10000-10099)”