Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2461205
  • 博文数量: 392
  • 博客积分: 7040
  • 博客等级: 少将
  • 技术积分: 4138
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-17 13:03
个人简介

范德萨发而为

文章分类

全部博文(392)

文章存档

2017年(5)

2016年(19)

2015年(34)

2014年(14)

2013年(47)

2012年(40)

2011年(51)

2010年(137)

2009年(45)

分类:

2009-12-04 15:49:33


#include <iostream>
#include <string>
using namespace std;

void printout(int count)
{
        if (0 == count)
        {
            cout << "No carry operation." << endl;
        }
        else if (1 == count)
        {
            cout << "1 carry operation." << endl;
        }
        else
        {
            cout << count << " carry operations." << endl;
        }
}

int main(int argc, char *argv[])
{
    int carray, sum, count;
    string s1, s2;
    while ((cin >> s1, cin >> s2) && (0 != s1.compare("0") || 0 != s2.compare("0")))
    {
        string::reverse_iterator ite1, ite2, end1, end2;
        ite1 = s1.rbegin();
        ite2 = s2.rbegin();
        end1 = s1.rend();
        end2 = s2.rend();
        carray = sum = count = 0;
        while (ite1 != end1 && ite2 != end2)
        {
            sum = (*ite1 - '0') + (*ite2 - '0');
            if (1 == carray)
            {
                sum += 1;
            }
            if (sum > 9)
            {
                carray = 1;
                count++;
            }
            else
            {
                carray = 0;
            }
            ite1++;
            ite2++;
        }
        if (ite1 == end1 && ite2 == end2)
        {
            printout(count);
            continue;
        }
        if (ite1 == end1)
        {
            while (ite2 != end2)
            {
                if (carray == 1 && *ite2 == '9')
                {
                    count++;
                }
                else
                {
                    break;
                }
                ite2++;
            }
        }
        else if (ite2 == end2)
        {
            while (ite1 != end1)
            {
                if (carray == 1 && *ite1 == '9')
                {
                    count++;
                }
                else
                {
                    break;
                }
                ite1++;
            }
        }
        printout(count);
    }
}


阅读(1206) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~