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

Spykaj
New poster
Posts: 47
Joined: Sun May 21, 2006 12:13 pm
You must use long long int instead of long.

Change:

Code: Select all

while(a!=0||b!=0)
{
scanf("%ld %ld", &a, &b);
to:

Code: Select all

while(scanf("%lld %lld",a,b)&&(a!=0||b!=0))
Last edited by Spykaj on Mon Apr 16, 2007 8:16 pm, edited 1 time in total.

ranacse05
New poster
Posts: 37
Joined: Wed Mar 28, 2007 5:08 pm
Location: Rajshahi
Contact:
LZ try long long int.your vc++ compilar will not support it but judge's pc'll suport it bcoz they use GCC/G++ compilar
I like to solve problems.

legnaleurc
New poster
Posts: 4
Joined: Mon Apr 23, 2007 6:05 am
I have Accepted with C, but when I try with C++, I got Wrong Answer.

Code: Select all

#include <iostream>
#include <string>
using namespace std;

int main()
{
string a,b;
int i,count,len;

while(cin >> a >> b)
{
if(a == "0" && b == "0")
break;
count = 0;
if(a.length() > b.length())
len = b.length();
else
len = a.length();
for(i = 0; i < len; i++)
{
if((a[a.length()-1-i]-'0' + b[b.length()-1-i]-'0') >=10)
{
if(a.length() > b.length())
{
if((a.length()-2-i)>=0)
{
a[a.length()-2-i]++;
}
}
else
{
if((b.length()-2-i)>=0)
{
b[b.length()-2-i]++;
}
}
count++;
}
}
if(a.length() > b.length())
{
while((a[a.length()-1-i]-'0')>=10)
{
if(a.length()-2-i>=0)
{
a[a.length()-2-i]++;
}
count++;
i++;
}
}
else
{
while((b[b.length()-1-i]-'0')>=10)
{
if(b.length()-2-i>=0)
{
b[b.length()-2-i]++;
}
count++;
i++;
}
}
if(count == 1)
cout << "1 carry operation." << endl;
else if(count > 1)
cout << count << " carry operations." << endl;
else
cout << "No carry operation." << endl;
}
return 0;
}

I have tried this inputs:

Code: Select all

123 456
555 555
123 594
123         777

4235 0

999
989
0 4336
5210357 1245874
9090 10
119 789
1 999
999 1
0            0
but all clear:

Code: Select all

No carry operation.
3 carry operations.
1 carry operation.
2 carry operations.
No carry operation.
3 carry operations.
No carry operation.
3 carry operations.
1 carry operation.
2 carry operations.
3 carry operations.
3 carry operations.
Any idea?

ranacse05
New poster
Posts: 37
Joined: Wed Mar 28, 2007 5:08 pm
Location: Rajshahi
Contact:

### answer

why use string?the range fit in to long long int
I like to solve problems.

legnaleurc
New poster
Posts: 4
Joined: Mon Apr 23, 2007 6:05 am
Well, I used same method(char*) in C and Accepted.
I just wonder about why WA in C++.....

ranacse05
New poster
Posts: 37
Joined: Wed Mar 28, 2007 5:08 pm
Location: Rajshahi
Contact:

### Replay

Try to use long long
I like to solve problems.

balkrishna
New poster
Posts: 3
Joined: Tue Jun 05, 2007 5:57 pm
Contact:

### wa with this code .. please help

getting wrong answer with this code

Code: Select all

#include<stdio.h>
#include<string.h>
#define MAXDIGITS 20
int check(char a[],char b[])
{
if(a[0]=='0' && b[0]=='0')
return 0;
else
return 1;
}

char *adjuststr(char a[],char b[],int m,int n)
{
int i;
int dif;
if(m>n){
dif=m-n;
for(i=m;i>dif;i--)
{
b[i-1]=b[i-dif-1];
}
b[m]='\0';
for(i=0;i<dif;i++)
{
b[i]='0';
}
}
else
{

dif=n-m;

for(i=n;i>dif;i--)
{
a[i-1]=a[i-dif-1];
}
a[n]='\0';
for(i=0;i<dif;i++)
{
a[i]='0';
}
}
return a,b;
}
int main()
{
int carry=0;
int counter=0;
int i;
char num1[MAXDIGITS],num2[MAXDIGITS];
scanf("%s %s",num1,num2);
while(check(num1,num2)!=0){
int len1=strlen(num1),len2=strlen(num2);
if(len1==len2)
{counter=0;
i=len1-1;
while(num1[i]!='\0' && num2[i]!='\0' && i>=0){
if(((num1[i]-'0')+(num2[i]-'0')+carry)>9)
counter++;

carry=((num1[i]-'0')+(num2[i]-'0')+carry)/10;
i--;
}
if(counter==0)
printf("No carry operations\n");
else
printf("%d carry operations\n",counter);
}
else {
adjuststr(num1,num2,len1,len2);
len1=strlen(num1);
i=len1-1;
counter=0;
while(num1[i]!='\0' && num2[i]!='\0' && i>=0){
if(((num1[i]-'0')+(num2[i]-'0')+carry)>9)
counter++;
carry=((num1[i]-'0')+(num2[i]-'0')+carry)/10;
i--;
}
if(counter==0)
printf("No carry operations\n");
else
printf("%d carry operations\n",counter);

}
i=len1-1;
scanf("%s %s",num1,num2);
}
return 0;
}

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

Input:

Code: Select all

1 999
0 999
999 0
0 0
Output:

Code: Select all

3 carry operations.
No carry operation.
No carry operation.
You are missing full stops. And be carefull about
1 carry operation.
No carry operation.
3 carry operations.
Ami ekhono shopno dekhi...
HomePage

balkrishna
New poster
Posts: 3
Joined: Tue Jun 05, 2007 5:57 pm
Contact:

### still wa

thanks for your suggestion of inputs.
they still give the appropriate outputs but judges verdict is still wa....
this code too is not accepted.
#include<stdio.h>
#include<string.h>
#define MAXDIGITS 20
int check(char a[],char b[])
{
if(strcmp(a,"0")==0 && strcmp(b,"0")==0)
return 0;
else
return 1;
}

char *adjuststr(char a[],char b[],int m,int n)
{
int i;
int dif;
if(m>n){
dif=m-n;
for(i=m;i>dif;i--)
{
b[i-1]=b[i-dif-1];
}
b[m]='\0';
for(i=0;i<dif;i++)
{
b='0';
}
}
else
{

dif=n-m;

for(i=n;i>dif;i--)
{
a[i-1]=a[i-dif-1];
}
a[n]='\0';
for(i=0;i<dif;i++)
{
a='0';
}
}
return a,b;
}
int main()
{
int carry=0;
int counter=0;
int i;
char num1[MAXDIGITS],num2[MAXDIGITS];
scanf("%s %s",num1,num2);
while(check(num1,num2)!=0){
int len1=strlen(num1),len2=strlen(num2);
if(len1==len2)
{
counter=0;
i=len1-1;
while(num1!='\0' && num2!='\0' && i>=0){
if(((num1-'0')+(num2-'0')+carry)>9)
counter++;

carry=((num1-'0')+(num2-'0')+carry)/10;
i--;
}
if(counter==0)
{
printf("No carry operation.\n");
}
else if(counter==1)
{
printf("1 carry operation.\n");
}
else
{
printf("%d carry operations.\n",counter);
}
}
else
{
adjuststr(num1,num2,len1,len2);
len1=strlen(num1);
i=len1-1;
counter=0;
while(num1!='\0' && num2!='\0' && i>=0){
if(((num1[i]-'0')+(num2[i]-'0')+carry)>9)
counter++;
carry=((num1[i]-'0')+(num2[i]-'0')+carry)/10;
i--;
}
if(counter==0)
{
printf("No carry operation.\n");
}
else if(counter==1)
{
printf("%d carry operation.\n",counter);
}
else
{
printf("%d carry operations.\n",counter);
}
}
i=len1-1;
scanf("%s %s",num1,num2);
}
return 0;
}

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:
In my compiler your code fails for the above set.
Ami ekhono shopno dekhi...
HomePage

hjuarez
New poster
Posts: 1
Joined: Thu Aug 02, 2007 8:14 pm

### WA in my code, :S

I dont know what is bad in my code, can anybody tell me what is wrong? the judge tell me WA.

Code: Select all

/* @BEGIN_OF_SOURCE_CODE */

#include <iostream>
#include <stdio.h>
#include <math.h>

using namespace std;

int getdigit(int index, int number)
{
int digit;

if( number == 0 && index == 0 )
return 0;

digit = number / (int)pow((double)10, (double)index);

if (digit == 0) return 0;

return digit % 10;
}

int main(int argc, char *argv[])
{
unsigned int x, y;
int len, len2, digit1, digit2;
char num[10];
char num2[10];

while( cin>>x>>y && !(x==0 && y==0) )
{
sprintf(num, "%d", x);
sprintf(num2, "%d", y);
len = strlen(num);
len2 = strlen(num2);

int max = len2 > len ? len2 : len;
int carry = 0;
int sum = 0;
int inc = 0;

for(int i=0; i<max; i++)
{
digit1 = len == max ? getdigit(i, x) : 0;
digit2 = len2 == max ? getdigit(i, y) : 0;

sum = digit1 + digit2 + inc;
inc = 0;
inc += (sum > 9);
carry += inc;
}

switch(carry)
{
case 0: cout<<"No carry operation.\n"; break;
case 1: cout<<"1 carry operation.\n"; break;
default: cout<<carry<<" carry operations.\n"; break;
}
}

return 0;
}

/* @END_OF_SOURCE_CODE */

Sayeef
New poster
Posts: 12
Joined: Sun Jun 18, 2006 3:06 am

### I get WA.. For the following Code 10035

I get WA for this program...

Code: Select all

#include<iostream>
#include<string>
using namespace std;

int main()
{

int count=0,chk,j,i;
while(1)
{
int res=0;
count=0;chk=0;
char s1[20]={0},s2[20]={0};
cin>>s1;
cin>>s2;
if(!(strcmp(s1,"0"))&&!(strcmp(s2,"0")))
break;
for(i=0,j=0;(s2[i]||s1[j]);i++,j++)
{
res=0;
if((s2[i]>=48&&s2[i]<58)&&(s1[j]>=48&&s1[j]<58))
res=s2[i]+s1[j]+chk-96;
else if(s2[i]>=48&&s2[i]<58)
res=(s2[i]-48)+chk;
else if(s1[j]>=48&&s1[j]<58)
res=(s1[j]-48)+chk;

chk=(int)(res/10);
if(chk)
count++;
}

if(count==1)
cout<<"1 carry operation."<<endl;
else if(count>1)
cout<<count<<' '<<"carry operations."<<endl;
else
cout<<"No carry operation."<<endl;

}

return 0;
}

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

Input:

Code: Select all

119 789
6322168 361892
254312334 8387
277931 56502860
586509 66138934
10 61849637
92 9359840
0 0
Output:

Code: Select all

2 carry operations.
3 carry operations.
3 carry operations.
2 carry operations.
4 carry operations.
No carry operation.
1 carry operation.
Hope these help.
Ami ekhono shopno dekhi...
HomePage

apurba
New poster
Posts: 42
Joined: Sun Oct 07, 2007 10:29 pm

### !!!!!!!!!!!!!

Code: Select all

remove ur code after acc............

Code: Select all

keep dreaming...

scott1991
New poster
Posts: 28
Joined: Fri Dec 07, 2007 11:41 pm
why does my code output the first answer wrongly and then the rest correct as far as i can see. Cheers for any help.

Code: Select all

#include <stdio.h>
#include <math.h>

int main()
{
long long int a, b, carry=0,temp;
int length;
while(scanf("%lld%lld",&a,&b)==2)
{
if(a==0&&b==0)
{
return 0;
}
if(a<b)
{
temp=a;a=b;b=temp;
}
length=int(log10(a*1.0)+2);
while(length>0)
{
if(a%10+b%10>=10)
{
if(length==1)
{
length++;
}
carry++;
a=int(a/10);
b=int(b/10);
b+=1;
}
else
{
a=int(a/10);
b=int(b/10);
}
length--;
}
if (carry==0)
{
printf("No carry operation.\n");
}
else if(carry==1)
{
printf("%lld carry operation.\n",carry);
}
else
{
printf("%lld carry operations.\n",carry);
}
carry=0;
}
return 0;
}