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

tarsun
New poster
Posts: 7
Joined: Thu Sep 20, 2012 7:58 pm
Contact:

Re: 10035 - Primary Arithmetic

Post by tarsun »

output:
4 carry operations.

is it r8?

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10035 - Primary Arithmetic

Post by brianfry713 »

Input:

Code: Select all

9999 9999
9 8889
0 0
Correct output:

Code: Select all

4 carry operations.
1 carry operation.
Check input and AC output for thousands of problems on uDebug!

shipu_a
New poster
Posts: 23
Joined: Tue Oct 23, 2012 8:04 pm
Location: Dhaka,Bangladesh
Contact:

Re: 10035 - Primary Arithmetic

Post by shipu_a »

where is my problem ?help me! :roll: :cry:

Code: Select all

#include<stdio.h>
#include<string.h>
int main()
{
    char a[20],b[20],e[20],d[20];
    int p,c,i,l,s,len,y,k,n,j;
    while(scanf("%s%s",a,b)==2)
    {
        k=(a[0]-'0');
        l=(b[0]-'0');
        if(k==0&&l==0)
        break;
        l=strlen(a);
        len=strlen(b);
        if(l>len)
        {
        y=l;
        n=l-len;
        for(i=0;i<n;i++)
        d[i]='0';
        for(i=i,j=0;j<len;i++,j++)
        d[i]=b[j];
        d[l]='\0';
        strcpy(e,a);
        }

        else if(l<len)
        {
        y=len;
        n=len-l;
        for(i=0;i<n;i++)
        e[i]='0';
        for(i=i,j=0;j<len;i++,j++)
        e[i]=a[j];
        e[len]='\0';
        strcpy(d,b);
        }
        else if(l==len)
        {
           y=l;
           strcpy(e,a);
           strcpy(d,b);
        }

        c=s=0;
        p=0;
        for(i=y;i>=0;i--)
        {
            s=(e[i]-'0')+(d[i]-'0')+p;
            if(s>=10)
            {
                c++;
                p=1;
            }
        }
        if(c==0)
        printf("No carry operation.\n");
        else if(c==1)
        printf("1 carry operation.\n");
        else
        printf("%d carry operations.\n",c);
    }
    return 0;
}
Input:

Code: Select all

12354  25478654
Output:

Code: Select all

3 carry operation.
Last edited by shipu_a on Wed Nov 07, 2012 11:29 pm, edited 1 time in total.
Nothing is imposible in the world.....And
Never Judge a Book by Its Cover.............
BUBT_Psycho
http://uhunt.felix-halim.net/id/168573
http://shipuahamed.blogspot.com

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10035 - Primary Arithmetic

Post by brianfry713 »

You're missing the s at the end of operations.
Check input and AC output for thousands of problems on uDebug!

shipu_a
New poster
Posts: 23
Joined: Tue Oct 23, 2012 8:04 pm
Location: Dhaka,Bangladesh
Contact:

Re: 10035 - Primary Arithmetic

Post by shipu_a »

brianfry713 wrote:You're missing the s at the end of operations.
plz cheak again....i edit something but again WA .
Nothing is imposible in the world.....And
Never Judge a Book by Its Cover.............
BUBT_Psycho
http://uhunt.felix-halim.net/id/168573
http://shipuahamed.blogspot.com

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10035 - Primary Arithmetic

Post by brianfry713 »

Input:
112 878
0 0

Correct output:
1 carry operation.

You need to reset p to 0 if there isn't a carry.
Check input and AC output for thousands of problems on uDebug!

nazmus_20000
New poster
Posts: 7
Joined: Fri Dec 14, 2012 9:01 pm

10035 - Primary Arithmetic

Post by nazmus_20000 »

plz can anyone describe why it is not working....


#include<stdio.h>
#include<string.h>
int main()
{
char a[1000],b[1000];
int i,j,k,l,t;
while(scanf("%s %s",&a,&b)!=EOF)
{
if(a[0]=='0'&&b[0]=='0')
{
for(i=0,k=0;a!='\0';i++)
{
if(a=='0')k++;
}
for(j=0,t=0;b[j]!='\0';j++)
{
if(a[j]=='0')t++;
}
}
if((i==k)&&j==t)break;
else
{
if(strlen(a)==strlen(b))
{
for(i=strlen(a)-1,k=0,l=0;i>=0;i--)
{
if((a-48)+(b-48)+l>9)
{
k++;
}
t=((a-48)+(b-48)+l)/10;
l=t;
}
}
else if(strlen(a)>strlen(b))
{
for(i=strlen(b)-1,j=strlen(a)-1,k=0,l=0;i>=0;i--,j--)
{
if((a[j]-48)+(b-48)+l>9)
{
k++;
}
t=((a[j]-48)+(b-48)+l)/10;
l=t;
}
for(;j>=0;j--)
{
if((a[j]-48)+l>9)
{
k++;
}
t=((a[j]-48)+l)/10;
l=t;
}
}
else if(strlen(a)<strlen(b))
{
for(i=strlen(a)-1,j=strlen(b)-1,k=0,l=0;i>=0;i--,j--)
{
if(((a-48)+(b[j]-48)+l)>9)
{
k++;
}
t=((a-48)+(b[j]-48)+l)/10;
l=t;
}
for(;j>=0;j--)
{
if((b[j]-48)+l>9)
{
k++;
}
t=((b[j]-48)+l)/10;
l=t;
}
}
if(k==0)printf("No carry operation.\n");
else{printf("%d carry operations.\n",k);}
}
}
return 0;
}

@ce
Learning poster
Posts: 71
Joined: Mon May 28, 2012 8:46 am
Location: Ranchi, India

Re: 10035 - Primary Arithmetic

Post by @ce »

Input

Code: Select all

1 9
0 0
Your Output

Code: Select all

1 carry operations.
Correct Output

Code: Select all

1 carry operation.
When answer is 1, dont put "s" in operation.
-@ce

SayidAfshar
New poster
Posts: 1
Joined: Fri Jan 04, 2013 1:29 pm

Re: 10035 - Primary Arithmetic

Post by SayidAfshar »

this is my code what is the problem??i get WA
#include<iostream.h>//.h
//using namespace std;
int main()
{
long a,b,x,y,j;
while(cin>>a>>b)
{
j=0;
if(a==0 && b==0)break;
x=1;y=1;
while(x !=0 || y !=0)
{
x=a%10;
y=b%10;
a/=10;
b/=10;
if(x+y>9)
{
x=a%10;
y=b%10;
if(x>y)b+=1;
else if(x<y) a+=1;
j++;
}
}
if(j==0)cout<<"No carry operation."<<endl;
if(j==1)cout<<"1 carry operation."<<endl;
if(j>1)cout<<j<<" carry operations."<<endl;
}
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10035 - Primary Arithmetic

Post by brianfry713 »

Input: 909 909
AC Output: 2 carry operations.
Check input and AC output for thousands of problems on uDebug!

agwells
New poster
Posts: 3
Joined: Fri Jan 04, 2013 10:51 pm

10035 Java Version

Post by agwells »

Hi
This is the first time I try this website and I wanted to test my solution for 10035 problem, primary arithmetic, the system gives me Runtime error, I don't know why this could happen because it works fine on my computer. This is the code: Thanks for any help:

Code: Select all

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);

		while (reader.hasNextLine()) {
			int a = reader.nextInt();
			int b = reader.nextInt();

			if (a == 0 && b == 0)
				break;

			int res = solve(a, b);
			if (res == 0) {
				System.out.println("No carry operation.");
			} else {
				String s = res > 1 ? "s" : "";
				System.out.println(res + " carry operation" + s + ".");
			}
		}
		reader.close();
	}

	public static int solve(int a, int b) {
		if (a == 0 || b == 0) {
			return 0;
		}

		if (a % 10 + b % 10 >= 10) {
			return 1 + solve(a / 10, b / 10 + 1);
		}
		return solve(a / 10, b / 10);
	}
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10035 Java Version

Post by brianfry713 »

You get WA with this code, not RE.
Try input

Code: Select all

1 999
0 0
Also Main shouldn't be public.
http://uva.onlinejudge.org/index.php?op ... &Itemid=30
Check input and AC output for thousands of problems on uDebug!

agwells
New poster
Posts: 3
Joined: Fri Jan 04, 2013 10:51 pm

Re: 10035 Java Version

Post by agwells »

hi, thanks for the reply
I made the Main class non public and fixed the code, but I still get the RE message.

The code is:

Code: Select all

package com.ag.uvaproblems.primaryArithmetic;

import java.util.Scanner;
public class Main {

	static void main(String[] args) {
		Scanner reader = new Scanner(System.in);

		while (reader.hasNextLine()) {
			int a = reader.nextInt();
			int b = reader.nextInt();

			if (a == 0 && b == 0)
				break;

			int res = solve(a, b);
			if (res == 0) {
				System.out.println("No carry operation.");
			} else {
				String s = res > 1 ? "s" : "";
				System.out.println(res + " carry operation" + s + ".");
			}
		}

		reader.close();
	}

	public static int solve(int a, int b) {
		if (a == 0 || b == 0) {
			return 0;
		}
		
		int max = Math.max(a, b);
		int min = Math.min(a, b);

		if (max % 10 + min % 10 >= 10) {
			return 1 + solve(max / 10, min / 10) + solve(max / 10, 1);
		}
		return solve(max / 10, min / 10);
	}
}
is there any way to see a more detailed error message?

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10035 Java Version

Post by brianfry713 »

agwells wrote:is there any way to see a more detailed error message?
Not through the judge.

Don't use a package.
http://uva.onlinejudge.org/index.php?op ... &Itemid=30

Main is still public in the code you posted.
Main class should contain method: public static void main (String[] args).

Try input:

Code: Select all

555 445
0 0
Just write your code based on the method you would use as if you were solving this by hand, without recursion.
Check input and AC output for thousands of problems on uDebug!

agwells
New poster
Posts: 3
Joined: Fri Jan 04, 2013 10:51 pm

Re: 10035 Java Version

Post by agwells »

thanks for the corrections, I fixed the recursive version and now it works. :D

Post Reply

Return to “Volume 100 (10000-10099)”