UVA Problem 10035 – Primary Arithmetic Solution

UVA Problem 10035 – Primary Arithmetic Solution:


Click here to go to this problem in uva Online Judge.

Solving Technique:

For this problem two integers less than 10 digits are given. The task is to find how many carry operations occur when adding.

This problem may be solved using strings since input integer is less than 10 digits. It can be solved with out using array. My implementation is using character array to add and count carry operations.

Similar to this UVA 713 – Adding Reversed Numbers Solution and UVA PROBLEM 1225 – DIGIT COUNTING

 

Important:  Be sure to add or print a new line after each output unless otherwise specified. The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer. Please compile with c++ compiler as some of my codes are in c and some in c++.


More Inputs of This Problem on uDebug.


Input:

123 456
555 555
123 594
0 0

 


Output:

No carry operation.
3 carry operations.
1 carry operation.

Code:

/**
 * Author:    Asif Ahmed
 * Site:      https://quickgrid.wordpress.com
 * Problem:   UVA 10035 - Primary Arithmetic
 * Technique: Adding leading 0's integer string,
 *            Making two strings of same length,
 *            Adding String Integers.
 */

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

#define N 128

static char s[N];
static char t[N];

static char output[N];


int main(){

    while( scanf("%s", s) && scanf("%s", t) ){

        // Although a major flaw input beginning with 0 and
        // two strings matching. But for this problem it doesn't matter.
        if( strcmp(s,t) == 0 && s[0] == '0' )
            break;


        int lens = strlen(s);
        int lent = strlen(t);


        // Add leading 0's to smaller string (May not be necessary).
        // Shift each characters in string right by padding length.
        if( lens > lent ){

            int padding = lens - lent;

            for(int i = lent - 1; i >= 0; --i)
                t[i + padding] = t[i];

            for(int i = 0; i < padding; ++i)
                t[i] = '0';
        }

        else if( lens < lent ){

            int padding = lent - lens;

            for(int i = lens - 1; i >= 0; --i)
                s[i + padding] = s[i];

            for(int i = 0; i < padding; ++i)
                s[i] = '0';
        }



        int maxlen;
        if(lens > lent)
            maxlen = lens;
        else maxlen = lent;


        int carry = 0;
        int c = 0;
        int sum = 0;


        // Add two Strings, if a carry operation occurs
        // then add that to the count.
        for(int i = maxlen - 1; i >= 0; --i){

            sum += s[i] - '0' + t[i] - '0';

            sum = sum + carry;

            carry = sum / 10;

            if(carry)
                ++c;

            sum = 0;

        }


        if(!c)
            printf("No carry operation.\n");
        else if( c > 1 )
            printf("%d carry operations.\n", c);
        else
            printf("%d carry operation.\n", c);

    }

    return 0;
}
Advertisements

3 thoughts on “UVA Problem 10035 – Primary Arithmetic Solution

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s