PAT甲级——A1001A+BFormat
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
很简单,数字转字符串即可
#define _CRT_SECURE_NO_WARNINGS #include <iostream>
#include <vector>
#include <sstream>
#include <string>
#include <stack> using namespace std; int main()
{
int a, b,sum;
string str;
scanf("%d %d", &a, &b);
sum = a + b;
stringstream ss;
ss << sum;
ss >> str; ///此处可以通过sum/1000,然后转为字符串
stack<string>res;
int k;
string s;
for (k = str.length(); k > ;k-=)
{
s.assign(str.begin() + k - , str.begin() + k);
res.push(s);
res.push(",");
}
s.assign(str.begin(), str.begin() + k);
if (s == "-")
res.pop();
res.push(s);
while (!res.empty())
{
cout << res.top();
res.pop();
}
return ;
}
更简洁点
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
string s = to_string(a + b);
int len = s.length();
for (int i = ; i < len; i++) {
cout << s[i];
if (s[i] == '-') continue;
if ((i + ) % == len % && i != len - ) cout << ",";
}
return ;
}
PAT甲级——A1001A+BFormat的更多相关文章
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲级1131. Subway Map
PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...
- PAT甲级1127. ZigZagging on a Tree
PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
- PAT甲级1123. Is It a Complete AVL Tree
PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...
- PAT甲级1119. Pre- and Post-order Traversals
PAT甲级1119. Pre- and Post-order Traversals 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二进制树可以通过给定的一对后序和顺序遍历序列来确定,也可以通 ...
- PAT甲级1114. Family Property
PAT甲级1114. Family Property 题意: 这一次,你应该帮我们收集家族财产的数据.鉴于每个人的家庭成员和他/她自己的名字的房地产(房产)信息,我们需要知道每个家庭的规模,以及他们的 ...
- PAT甲级1111. Online Map
PAT甲级1111. Online Map 题意: 输入我们当前的位置和目的地,一个在线地图可以推荐几条路径.现在你的工作是向你的用户推荐两条路径:一条是最短的,另一条是最快的.确保任何请求存在路径. ...
- PAT甲级1107. Social Clusters
PAT甲级1107. Social Clusters 题意: 当在社交网络上注册时,您总是被要求指定您的爱好,以便找到一些具有相同兴趣的潜在朋友.一个"社会群体"是一群拥有一些共同 ...
- PAT甲级1103. Integer Factorization
PAT甲级1103. Integer Factorization 题意: 正整数N的K-P分解是将N写入K个正整数的P次幂的和.你应该写一个程序来找到任何正整数N,K和P的N的K-P分解. 输入规格: ...
随机推荐
- vue 报错:Cannot read property '__ob__' of undefined
我的原因:引入组件后未注册 <script> import ComFirst from "../../components/ComFirst.vue" import C ...
- 常用指令linux总结
linux的基础操作命令: 常见命令: man 查看帮助文档 用法:man + 命令 help 查看指定命令的用法 用法: 命令 --help(有空格) tab linux下命令与文件名补全 用法:在 ...
- vba取局域网电脑共享文件夹下的Excel文件
Private Sub CommandButton1_Click() Dim xlapp1 As Excel.Application Dim xlbook1 As Excel.Workbo ...
- 时间 '2018-08-06T10:00:00.000Z' 格式转化为本地时间(转)
原文:https://blog.csdn.net/sxf_123456/article/details/81582964 from datetime import datetime,timedelta ...
- layui实现已知被选中的option,怎样渲染
在项目中用到layui实现第几个option 实现,在select中渲染出需要展示的option 代码: $("#period option[value="+res.data.se ...
- WifiManager Wifi 管理器&&知识点
WifiManager 主要使用的技术: SimpleWifi,MahaApp.Metro控件 一 网卡设置 1.获取所有网卡(NetWorkAdapter类) 方法A 通过API SELECT * ...
- Java 多线程 - 创建线程的方法 + Executors.newXXXThreadPool()缺点
java中创建线程的三种方法以及区别: https://www.cnblogs.com/3s540/p/7172146.html 通过Executor 的工具类,创建三种类型的普通线程池: https ...
- chrome的驱动安装
首先找到对应的chromedriver,百度搜索,http://npm.taobao.org/mirrors/chromedriver/ 将下载好的chrome驱动解压,放在/usr/loacl/bi ...
- docker-compose安装及docker-compose.yml详解
1.下载安装 [root@cx-- ~]# curl -L https://github.com/docker/compose/releases/download/1.24.1/docker-comp ...
- CSS 常用的兼容性调试技巧
1.实现所有浏览器主页居中 Firefox下主页居中代码:.box{margin:0px auto} IE5.5下主页居中代码:body{text-align:center;} 将以上两种代码,合在一 ...