C++ 面试题之两个超过25位的数相加求和

上次有个兄弟面试碰到的,在这里记录下

void calculateAdd()

{

              string str1 = "2142135213214235231323241";

              string str2 = "809327498327413628164321434214";

              string result = "";

             cout << "str1的长度为:" << str1.length() <<endl;

             cout << "str2的长度为:" << str2.length() <<endl;

             int quotient = 0;

             int remainder = 0;

             int count = 0;

             int strNum1 = str1.length();

             int strNum2 = str2.length();

             int max = strNum1 > strNum2 ? strNum1: strNum2;

             cout << "max的长度为:" << max <<endl;

             while (max > 0) {

                       strNum1--;

                       strNum2--;

                       max--;

                       int nStr1 = strNum1 >= 0 ? ((int)(str1.at(strNum1)) - 48):0;

                       int nStr2 = strNum2 >= 0 ? ((int)(str2.at(strNum2)) - 48):0;

                       count = nStr1 + nStr2 + quotient;

                      if (count > 9){

                               quotient = count / 10;

                               remainder = count % 10;

                     }else{

                              quotient = 0;

                              remainder = count;

                     }

                    result = (char)(remainder+48) + result;

            }

           if(quotient > 0){

                     result = (char)(quotient+48) + result;

           }

           cout << "result的长度为:" << result.length() <<endl;

          cout<<"----------和为:"<<result.c_str()<<endl;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值