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

muktadir.rhmn
New poster
Posts: 6
Joined: Sat May 03, 2014 11:44 am

Re: 10035 - Primary Arithmetic

Post by muktadir.rhmn »

please help. why WA???

Code: Select all

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

int main()
{
     char  num[2][11];
     int carry, ans, sumOfDigits, bigNumI, smallNumI, bigNumDigits, smallNumDigits, tmp;

    scanf("%s %s", num[0], num[1]);
    while(!(atoi(num[1]) == 0 && atoi(num[0]) == 0)){

        carry = 0;
        ans = 0;
        bigNumDigits = strlen(num[0]);
        smallNumDigits = strlen(num[1]);
        if ( bigNumDigits < smallNumDigits){
            tmp = bigNumDigits;
            bigNumDigits = smallNumDigits;
            smallNumDigits = tmp;
            bigNumI = 1;
            smallNumI = 0;
        }
        else{
            bigNumI = 0 ;
            smallNumI  = 1 ;
        }

        while( smallNumDigits--){
            sumOfDigits = num[bigNumI][--bigNumDigits] + num[smallNumI][smallNumDigits] + carry;
            if (sumOfDigits > 105){
                carry = 1;
                ans ++;
             }
             else
                carry = 0;
        }

        while(bigNumDigits-- && carry){
            sumOfDigits = num[bigNumI][bigNumDigits] + carry;
            if (sumOfDigits > 57){
                carry = 1;
                ans ++;
             }
             else
                carry = 0;
        }

        /* output*/
        if(ans)
            printf("%d carry operations.\n", ans);
        else
            printf("No carry operation.\n");

        scanf("%s %s", num[0], num[1]);

    }
   return 0;
}

lbv
Experienced poster
Posts: 128
Joined: Tue Nov 29, 2011 8:40 am

Re: 10035 - Primary Arithmetic

Post by lbv »

muktadir.rhmn wrote:please help. why WA???
The code you posted doesn't pass the sample cases from the problem statement. Notice that the output from your program must match the expected output exactly. For example the strings "operation" and "operations" are considered different.

muktadir.rhmn
New poster
Posts: 6
Joined: Sat May 03, 2014 11:44 am

Re: 10035 - Primary Arithmetic

Post by muktadir.rhmn »

thnx a lot . got AC

zishan
New poster
Posts: 1
Joined: Sat Jun 21, 2014 8:46 am

Re: 10035 - Primary Arithmetic

Post by zishan »

Your code is ok,there is no minor mistake,please check your code with sample output

fakeillution
New poster
Posts: 1
Joined: Sat Jul 19, 2014 5:13 pm

Re: 10035 - Primary Arithmetic

Post by fakeillution »

please help,why WA?
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
while(1)
{
long long a,b;
cin>>a>>b;
if(a==0 && b==0)break;
if(a<b)swap(a,b);
int carry=0,flag=0,temp;
while(a)
{
if(flag)
temp=(a%10)+(b%10)+1;
else temp=(a%10)+(b%10);
if(temp>=10)
{

if(a/10)
{
carry++;
flag=1;
}
else
{
if(flag)carry++;
}
}
else flag=0;

a=a/10;
b=b/10;


}
if(!carry)
cout<<"No carry operation."<<endl;
else if(carry==1)
cout<<carry<<" carry operation."<<endl;
else
cout<<carry<<" carry operations."<<endl;
}
}

lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 10035 - Primary Arithmetic

Post by lighted »

I don't understand why are you checking this,

Code: Select all

if(flag)carry++;

increasing

Code: Select all

carry++

must be done always if

Code: Select all

if temp>=10

It must be

Code: Select all

if(temp>=10)
{

  if(a/10)
  {
    carry++;
    flag=1;
  }
  else
  {
    carry++;
  }
}
Or it must be

Code: Select all

if(temp>=10)
{
  carry++;

  if(a/10)
    flag=1;
}
Don't forget to remove your code after getting accepted. 8)
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman

ehsanulbigboss
New poster
Posts: 32
Joined: Tue Jul 22, 2014 1:17 am

10035 - Primary Arithmetic

Post by ehsanulbigboss »

OK!!!
Last edited by ehsanulbigboss on Tue Jul 22, 2014 7:52 pm, edited 1 time in total.

lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 10035 - Primary Arithmetic

Post by lighted »

You must search topics about your problem first.
Insert problem number 10035 into search box and you will get many explanations why your code may get WA.
http://acm.uva.es/board/search.php?keyw ... 2e7ff678c7

After first case you forget to set n to zero. Add line n = 0;

Code: Select all

while (scanf("%ld %ld",&x,&y)==2)
{
n = 0;
if (x==0 && y==0)
break;
Don't forget to remove your code after getting accepted. 8)
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman

axelblaze
New poster
Posts: 34
Joined: Mon Jun 23, 2014 7:45 pm

Re: 10035 - Primary Arithmetic

Post by axelblaze »

I've tested some critical I/Os but still getting WA.
please help...

Code: Select all

Removed after AC
Last edited by axelblaze on Mon Aug 18, 2014 9:15 am, edited 1 time in total.

lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 10035 - Primary Arithmetic

Post by lighted »

My advice to you -> always copy/paste output format from sample or problem description. :)
Your output

Code: Select all

1 carry operations.
Sample output

Code: Select all

1 carry operation.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman

axelblaze
New poster
Posts: 34
Joined: Mon Jun 23, 2014 7:45 pm

Re: 10035 - Primary Arithmetic

Post by axelblaze »

Thanks lighted... How silly of me...! :P

battirunner
New poster
Posts: 14
Joined: Fri Aug 15, 2014 3:48 pm

Re: 10035 - Primary Arithmetic

Post by battirunner »

why WA, checked a lot,,,, pleas help.
#include<stdio.h>
#include<string.h>
int main()
{
char a[14],b[14];
int c=0,i,l,carry=0;
while(1)
{
scanf("%s%s",a,b);
if(a[0]=='0'&&b[0]=='0')
break;
i=strlen(a);
l=strlen(b);
i--;
l--;
while(i>=0&&l>=0)
{


if(((a+b[l])+carry-96)>9)
{
c++;
carry=1;
}
else
carry=0;

i--;
l--;

}

if(i>l)
{
if((a+carry)>57)
c++;
}
else if(l>i)
{
if((b[l]+carry)>57)
c++;
}
if(c==0)
printf("No ");
else
printf("%d ",c);


printf("carry operation");
if(c<2)
printf(".\n");
else
printf("s.\n");


c=0;
a[0]='\0';
b[0]='\0';
carry=0;
}
return 0;
}

lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 10035 - Primary Arithmetic

Post by lighted »

Input

Code: Select all

999 1
Output

Code: Select all

3 carry operations
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman

battirunner
New poster
Posts: 14
Joined: Fri Aug 15, 2014 3:48 pm

Re: 10035 - Primary Arithmetic

Post by battirunner »

what a childish calculation mistake..........
thanks a lot

palashcse2k8
New poster
Posts: 1
Joined: Fri Sep 12, 2014 6:20 pm

Re: 10035 - Primary Arithmetic

Post by palashcse2k8 »

why WA ???
please help...

#include <iostream>

using namespace std;

int main()
{
long long int a,b;
while (cin>>a>>b && a!=0 && b!=0)
{
int r=0,c=0;
while (a||b)
{
r = (a%10 + b%10 + r)/10;
a=a/10;
b=b/10;
c+=r;
}
if(c==0)
cout<<"No carry operation."<<endl;
else if (c==1)
cout<<c<<" carry operation."<<endl;
else
cout<<c<<" carry operations."<<endl;
}
return 0;
}

Post Reply

Return to “Volume 100 (10000-10099)”