1001 A+B Format (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 Specification:

Each input file contains one test case. Each case contains a pair of integers a and b where −10​6​​≤a,b≤10​6​​. 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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. PAT (Advanced Level) Practice 1001-1005

    PAT (Advanced Level) Practice 1001-1005 PAT 计算机程序设计能力考试 甲级 练习题 题库:PTA拼题A官网 背景 这是浙大背景的一个计算机考试 刷刷题练练手 ...

  4. PAT (Advanced Level) Practice(更新中)

    Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...

  5. PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...

  6. PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642 题目描述: Shuffling is a procedure us ...

  7. 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 ...

  8. PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...

  9. 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) ...

随机推荐

  1. 工作经验(Unity篇)

    我的工作是C++开发,主要是做底层,其中绝大部分是给Unity调用的,以下是我的脚印,希望不会重蹈覆辙 Unity具有强大的跨平台性,但是使用到库文件不尽相同,例如Android中就使用so库文件,W ...

  2. SpringBoot | 第八章:统一异常、数据校验处理

    前言 在web应用中,请求处理时,出现异常是非常常见的.所以当应用出现各类异常时,进行异常的捕获或者二次处理(比如sql异常正常是不能外抛)是非常必要的,比如在开发对外api服务时,约定了响应的参数格 ...

  3. LR C语言语句复习,几个简单代码

    嵌套循环 Action() { int i,j; ;i<=;i++) { ) beark; else lr_output_message("i=%d",i); ;j<= ...

  4. <Linux系统isosize指令用法>

    isosize命令:iso9660文件系统大小显示 isosize命令用于显示iso9660文件系统的大小,还文件可以使普通文件,也可以是块设备,如/dev/sr0或者/dev/sda.如果没有相关选 ...

  5. 几百道常见Java初中级面试题

     注:  有的面试题是我面试的时候遇到的,有的是偶然看见的,还有的是朋友提供的, 稍作整理,以供参考.大部分的应该都是这些了,包含了基础,以及相对深入一点点的东西.   JAVA面试题集 基础知识: ...

  6. mysql数据库初步了解

    一丶数据库服务器丶数据管理系统丶数据库丶表与记录的关系 记录:1 xxxx 3245646546(多个字段的信息组成一条记录,即文件中的一行内容) 表: Student.school,class_li ...

  7. Android 使用GreenDao 添加字段,删除表,新增表操作

    GreenDao 给我个人感觉 比一般的ORM框架要好很多,虽然说上手和其他的比起来,较复杂,但是如果使用熟练以后,你会爱上这个框架的 用这些ORM 框架给我的感觉都是,当升级时,都需要进行数据库所有 ...

  8. Android 5.0以上获取系统运行进程信息

    在Android 5.0以上系统,调用getRunningAppProcesses 方法返回的列表为空,这是因为谷歌考虑到安全原因,已经把这个方法移除掉了, 那以后要获取系统运行的后台进程这个方法用不 ...

  9. URL最大长度问题

    在http协议中,其实并没有对url长度作出限制,往往url的最大长度和用户浏览器和Web服务器有关,不一样的浏览器,能接受的最大长度往往是不一样的,当然,不一样的Web服务器能够处理的最大长度的UR ...

  10. UGUI的事件系统分析

    UGUI的源码还是非常清晰的,打开源码可以发现, 从UGUI的源码可知:在EventSystem中调用每一帧函数来实现: private void TickModules() { for (var i ...