SDNU 1372.Problem C: Primary Arithmetic (precision)

Description

 

 

Children are taught to add multi-digit numbers from right-to-left one digit at a time. Many find the "carry" operation - in which a 1 is carried from one digit position to be added to the next - to be a significant challenge. Your job is to count the number of carry operations for each of a set of addition problems so that educators may assess their difficulty.

 

Input

Each line of input contains two unsigned integers less than 10 digits. The last line of input contains 0 0. 

Output

For each line of input except the last you should compute and print the number of carry operations that would result from adding the two numbers, in the format shown below.

Sample Input

123 456
555 555
123 594
0 0

Sample Output

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

Source

Idea: forget initialization, so I wa many times.
#include <cstdio>
#include <iostream>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
using namespace std;
#define ll long long

const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;

char s1[18], s2[18], ads[1000+8];
int n-, m, SUM; 

void additive ( char * A, char * B) 
{ 
//     Memset (ADS, 0, the sizeof (ADS)); 
    int len, LEN1, LEN2;
     int AD [ 100 ]; 
    LEN1 = strlen ( a); 
    LEN2 = strlen (B);
     IF (LEN1 == LEN2) 
        len = LEN1;
     the else  IF (LEN1> LEN2) /// short insufficient digits. Digital moved back, front fill "0", to facilitate the calculation 
    { 
        len = LEN1;
         for ( int= len I; I> = len-LEN2; i-- ) 
            B [I] = B [I-len + LEN2];
         for ( int I = len-len2- . 1 ; I> = 0 ; i-- ) 
            B [ I] = ' 0 ' ; 
    } 
    the else  IF (LEN1 <LEN2) /// short insufficient digits. Digital moved back, front fill "0", to facilitate the calculation 
    { 
        len = LEN2;
         for ( int I = len; I> = len-LEN1; i-- ) 
            A [I] = A [I + len- LEN1];
         for ( intlen1- = len-I . 1 ; I> = 0 ; i-- ) 
            A [I] = ' 0 ' ; 
    } 
    int T = 0 ;
     for ( int I = len . 1 ; I> = 0 ; i--) /// calculated 
    { 
        AD [I] = (A [I] - ' 0 ' ) + (B [I] - ' 0 ' ) + T; /// to add some of its original number, plus former requires advanced ". 1" 
        T = 0 ;
         IF (AD [I]> = 10 ) 
        { 
            T ++ ; 
            SUM ++ ;
 //             COUT << << SUM "---" << endl; 
            AD [I] = AD [I] - 10 ; 
            ADS [I] = AD [I] + ' 0 ' ; 
        } 
        the else ADS [I] = AD [I] + ' 0 ' ; 
    } 
    IF (T == . 1 ) /// If the number has been increased, all numbers will be moved back, and then added in a front 
    {
         for ( int I = len; I> = 0 ; i-- ) 
            ADS [I] = ADS [I- . 1 ]; 
        ADS [ 0] = '1';
    }
}

int main()
{
    while(~scanf("%d%d", &n, &m) && (n+m))
    {
        int buffer1 = n, buffer2 = m, id1 = 0, id2 = 0;
        memset(s1, 0, sizeof(s1));
        memset(s2, 0, sizeof(s1));
        while(buffer1)
        {
            id1++;
            buffer1 /= 10;
        }
        while(buffer2)
        {
            id2++;
            buffer2 /= 10;
        }
        for(int i = id1-1; i >= 0; i--)
        {
            s1[i] = n%10+'0';
            n /= 10;
        }
        for(int i = id2-1; i >= 0; i--)
        {
            s2[i] = m%10+'0';
            m /= 10;
        }
//        cout<<s1<<endl<<s2<<endl;
        sum = 0;
        additive(s1, s2);
        if(sum == 0)printf("No carry operation.\n");
        else if(sum == 1)printf("%d carry operation.\n", sum);
        else printf("%d carry operations.\n", sum);
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/RootVount/p/11262287.html