Page 14 of 22
Re: 10035 - primary arithmetic
Posted: Mon Jun 06, 2011 6:56 pm
by rahat khan
#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c[10],d[10],i,j,x,y,m;
while(1)
{
m=0;
scanf("%d %d",&a,&b);
if(a==0&&b==0)
break;
x=log10(a)+1;
y=log10(b)+1;
for(i=0;i<x;i++)
{
c=a%10;
a=a/10;
}
for(j=0;j<y;j++)
{
d[j]=b%10;
b=b/10;
}
if(x<=y)
{
for(i=0;i<x;i++)
{
if(c+d>9)
{
m=m+1;
}
}
}
if(x>y)
{
for(j=0;j<y;j++)
{
if(c[j]+d[j]>9)
{
m=m+1;
}
}
}
if(m==0)
printf("No carry operation.\n");
else if(m==1)
printf("%d carry operation.\n",m);
else
printf("%d carry operations.\n",m);
}
return 0;
}
Re: 10035 - Primary Arithmetic
Posted: Mon Jun 13, 2011 6:49 pm
by SS_HASAN
#include<stdio.h>
int main()
{
long long m, n;
int sum, count, carry;
while(scanf("%I64d %I64d",&m,&n))
{
if(!m && !n) break;
count = 0;
sum = 0;
carry = 0;
for( ; m != 0 || n != 0; m /= 10, n /= 10)
{
sum = (m % 10) + (n % 10) + carry;
carry = 0;
if(sum > 9)
{
carry = 1;
count++;
}
}
if(count == 0)
printf("No carry operation.\n");
else if(count == 1)
printf("%d carry operation.\n",count);
else
printf("%d carry operations.\n",count);
}
return 0;
}
//Please help me with this code..i'm getting WA..:'(
Re: 10035 - Primary Arithmetic
Posted: Sun Jul 03, 2011 2:51 am
by ravingeek
i can't find why the system refuse my code
i can't find where is the mistake
can you help me please?
here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 10
void sum(char x[SIZE],long *sum) {
int i;
for(i=0; i<SIZE; i++)
*sum+=x;
}
int main() {
char x[SIZE], y[SIZE],test[SIZE];
long z[SIZE],i,carry,sum0,sum1;
memset(test,'0', sizeof(char)*SIZE);
while(1) {
memset(x,'0', sizeof(char)*SIZE);
memset(y,'0', sizeof(char)*SIZE);
memset(z, 0, sizeof(long)*SIZE);
carry=0,sum0=0,sum1=0;
memset(z, 0, sizeof(int)*SIZE);
scanf("%s %s",x,y);
sum(x,&sum0);
sum(y,&sum1);
if(sum0==432 && sum1==432)
break;
for(i=0; i<SIZE; i++) {
z=(x+y)-96;
if (z>=SIZE)
carry++;
}
if(x[0]=='-'||y[0]=='-')
carry=0;
memset(z,0,SIZE);
if(carry==0)
printf("No carry operation.\n");
else if(carry==1)
printf("1 carry operation.\n");
else
printf("%ld carry operations.\n",carry);
carry=0;
memset(z, 0, sizeof(int)*SIZE);
}
return 0;
}
thanks in advance
Re: 10035 - Primary Arithmetic
Posted: Wed Jul 06, 2011 5:40 pm
by The_Madman
ravingeek wrote:i can't find why the system refuse my code
What do you mean by
refuse ? Can you please tell the exact
verdict?
ravingeek wrote:i can't find where is the mistake
Your code seems to compile correctly but gives incorrect output. Did you run your program on the inputs given in the previous posts?
Code: Select all
for(i=0; i<SIZE; i++) {
z[i]=(x[i]+y[i])-96;
if (z[i]>=SIZE) carry++;
}
In ASCII 96 stands for E. So what are you trying to do actually? Remember, if adding two digits is < 10, you may have a carry which makes it 10. Hope it helps.
Re: 10035 - Primary Arithmetic
Posted: Wed Jul 06, 2011 5:45 pm
by The_Madman
UVA do not give any
verdict for the code below. It only says,
Your submission with number ******* for the problem 10035 - Primary Arithmetic has failed with verdict .
Verdict what? Where is the problem?
Code accepted and removed. I feel stupid. The problem does explicitly specify less than 10 digits. While it certainly has input cases > 10 digits. Since, I've just made my arrays length 40 and it got accepted. This is unexpected. Can anyone report this to the right authority?
Re: 10035 - Primary Arithmetic
Posted: Fri Jul 08, 2011 1:36 am
by Ahmad
for anyone keep getting WA
just take care of the 'S' in case the output is greater than or equal to 1
and try this case:
Input:
Output:
Ahmad
Re: 10035 - Primary Arithmetic
Posted: Thu Aug 04, 2011 10:42 am
by rahat khan
#include<stdio.h>
int main()
{
long long a,b,c,m[100],n[100],x,y,i,e;
while(scanf("%lld %lld",&x,&y))
{
if(x==0&&y==0)
break;
c=0;
if(a<b)
{
a=y;
b=x;
}
else
{
a=x;
b=y;
}
e=0;
for(i=1;;i++)
{
m=a%10;
m=m+e;
a=a/10;
n=b%10;
b=b/10;
if((m+n)>9)
{
c++;
e++;
}
else
e=0;
if(a==0)
break;
}
if(c==0)
printf("No carry operation.\n");
else
printf("%lld carry operations.\n",c);
}
return 0;
}
UVA 10035
Posted: Mon Oct 31, 2011 10:58 am
by expecto_petronum
I am getting wrong answer. I can't found any problems. Can any one help me?
Code: Select all
#include<iostream>
using namespace std;
int main()
{
long long unsigned num1,num2,rem1,rem2,q1,q2;
int carry,p_carry;
//scanf("%u%u",&num1,&num2);
cin>>num1>>num2;
while(num1!=0 && num2!=0)
{
q1=num1;
q2=num2;
carry=0;
p_carry=0;
while(q1>0 || q2>0)
{
rem1=q1%10;
rem2=q2%10;
q1=q1/10;
q2=q2/10;
if(p_carry==1)
{
p_carry=0;
if(rem1+rem2>8)
{
carry++;
p_carry=1;
}
}
else
{
if(rem1+rem2>9)
{
carry++;
p_carry=1;
}
}
}
if(carry==0)
cout<<"No carry operation.\n";
if(carry>1)
cout<<carry<< " carry operations.\n";
//printf("%d carry operations.\n",carry);
if(carry==1)
cout<<"1 carry operation.\n";
//scanf("%d%d",&num1,&num2);
cin>>num1>>num2;
}
return 0;
}
why WA........to 10035????????/////
Posted: Thu Dec 08, 2011 12:16 pm
by artless
#include<stdio.h>
int main()
{
int a,b,count,carry,sum;
while(scanf("%d %d",&a,&b)==2&&(a||b!=0))
{count=0;carry=0;sum=0;
while(a>0||b>0)
{ sum=(a%10)+(b%10)+carry;
if(sum>=10) count+=1;
carry=((a%10)+(b%10)+carry)/10;
a=a/10;b=b/10;
}
if(count==0) printf("No carry operation.\n");
else if(count>0) printf("%d carry operation.\n",count);
}
return 0;
}

Re: why WA........to 10035????????/////
Posted: Tue Jan 10, 2012 1:29 am
by brianfry713
Do a diff against the sample output.
if(count==0) printf("No carry operation.\n");
else if(count==1) printf("1 carry operation.\n");
else printf("%d carry operations.\n",count);
Re: UVA 10035
Posted: Tue Jan 17, 2012 12:27 am
by ahsanalishahid
I have tried every possible test case i think of but judge is giving me wrong answer. Here is the code. Help me if I am wrong.
Code: Select all
#include <iostream>
#include <vector>
#include <string>
#include <cstdio>
#include <sstream>
#include <cstdlib>
using namespace std;
int main()
{
string number1 = "";
string number2 = "";
int sum = 0;
int n1 = 0, n2 = 0;
char c1 = 'n', c2 = 'n';
stringstream ss("");
string temp = "", answer = "";
int carry = 0;
int counter =0;
while( cin >> number1 >> number2 )
{
//ss.clear();
if(number1 == "0" && number2 == "0")
{
break;
}
//if (number1 != "0" || number2 != "0") break;
if(number1.length() > number2.length() )
{
while( number2.length() != number1.length() )
{
number2 = "0" + number2;
}
}
else if(number2.length() > number1.length() )
{
while( number1.length() != number2.length() )
{
number1 = "0" + number1;
}
}
while(answer.length() != number1.length() ) answer += "0";
//if(number1.length() < 10)
for(int j=answer.length()-1;j>=0;j--)
{
//cout << '2' << endl;
c1 = number1[j];
n1 = atoi(&c1);
//cout << number2 <<endl;
c2 = number2[j];
//cout << "ccc" << endl;
n2 = atoi(&c2);
sum = n2 + n1 + carry;
if(sum > 9)
{
int lastnum = sum / 10;
int remainder = sum %10;
carry = lastnum;
counter++;
//cout << "carry " << carry << endl;;
ss << remainder; ss >> temp;
answer[j] = temp[0];
temp.clear();
ss.clear();
}
else
{
ss << sum;
ss >> temp;
answer[j] = temp[0];
temp.clear();
ss.clear();
carry = 0;
}
if( j == 0 && carry != 0)
{
ss << carry;
ss >> temp;
answer = temp[0] + answer;
carry =0;
temp.erase();
ss.clear();
//j++;
}
}
if(counter == 0 )
{
if(number1 != "0" || number2 != "0")
{
cout << "No carry operation." << endl;
}
}
else if(counter== 1)
{
cout << counter << " carry operation." << endl;
}
else
{
cout << counter << " carry operations." << endl;
}
counter = 0;
number1.erase();
number2.erase();
answer.erase();
}
cin.get();
cin.get();
return 0;
}
I have tried all test cases that I have found on this board and its answer is correct but I am getting WA. I am freaking out :/ kindly help me.
Regards.
Re: UVA 10035
Posted: Thu Jan 19, 2012 1:46 am
by brianfry713
ahsanalishahid, the code you posted is not correct for the sample I/O.
Re: UVA 10035
Posted: Tue Jan 24, 2012 9:38 pm
by ahsanalishahid
@brianfry, can u please point out what am I doing wrong as I am still unable to figure out.
Re: UVA 10035
Posted: Wed Jan 25, 2012 2:07 am
by brianfry713
For the first input 123 456, right after you execute "sum = n2 + n1 + carry", here's what's in your variables:
n1=3 n2=63
n1=2 n2=52
n1=14 n2=414
I wouldn't recommend calling atoi on a single char.
Re: UVA 10035
Posted: Wed Jan 25, 2012 11:18 am
by ahsanalishahid
@brianfry . THanks man! i used stringstream instead and it worked

Thanks again
