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 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
这里由于要在数字间加逗号,所以这里把数字和转化为字符串的形式(分离符号方便直接对数字操作,避免符号占1位导致后面位数判断出错)
注意:1、当数字>=0不需要+(=0:测试点4)
#include<iostream>
#include<string>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
string ch = a + b >= 0 ? "" : "-"; //分离符号
string sum = to_string(abs(a + b));
int k = 1;
for (int i = sum.length() - 1; i >= 0; k++, i--) { //每3位加",",除首位
if (k % 3 == 0 && i)
sum.insert(i, ",");
}
cout << ch + sum; //输出数字
return 0;
}
PAT 甲级1001 A+B Format (20)(C++ -思路)的更多相关文章
- 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 ...
- PAT甲级 1001. A+B Format (20)
题目原文: Calculate a + b and output the sum in standard format -- that is, the digits must be separated ...
- 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 ...
- 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 ...
- 【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 ...
- PAT甲级 1001 A+B Format
题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400 1001 A+B Format ( ...
- PAT 甲级 1001 A+B Format
https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400 Calculate a + b and ou ...
- 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 ...
- 1001.A+B Format (20)(思路,bug发现及其修改,提交记录)
https://github.com/031502316a/object-oriented/tree/master/1001 ---恢复内容开始--- 1.解题思路 一开始见到题目时,感觉难的就是输出 ...
随机推荐
- 《深入理解JAVA虚拟机》----------第三章 垃圾收集器与内存分配策略,笔记(下)
1.垃圾收集器 1.1 Serial收集器 这个收集器是一个单线程的收集器,它在进行垃圾收集时,必须暂停其他所有的工作线程. 它是虚拟机运行在Client模式下的默认新生代收集器,它简单而高效. 1. ...
- HTML5 data属性
在HTML5中添加了data-*的方式来自定义属性,所谓data-*实际上上就是data-前缀加上自定义的属性名,命名可以用驼峰命名方式,但取值是必需全部使用小写,否则是undefinde 使用这样的 ...
- File "/usr/bin/yum", line 30 except KeyboardInterrupt, e:
1.centos7 系统,自带python2.7 ,我安装了python3.5 并设置为系统默认python ,yum 语法不支持 2.解决办法: ls /usr/bin | grep python选 ...
- Oracle VM VirtulBox 安装Ubuntu16.04
曾经自己在电脑中检索到Ubuntu kylin 16-10.vmdk 后就通过. 这种方式就进行了新建. 后自己从http://www.gaofumei.net/linux-download/783. ...
- pring Boot 与Spring Cloud版本对应
Spring Boot Spring Cloud 1.2.x Angel版本 1.3.x Brixton版本 1.4.x Camden版本 1.5.x Dalston版本.Edgware版本 2.0. ...
- ettercap的使用
ettercap -i eth0 -T -M arp:remote -q /<网关地址>// /<目标地址>// arp:remote ,表示双向 使用图形化界面 etterc ...
- kafka消息队列的简单理解
kafka在大数据.分布式架构中都很流行.kafka可以进行流式计算,也可以做为日志系统,还可以用于消息队列. 本篇主要是消息队列相关的知识. 零.kafka作为消息队列的优点: 分布式的系统 高吞吐 ...
- dedecms 后台修改系统设置,但是config.cache.inc.php文件不能写入
fopen居然返回false,既不是目录或文件权限相关问题,也不是文件路径问题(相对路径.绝对路径)等,更不是打开文件的模式问题(r,w,a等).网上搜了一会,说到返回false的原因无非都是上面三种 ...
- 比较perl+python
作者:iTech出处:http://itech.cnblogs.com/ http://hyperpolyglot.org/scripting perl (1987) python (1991) ...
- flash as3.0 截图保存图片
import flash.display.MovieClip; import flash.events.MouseEvent; import flash.filesystem.*; ///////// ...