P1601 A+B Problem(高精)(c++实现)

我是利用栈来输出的(大佬们会用压位去做这个题目,我还得去学习一下)(已经是学会了,有兴趣的朋友可以去看看)

https://blog.csdn.net/weixin_60257971/article/details/121512751https://blog.csdn.net/weixin_60257971/article/details/121512751

我的想法就是从低位开始一个个相加,再存放在栈里面。

但是有个问题,不知道有没有大佬可以帮我解决,当我用cin输入的时候是可以ac的,但用getline的时候就会显示错误。错误格式为下图所示。

#include<iostream>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<stack>
using namespace std;

int main(){
    string str1,str2;
    //getline(cin,str1);
    //getline(cin,str2);
    cin>>str1;
    cin>>str2;
    stack<int> Q;
    int flag=0;
    int ch1,ch2;
    if(str1.length()<str2.length()){
        str1.swap(str2);
    }
    while(!str2.empty()){
        ch1=(int)str1.back()-48;
        ch2=(int)str2.back()-48;
        if((ch1+ch2+flag)>=10){
            ch1=ch1+ch2+flag-10;
            flag=1;
        }else{
            ch1=ch1+ch2+flag;
            flag=0;
        }
        Q.push(ch1);
        str1.pop_back();
        str2.pop_back();
    }
    while(!str1.empty()){
        ch1=(int)str1.back()-48;
        if((ch1+flag)>=10){
            ch1=ch1+flag-10;
            flag=1;
        }else{
            ch1=ch1+flag;
            flag=0;
        }
        Q.push(ch1);
        str1.pop_back();
    }
    if(flag==1){
        Q.push(flag);
    }
    while(!Q.empty()){
        int i;
        i=Q.top();
        Q.pop();
        cout<<i;
    }
    
    system("pause");
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值