UVA 10035: Primary Arithmetic

Jasonlin posted @ 2011年3月27日 13:30 in UVA , 2740 阅读

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=12&problem=976&mosmsg=Submission+received+with+ID+8683305

题目大意:

      小学生问题,两个数加起来求进位的次数,如999+11进位3次。

解题思路:

      从低位开始加,大于等于10就进位。不小心错了一次,要加到最长那个数最高位为止,每次都有一些地方让我不能1Y。

要是题目每个数很大,那就必须要用高精度算法处理啦。

解题代码:

#include<iostream>
using namespace std;
typedef long long ll;
int main(){
    ll a,b;
    while(cin>>a>>b&&a+b){
        int c=0,count=0;
        while(a||b){
            c=(a%10+b%10+c)>=10;
            if(c)
                count++;
            a/=10;
            b/=10;
        }
        if(count==0)
            cout<<"No carry operation."<<endl;
        else
            if(count==1)
                cout<<"1 carry operation."<<endl;
            else
                cout<<count<<" carry operations."<<endl;
    }
}

扩展知识:

http://wenku.baidu.com/view/a03d3ccfa1c7aa00b52acb46.html

http://www.cnblogs.com/ghost-draw-sign/articles/1533798.html

 

  • 无匹配
  • 无匹配

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter