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) ...
随机推荐
- FFmpeg编译i386 OSX 脚本
话不多说, 直接上脚本 #!/bin/sh # directories PLATFORM="OSX" # FFmpeg脚本目录 SOURCE="ffmpeg-2.8.7& ...
- Vmware 可用的激活码
4F207-8HJ1M-WZCP8-000N0-92Q6G 0A6Z5-8H1EJ-WZCL1-PK072-23DJG 0F0Q9-8F38L-RZXT9-4U054-ACW5F JA02E-09H4 ...
- Servlet高级部分Listener
监听器的使用场景: ①:统计在线人数 ②:实现单一登录[一个账号只能在一台机器上登录] Servlet中的8大监听器: 1. ServletContextListener [接口方 ...
- Android compress 压缩 会不会失真
微信的缩略图要求是不大于32k,这就需要对我的图片进行压缩.试了几种方法,一一道来. 代码如下 ByteArrayOutputStream baos = new ByteArrayOutputStre ...
- C++ 宏定义的简单使用
1.定义常量 #define ARRMAX 50 int arr[ARRMAX]; (这种做法不如直接用const来直接定义常量.) 2.代替模板函数或者内联函数,将函数定义成宏.执行效率很快 #de ...
- C#启动或停止 计算机中“服务”
第一.要添加一个引用System.ServiceProcess 第二.要在程序中使用命名空间ServiceProcess 代码片段: using System.ServiceProcess; Serv ...
- GitLab-CE-8.9.4 (OpenLogic CentOS 7.2)
平台: CentOS 类型: 虚拟机镜像 软件包: gitlab-8.9.4 bug tracking collaboration commercial development devops git ...
- 手机端@media screen布局自适应
@media only screen and (min-width: 310px) and (max-width: 360px) { }@media only screen and (min-widt ...
- 新建framework的bundle资源 linker command failed with exit code 1解決
enable bitcode 设为no
- 根据图片的URL来实例化图片
正常的Image图片类实例化的时候都需要使用本地的虚拟路径而不能使用URL,如果使用URL就会出现 不支持 URI 格式 这样的问题,正确的写法如下: HttpWebRequest reques ...