1001 A+B Format

  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 −. 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

题目解析

本题给出两个正整数,要求将其和由最后一位的数字开始每隔三位加一个,但是首尾与负数的符号后第一位都不可以有,

可以使用头文件string中的to_string()函数将两数加和转化为字符串。

将其反转,反转后首位便是和的最后一位,遍历反转后字符串,将其加入ans,每隔3位向ans中添加一位 , 对末位与符号前位进行特判即可。

 #include <bits/stdc++.h>
using namespace std;
int main()
{
int a, b;
scanf("%d%d", &a, &b); //输入a b
string str = to_string(a + b);
//将a与b的和转换为字符串储存在str中
int cnt = ;
string ans = "";
//ans记录最终答案
reverse(str.begin(),str.end());
//由于要从最后一个数字开始每隔三个数字添加一个 ,
//将str反转
for(auto i : str){
ans += i;
//每隔3个数字添加一个 , 最后一个位置与-前的位置不能添加 ,
if(cnt % == && cnt != str.size() && !(cnt == str.size() - && str[str.size() - ] == '-'))
ans += ',';
cnt++;
}
reverse(ans.begin(),ans.end());
cout << ans << endl;
return ;
}

PTA (Advanced Level) 1001 A+B Format的更多相关文章

  1. PAT (Advanced Level) 1001. A+B Format (20)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

  2. PTA(Advanced Level)1036.Boys vs Girls

    This time you are asked to tell the difference between the lowest grade of all the male students and ...

  3. PTA (Advanced Level) 1004 Counting Leaves

    Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...

  4. PTA(Advanced Level)1025.PAT Ranking

    To evaluate the performance of our first year CS majored students, we consider their grades of three ...

  5. PTA (Advanced Level) 1009 Product of Polynomials

    1009 Product of Polynomials This time, you are supposed to find A×B where A and B are two polynomial ...

  6. PTA (Advanced Level) 1006 Sign In and Sign Out

    Sign In and Sign Out At the beginning of every day, the first person who signs in the computer room ...

  7. PTA (Advanced Level) 1002 A+B for Polynomials

    1002 A+B for Polynomials This time, you are supposed to find A+B where A and B are two polynomials. ...

  8. PTA (Advanced Level) 1027 Colors in Mars

    Colors in Mars People in Mars represent the colors in their computers in a similar way as the Earth ...

  9. PTA (Advanced Level) 1020 Tree Traversals

    Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...

随机推荐

  1. 【第一次玩Travis CI】终于弄好了我的马鸭

    真是不容易,我都要哭了.熬了半天终于弄完了!! 终于可以坐这儿挺会小曲,写写感受了. 作为一个程序写的不咋滴的程序员,倒是特别喜欢写博客,也是绝了. 高三的时候,用OneNote,后来转到Lofter ...

  2. win10 损坏 bios?

    自从前几个月升级为win10后,主板莫名奇妙的就出问题了,遇到3块不同型号2块技嘉b75,1块微信ph67 技嘉b75-ds3v 技嘉b75m-d3v 微星ph67s-c43 ms-7673 1.0 ...

  3. October 21st 2017 Week 42nd Saturday

    Only I can change my life. No one can do it for me. 只有我可以改变我的命运,没有人可以帮我做. Stop complaining about the ...

  4. 51nod 1625 夹克爷发红包

    题目链接戳这里 题意是有一个赋有非负数的矩阵,每次可以将某一行or某一列替换成某个数值,可以替换<=k次,问如何替换能使得矩阵总和最大,输出最大值. 一开始想的是简单的贪心:比如找当前收益最大的 ...

  5. Pre标签 自动换行

    <pre> 元素可定义预格式化的文本.被包围在 pre 元素中的文本通常会保留空格和换行符.而文本也会呈现为等宽字体. <pre> 标签的一个常见应用就是用来表示计算机的源代码 ...

  6. 8、Django的模型层(2)

    第3节:多表操作 3.1 创建模型 实例:我们来假定下面这些概念,字段和关系 作者模型:一个作者有姓名和年龄. 作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作 ...

  7. Spark项目之电商用户行为分析大数据平台之(十一)JSON及FASTJSON

    一.概述 JSON的全称是”JavaScript Object Notation”,意思是JavaScript对象表示法,它是一种基于文本,独立于语言的轻量级数据交换格式.XML也是一种数据交换格式, ...

  8. python生成语谱图

    语音的时域分析和频域分析是语音分析的两种重要方法,但是都存在着局限性.时域分析对语音信号的频率特性没有直观的了解,频域特性中又没有语音信号随时间的变化关系.而语谱图综合了时域和频域的优点,明显的显示出 ...

  9. Python2.7-glob

    glob 模块,寻找所有匹配指定的模式的路径名,利用的是 Unix shell 的规则,可以在 Windows 环境下使用.模块是通过 os.listdir() 和 fnmatch.fnmatch() ...

  10. Oracle 视图view

    在我看来,oracle的视图就是用于将多个表的关联查询结果映射成[临时表],视图与系统表中的数据是实时对应的. 我们可以像操作表的查询一样来操作视图查询. 视图写法: CREATE OR REPLAC ...