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. Python实例---爬去酷狗音乐

    项目一:获取酷狗TOP 100 http://www.kugou.com/yy/rank/home/1-8888.html 排名 文件&&歌手 时长 效果: 附源码: import t ...

  2. windows10下安装source insight 4.0(破解版)

    1.从官网下载source insight4.0版本(不用下载,在后面已经把所有需要的文件都准备好了); 2.安装source insightt4.0; 3.使用下载好的sourceinsight4. ...

  3. 解决Win10无法安装.Net Framework 3.5,错误代码0x800F081F

    重新安装了一遍Win10,但是不知怎的无法安装.net framework 3.5,即便是下载离线安装包也没法用. 网上有人说需要使用win10的ISO文件,个人感觉太麻烦,在这里分享一个很方便的操作 ...

  4. loli的搜索测试-5

    今天早上听说不放假感觉很sad,又听说要考试感觉更sad了,早读的时候Juan_feng表示即使考也不会考虫食算,结果上午并没有考试,就自己做了几道题.不得不说做题的决定实在是太明智了,做NOIP历年 ...

  5. 包(package),继承

    1.包(package) 1)为何用包 包用于管理程序中的类,主要解决类同名问题(它的唯一性),也可以看作是现实生活中的目录. 2)作用 —可以解决包的同名问题. —可以更好地管理类,有了包的概念,使 ...

  6. 静态性能测试-hc课堂笔记

    UI自动化,需要掌握html相关知识 w3c网站. 会了性能测试就会了接口自动化. 静态扫描:降低40-50% findbugs,隐含的bug checkstyle,风格规范 域名解析: 输入网址-D ...

  7. leetcode242—Valid Anagram

    Given two strings s and t , write a function to determine if t is an anagram of s. Example 1: Input: ...

  8. java中sleep()方法的解析

    Thread.sleep(3000); 就是指让当前正在运行的占用cpu时间片的线程挂起3000ms,把cpu的时间片交给其他线程,但是并没有指定把CPU的时间片接下来到底交给哪个线程,而是让这些线程 ...

  9. Fiddler-http检测调试工具

    Fiddler是最强大最好用的Web调试工具之一,它能记录所有客户端和服务器的http和https请求,允许你监视,设置断点,甚至修改输入输出数据. 使用Fiddler无论对开发还是测试来说,都有很大 ...

  10. day39

    今日内容: 1.对于表,库,记录的基本操作 2.数据库引擎的了解 3.表的详细 4.数据类型的掌握 1.回顾昨日对于表,库,记录的基本操作 库 增: create database mydb2; 删: ...