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.解题思路 一开始见到题目时,感觉难的就是输出 ...
随机推荐
- (转)如何禁用Windows 10系统的触摸屏
https://baijiahao.baidu.com/s?id=1593890738706748667 现在许多优质的Windows 10个人电脑都配备了触摸屏,因为触摸屏的日益普及,Windows ...
- IP/IGMP/UDP校验和算法
校验和算法:IP.IGMP.UDP和TCP报文头部都有检验和字段,其算法都是一样的. IP.IGMP.UDP和TCP校验和的范围:仅报文头部长度. 在发送数据时,为了计算数据包的检验和.应该按如下步骤 ...
- VC++ MFC如何生成一个可串行化的类
一.MFC允许对象在程序运行的整个过程中持久化的串行化机制(1)串行化是指向持久化存储媒介(如一个磁盘文件)读或写对象的过程.(2)串行化用于在程序运行过程时或之后修复结构化数据(如C++类或结构)的 ...
- 如何配置eclipse的安卓SDK下载目录
首先,打开eclipse,主界面如图 2 点击Windows下的preference 3 然后在出现的对话框中选择“android” 4 然后我们就能看到的主界面,在这里输入android sdk的目 ...
- asyncio的核心概念与基本架构
https://blog.csdn.net/qq_27825451/article/details/86218230 虽然有部分运行错误,但是还是有参考价值.
- kafka 删除topic清空数据
原 kafka 删除topic清空数据 2018年11月20日 18:17:50 Ming! 阅读数:1391 版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载. https://bl ...
- php数组按值的大小排序
array_multisort(array_column($nima,'zongfen'),SORT_DESC,$nima);
- ubuntu下mysql源码编译安装
建议:cpu4核以上,内存4G以上 1. 安装环境:Ubuntu Server 14.10MySQL-5.6.23.tar.gz 2. 安装必备的工具sudo apt-get install make ...
- mysql ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
为了加强安全性,MySQL5.7为root用户随机生成了一个密码,在error log中,关于error log的位置,如果安装的是RPM包,则默认是/var/log/mysqld.log. 一般可通 ...
- 验货或VIP带尾续的半成品,不同客户对于相同编码,需要维护不同的尾续
前提:验货或VIP带尾续的半成品 不同客户对于相同编码,需要维护不同的C开头的尾续. 例子: 以下验货客户编码102001001134CZ绑定了SO:5000144993,而且目前5000144993 ...