前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PAT (Advanced Level) Practice 1001 A B Format (20 分)

PAT (Advanced Level) Practice 1001 A B Format (20 分)

作者头像
glm233
发布2020-09-28 11:03:56
3330
发布2020-09-28 11:03:56
举报

1001 A+B Format (20 分)

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

甲级第一题!自然要讲的清楚~

看要求,很简单计算两数之和,不就是相加吗,噢,原来是从个位开始,每隔三位加一个逗号,到最后不满三位则不加,先考虑简单的事情,如果是负数先输出负号,然后反向存数位,遇到下标是三的倍数就输出逗号(英文),注意最后一个逗号不输出~

代码语言:javascript
复制
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#include<unordered_set>
#define rg register ll
#define inf 2147483647
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define ll long long
#define maxn 300005
#define lb(x) (x&(-x))
const double eps = 1e-6;
using namespace std;
inline ll read()
{
	char ch = getchar(); ll s = 0, w = 1;
	while (ch < 48 || ch>57) { if (ch == '-')w = -1; ch = getchar(); }
	while (ch >= 48 && ch <= 57) { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
	return s * w;
}
inline void write(ll x)
{
	if (x < 0)putchar('-'), x = -x;
	if (x > 9)write(x / 10);
	putchar(x % 10 + 48);
}
ll a,b,tot=-1;
char s[500];
int main()
{
    cin>>a>>b;
    ll c=a+b;
    if(c==0)
    {
        cout<<0<<endl;
        return 0;
    }
    if(c<0)
    {
        cout<<"-";
    }
    c=abs(c);
    //cout<<c<<endl;
    while(c)
    {
        s[++tot]=c%10+48;
        //cout<<s[tot]<<endl;
        c/=10;
    }
    //cout<<s<<endl;
    for(rg i=tot;i>=0;i--)
    {
        cout<<s[i];
        if(i%3==0&&i)cout<<',';
    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/10/22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Input Specification:
  • Output Specification:
  • Sample Input:
  • Sample Output:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档