PAT Advanced 1001
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 −106≤a,b≤106. 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.(通用方法)直接将数字转化为字符串进行操作
#include<iostream>
#include<sstream>
#include<string>
using namespace std;
int main(){
long long int a,b;
string c;
cin>>a>>b;
a += b;
stringstream s1;
s1<<a;
s1>>c;
int num = c.length();
for( int i = 0; i < num; i++){
cout<<c[i];
if(c[i] == '-')
continue;
else if((num-i-1)%3 == 0&& (num-i-1) != 0)
cout<<",";
}
return 0;
}
2.第二种方法是在网上无意中看到的,感觉也非常好,分享一下。
鉴于题目的数据范围已经给定,其实最多只有7位数,那么就分以下几种情况:
1.结果小于1000,无需,
2.结果大于 1000 小于 1,000,000,需要一个逗号,
3.结果大于 1,000,000,需要两个逗号。
#include<iostream>
#include<math.h>
#include<string>
using namespace std; int main(){
long long int a,b;
cin>>a>>b;
a+=b;
if( a < ){
cout<<"-";
a = fabs(a);
}
if( a < )
cout<<a;
else if( a < )
printf("%d,%03d",a/,a%);
else
printf("%d,%03d,%03d",a/,a%/,a%);
return ;
}
注意:输出时因为两个逗号之间经过处理后的数可能为0,所以要用%03d输出,保证数位正确。
PAT Advanced 1001的更多相关文章
- 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 ...
- PAT (Advanced Level) Practice 1001 A+B Format (20 分) 凌宸1642
PAT (Advanced Level) Practice 1001 A+B Format (20 分) 凌宸1642 题目描述: Calculate a+b and output the sum i ...
- PAT (Advanced Level) Practice 1001-1005
PAT (Advanced Level) Practice 1001-1005 PAT 计算机程序设计能力考试 甲级 练习题 题库:PTA拼题A官网 背景 这是浙大背景的一个计算机考试 刷刷题练练手 ...
- PAT (Advanced Level) Practice(更新中)
Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...
- PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...
- PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642 题目描述: Shuffling is a procedure us ...
- PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642
PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642 题目描述: Being unique is so important to peo ...
- PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642
PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...
- PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642
PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642 题目描述: Given any string of N (≥5) ...
随机推荐
- wcf问题集锦
1.处理程序“svc-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler” HTTP 错误 404.3 - Not Found 由于扩展配置问题而无法提供 ...
- 邓俊辉数据结构学习-8-2-B树
B树 概述 动机: B树实现高速I/O 640K如何"满足"任何实际需求了-- 源自比尔·盖茨的一个笑话 前提知识 高速缓存 为什么高速缓存有效? 不同容量的存储器,访问速度差异悬 ...
- devExpress GridControl gridView笔记
gridView1.Appearance.EvenRow.BackColor = Color.FromArgb(, , , ); gridView1.Appearance.OddRow.BackCol ...
- 二维数组的转置(java)
public class ShuZhuDaoZhi { public static void main(String[] args) { int data[][] = new int[][]{{1, ...
- 韦东山笔记之用busybox构建根文件系统
1 百度搜索busybox进入busybox官网(https://busybox.net/)作者:恒久力行 QQ:624668529 点击左侧DownloadSource下载最新稳定版的busybo ...
- *.rpm is not signed解决
1.# yum install qemu*报错如下:Package qemu-kvm-tools-0.12.1.2-2.113.el6.x86_64.rpm is not signed2.解决# vi ...
- 在浏览器地址栏按回车、F5、ctrl+F5刷新页面的区别
url地址栏里敲击enter:这样的刷新,大家可以在firebug里看一下,只有少数的请求会发送出去,而且几乎没有图片的请求,这是因为请求时会先检查本地是不是缓存了请求的图片,如果有缓存而且没有过期( ...
- android-上下文菜单的创建 - 随心
//Menu设置//覆盖两个方法onCreateOptionsMenu(Menu menu).onOptionsItemSelected(MenuItem Item)//onCreateOptions ...
- SQL 使用触发器常见错误
今天做代码审查时,看见以下一段触发器的创建脚本,我们一起来分析一下 create trigger [trigger_puClassRoomType] on [dbo].[puClassRoomType ...
- hdu-1317 XYZZY---Floyd判连通+bellman最短路
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1317 题目大意: 题意:有n个房间(n<=100),每个房间有一个点权(第1号房间和第n号房间 ...