博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT甲级——A1001A+BFormat
阅读量:4541 次
发布时间:2019-06-08

本文共 1713 字,大约阅读时间需要 5 分钟。

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 很简单,数字转字符串即可
1 #define _CRT_SECURE_NO_WARNINGS 2  3 #include 
4 #include
5 #include
6 #include
7 #include
8 9 using namespace std;10 11 int main()12 {13 int a, b,sum;14 string str;15 scanf("%d %d", &a, &b);16 sum = a + b;17 stringstream ss;18 ss << sum;19 ss >> str;20 21 ///此处可以通过sum/1000,然后转为字符串22 stack
res;23 int k;24 string s;25 for (k = str.length(); k > 3;k-=3)26 { 27 s.assign(str.begin() + k - 3, str.begin() + k);28 res.push(s);29 res.push(",");30 }31 s.assign(str.begin(), str.begin() + k);32 if (s == "-")33 res.pop();34 res.push(s);35 while (!res.empty())36 {37 cout << res.top();38 res.pop();39 }40 return 0;41 }

更简洁点

1 #include 
2 using namespace std; 3 int main() { 4 int a, b; 5 cin >> a >> b; 6 string s = to_string(a + b); 7 int len = s.length(); 8 for (int i = 0; i < len; i++) { 9 cout << s[i];10 if (s[i] == '-') continue;11 if ((i + 1) % 3 == len % 3 && i != len - 1) cout << ",";12 }13 return 0;14 }

 

转载于:https://www.cnblogs.com/zzw1024/p/11159598.html

你可能感兴趣的文章
项目管理知识1
查看>>
在window环境下安装Python中的pip
查看>>
A大龙插件官方群3:621816328
查看>>
oi再见,你好明天。
查看>>
2018 Multi-University Training Contest 1 - D Distinct Values (STL+双指针)
查看>>
js学习笔记一-语法结构
查看>>
键盘对应的键值
查看>>
goLang 纳秒转 毫秒 转 英文时间格式
查看>>
微信小程序的坑坑
查看>>
图片轮播(Jquery)
查看>>
hdu 1704 Rank(floyd传递闭包)
查看>>
Educational Codeforces Round 27 G. Shortest Path Problem?(Guass异或线性基)
查看>>
【BZOJ3622】已经没有什么好害怕的了(动态规划+广义容斥)
查看>>
HDOJ 1023 Train Problem II
查看>>
途牛订单的服务化演进
查看>>
软件工程之四则运算
查看>>
ABAP 根据权限显示或隐藏状态栏的按钮
查看>>
跑步计划
查看>>
mvc中使用uploadify批量上传的应用
查看>>
Kibana查询说明
查看>>