https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400

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

Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.

Output

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

代码:
#include <bits/stdc++.h>
using namespace std; const int maxn = 1e5 + 10;
char s[maxn], out[maxn];
int a[maxn]; void zlr_itoa(int x) {
if(x == 0) {
s[0] = '0';
s[1] = 0;
return ;
}
int top = 0;
while(x) {
a[top ++] = x % 10;
x = x / 10;
}
int sz = 0;
while(top != 0) {
s[sz++] = (char)(a[-- top] + '0');
s[sz] = 0;
}
} int main() {
int a, b;
scanf("%d%d", &a, &b);
int sum = a + b;
if(sum == 0) {
printf("0\n");
return 0;
}
if(sum < 0) {
printf("-");
sum = sum * (-1);
} zlr_itoa(sum);
int len = strlen(s); if(len < 3) {
printf("%s\n", s);
return 0;
} if(len % 3 == 0) {
for(int i = 0; i < len - 3; i += 3) {
for(int j = i; j < i + 3; j ++)
printf("%c", s[j]);
printf(",");
}
printf("%c%c%c", s[len-3], s[len - 2], s[len - 1]);
} else if(len % 3 == 1) {
printf("%c,", s[0]);
for(int i = 1; i < len - 4; i += 3) {
for(int j = i; j < i + 3; j ++)
printf("%c", s[j]);
printf(",");
}
printf("%c%c%c", s[len-3], s[len - 2], s[len - 1]);
} else if(len % 3 == 2) {
printf("%c%c,", s[0], s[1]);
for(int i = 2; i < len - 5; i += 3) {
for(int j = i; j < i + 3; j ++)
printf("%c", s[j]);
printf(",");
}
printf("%c%c%c", s[len-3], s[len - 2], s[len - 1]);
} printf("\n");
return 0;
}

  

PAT 甲级 1001 A+B Format的更多相关文章

  1. PAT甲级 1001 A+B Format

    题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400 1001 A+B Format ( ...

  2. PAT 甲级 1001 A+B Format (20)(20 分)

    1001 A+B Format (20)(20 分) Calculate a + b and output the sum in standard format -- that is, the dig ...

  3. PAT 甲级1001 A+B Format (20)(C++ -思路)

    1001 A+B Format (20)(20 分) Calculate a + b and output the sum in standard format -- that is, the dig ...

  4. PAT甲级 1001. A+B Format (20)

    题目原文: Calculate a + b and output the sum in standard format -- that is, the digits must be separated ...

  5. PAT甲级——1001 A+B Format (20分)

    Calculate a+b and output the sum in standard format – that is, the digits must be separated into gro ...

  6. PAT甲 1001. A+B Format (20) 2016-09-09 22:47 25人阅读 评论(0) 收藏

    1001. A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Calculate ...

  7. 【PAT】1001. A+B Format (20)

    1001. A+B Format (20) Calculate a + b and output the sum in standard format -- that is, the digits m ...

  8. PAT Advanced 1001 A+B Format (20 分)

    Calculate a+b and output the sum in standard format -- that is, the digits must be separated into gr ...

  9. PAT甲级1001水题飘过

    #include<iostream> using namespace std; int main(){ int a, b; while(scanf("%d%d", &a ...

随机推荐

  1. 3504: [Cqoi2014]危桥

    3504: [Cqoi2014]危桥 链接 分析: 首先往返的可以转化为全是“往”,那么只要将容量除以2即可. 然后S向a1连边容量为an(除以2之前为2*an),S向a2连边容量为an,b1,b2向 ...

  2. socket客户端和服务器端

    服务器端: #!/usr/bin/env python #-*- coding:utf-8 -*- import socket sk=socket.socket() sk.bind(('127.0.0 ...

  3. 菜鸟vimer成长记——第2.2章、visual模式

    vi是没有可视模式的.然而大多数编辑软件都都是有可视模式.所以我猜测vim是几乎迫于无奈才有了visual模式. 个人建议,能不用就不用.这样能直接在头脑里处理好文本,锻炼文本处理能力,同时能转变过来 ...

  4. 提高你的Python编码效率的“武林秘籍”

    我用Python编程有几年了, 并且我仍然经常惊讶于Python代码可以如何的简洁,如何的 DRY . 我学到了很多小贴士和技巧,大多数来自于阅读开源项目的源代码,像Django, Flask, Re ...

  5. Permission Policies

    The Permission Policy determines Security System behavior when there are no explicitly specified per ...

  6. c#简易学生信息管理系统

    在近期的学习中,我们学习了泛型及泛型集合的概念和使用,泛型是c#中的一个重要概念,为了巩固我们学习的成果,我们可以使用一个实例来进行练习 题目及要求 要求使用Windows窗体应用程序,制作出如上图的 ...

  7. 2019网易笔试题C++--丰收

    题目描述 又到了丰收的季节,恰好小易去牛牛的果园里游玩. 牛牛常说他多整个果园的每个地方都了如指掌,小易不太相信,所以他想考考牛牛. 在果园里有N堆苹果,每堆苹果的数量为ai,小易希望知道从左往右数第 ...

  8. 路由器终端常用linux命令汇总(持续更新)

    ls:显示文件名与相关属性 ls -al;ls -l;ls -a 第一列: d:表示目录,dir. -:表示文件. l:表示链接文件,linkfile. 接下来的字符三个为一组,且均为rwx这3个字母 ...

  9. 炸!分享美团面试关于selenium的面试题

    个人分类: 软件测试 编辑 在这个互联网技术快速迭代的时代,每个测试员都知道技术对于职业发展的重要性,那些技术好的测试员不仅薪资高,而且大多数集中在一线互联网企业工作,让人感觉非常高大上的同时,也想去 ...

  10. Go入门指南

    第一部分:学习 Go 语言 第1章:Go 语言的起源,发展与普及 1.1 起源与发展 1.2 语言的主要特性与发展的环境和影响因素 第2章:安装与运行环境 2.1 平台与架构 2.2 Go 环境变量 ...