前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【PAT甲级】 A+B Format

【PAT甲级】 A+B Format

作者头像
喜欢ctrl的cxk
发布2019-11-08 11:38:06
3010
发布2019-11-08 11:38:06
举报
文章被收录于专栏:Don的成长史

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/weixin_42449444/article/details/88081893

Problem Description:

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 −10​6​​≤a,b≤10​6​​. 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:

代码语言:javascript
复制
-1000000 9

Sample Output:

代码语言:javascript
复制
-999,991

解题思路:

人生苦短,我用(xiang)Py。我用C++写的一个只有14分。。。。

AC代码:

代码语言:javascript
复制
a,b = map(int,input().split())
print(format(a+b,','))

C++14分代码:

代码语言:javascript
复制
#include <bits/stdc++.h>
using namespace std;

int main()
{
	int a,b;
	cin >> a >> b;
	int sum = a + b;
	string str = to_string(sum);
    stack<char> s;   //利用栈“先进后出”这个性质
    //将字符串中的字符全部推入栈中
    for(auto it : str)
    {
        s.push(it);
    }
    string result = "";
    int cnt = 0;
    while(!s.empty())   //清仓大甩卖
    {
        cnt++;
        result = s.top() + result;
        s.pop();
        if(cnt == 3)   //每三位数加一个逗号
        {
            result = "," + result;
        }
    }
    cout << result << endl;
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/03/02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Problem Description:
  • Input Specification:
  • Output Specification:
  • Sample Input:
  • Sample Output:
  • 解题思路:
  • AC代码:
  • C++14分代码:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档