SDNU 1372.Problem C:Primary Arithmetic(高精度)

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

思路:忘了初始化,让我wa了好多次。
#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, sizeof(ads));
    int len, len1, len2;
    int ad[100];
    len1 = strlen(a);
    len2 = strlen(b);
    if(len1 == len2)
        len = len1;
    else if(len1>len2)///短的数字位不足。将数字往后移,前面补“0”,便于计算
    {
        len = len1;
        for(int i = len; i >= len-len2; i--)
            b[i] = b[i-len+len2];
        for(int i = len-len2-1; i >= 0; i--)
            b[i] = '0';
    }
    else if(len1<len2)///短的数字位不足。将数字往后移,前面补“0”,便于计算
    {
        len = len2;
        for(int i = len; i >= len-len1; i--)
            a[i] = a[i-len+len1];
        for(int i = len-len1-1; i >= 0; i--)
            a[i] = '0';
    }
    int t = 0;
    for(int i = len-1; i >= 0; i--)///进行计算
    {
        ad[i] = (a[i]-'0')+(b[i]-'0')+t;///把原本该有的数加上,再加上前一位需要进的“1”
        t= 0;
        if(ad[i] >= 10)
        {
            t++;
            sum++;
//            cout<<sum<<"---"<<endl;
            ad[i] = ad[i]-10;
            ads[i] = ad[i]+'0';
        }
        else ads[i] = ad[i]+'0';
    }
    if(t == 1)///如果位数已经变大,就将所有数往后移,再在最前面加一
    {
        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;
}

 

转载于:https://www.cnblogs.com/RootVount/p/11262287.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值