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. 最简实例演示asp.net5中用户认证和授权(4)

    上篇: 最简实例演示asp.net5中用户认证和授权(3) 上面我们把自定义认证和授权的相关的最小基础类和要实现的接口都实现了,下面就是如何来进行认证和授权的配置. 首先我们要告诉系统,我们的用户和角 ...

  2. Java 多线程的实现方法

    package com.jckb; /**多线程实现的两种方法 * * @author gx * */ public class Test2 { public static void main(Str ...

  3. 【踩坑】vue 无法让后台保存 session

    今天在调试 iblog 客户端时,发现登录后进行增加.删除.更新操作时都提示还没有登录. 此问题曾经在用 ajax 调试时出现过,解决办法是,在请求时带上 creditials: true ,即让发出 ...

  4. PIC IDE编译器变量问题

    1.用const关键字是不能把变量定义到ROM区域的,在IDE编译器里要在变量的定义前面加入rom关键字.例如: rom char tmp[257]={0};const rom char tmp[25 ...

  5. 微信iOS端无法执行jquery on()方法

    微信iOS端无法执行jquery on()方法,click方法可以, 如下代码是不会执行的: $(function(){ $('body').on('click','.cka',function(){ ...

  6. happy2018暑期集训课后习题001

    根据需求补全下面的代码: 需求: 每行输入三个整数a.b.c,根据a的值不同对b和c进行不同的操作: a为0时,计算b+c a为1时,计算b-c a为2时,计算b*c a为3时,计算b/c 并输出操作 ...

  7. 流媒体 6——MPEG电视

    1.电视图像的数据率 1.1 ITU-R BT.601标准数据率 按照奈奎斯特(Nyquist)采样理论,模拟电视信号经过采样(把连续的时间信号变成离散的时间信号)和量化 (把连续的幅度变成离散的幅度 ...

  8. linux 命令——61 wget(转)

    Linux系统中的wget是一个下载文件的工具,它用在命令行下.对于Linux用户是必不可少的工具,我们经常要下载一些软件或从远程服务器恢复备份到本地服务器.wget支持HTTP,HTTPS和FTP协 ...

  9. iptables (2) 基本配置

    iptables 基本命令使用举例 一.链的基本操作 1.清除所有的规则.1)清除预设表filter中所有规则链中的规则.# iptables -F -F, --flush [chain] Flush ...

  10. 选中ListBox控件中的全部项

    实现效果: 知识运用: ListBox控件的SelectedItems属性 //获取ListBox控件中被选中数据项的集合 public ListBox.SelectedObjectCollectio ...