AcWing
  • 首页
  • 课程
  • 题库
  • 更多
    • 竞赛
    • 题解
    • 分享
    • 问答
    • 应用
    • 校园
  • 关闭
    历史记录
    清除记录
    猜你想搜
    AcWing热点
  • App
  • 登录/注册

【PAT刷题记录】1001 A+B Format (20分)

作者: 作者的头像   Alier ,  2020-01-26 13:55:56 ,  所有人可见 ,  阅读 823


0


思路有点非主流,主流思路是转化成字符串再处理,但我觉得当数字处理更加方便,不过要注意coner case

#include<iostream>
#include<algorithm>
#include<vector>
#include<iomanip>
using namespace std;

vector<int> ans;

int main(){
    int a,b;
    cin>>a>>b;
    int c=a+b;
    if(c<0) {
        cout<<"-";
        c=-c;
    }
    if(!c) ans.push_back(0);
    while(c){
        ans.push_back(c%1000);
        c/=1000;
    }

    int m=ans.size();
    for(int i=m-1;i>=0;i--){
        if(i!=m-1){
            cout<<setfill('0')<<setw(3)<<ans[i];
        }else{cout<<ans[i];}

        if(i!=0)cout<<",";

    }
    return 0;
}

1 评论


用户头像
TaoZex   2020-01-26 17:59         踩      回复

if(!c) ans.push_back(0);
while(c){
ans.push_back(c%1000);
c/=1000;
}
$个人建议可以用do-while实现更方便$


App 内打开
你确定删除吗?
1024
x

© 2018-2025 AcWing 版权所有  |  京ICP备2021015969号-2
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息