1001 A+B Format

该文章提供了一段C++代码,用于读取两个在-10^6到10^6之间的整数并计算它们的和,然后按照每三位数字用逗号分隔的标准格式输出。输入包含一对由空格分隔的整数,程序会将输出的和逆序存储并在达到四位时插入逗号。
摘要由CSDN通过智能技术生成

Calculate a+b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).

Input Specification:

Each input file contains one test case. Each case contains a pair of integers a and b where −106≤a,b≤106. The numbers are separated by a space.

Output Specification:

For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.

Sample Input:

-1000000 9

Sample Output:

-999,991

 1001 A+B Format (20 分) 全网最细 题目详解 翻译 完整代码 PAT甲级真题解析_a+b format 测试用例2__Pooooooocky的博客-CSDN博客

//先自己读题目,读不懂的情况下再去百度翻译。本题的意思就是输入两个整数,然后将他们的和按格式输出(从后往前,每三个数字 打一个逗号)。上面一个博客也是一种方法,仅供参考。

#include<bits/stdc++.h>
using namespace std;
int main(){
    int a,b,k=0,j=0;string s,x;
    cin>>a>>b;
    s=to_string(a+b);
    for(int i=s.size()-1;i>=0;i--){
        if(s[i]>='0'&&s[i]<='9')
        {
            j++;
            if(j==4)
               { x[k++]=',';x[k++]=s[i];j=1;}
            else 
            x[k++]=s[i];
        }
        else x[k++]=s[i];
    }
    for(int i=k-1;i>=0;i--)
        cout<<x[i];
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星河欲转。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值