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. SpringBoot | 第十八章:web应用开发之WebJars使用

    前言 前面一章节我们主要讲解了关于文件上传的两种方式.本章节继续web开发的相关知识点.通常对于web开发而言,像js.css.images等静态资源版本管理是比较混乱的,比如Jquery.Boots ...

  2. asp.net MVC 4.0 Model元数据回顾——HtmlHelper的ModelMetadata

    模板方法包括Display/DisplayFor.Editor/EditorFor.DisplayForModel/EditForModel提供辅助生成Html的模型元数据信息 public stat ...

  3. linux启动mysql报错 Starting MySQL... ERROR! The server quit without updating PID file (XXXX pid文件位置)

    最近在云服务器上安装mysql  启动时报错了,从错误中可以看出,定位在pid文件上,有三种解决方案 1.重启服务器:因为服务器更新时,可能会禁用某些守护进程,重启后即可恢复 2.删除配置文件,重启试 ...

  4. css3的transform变换scale和translate等影响jQuery的position().top和offset().top

    css3的transform变换scale和translate等影响jQuery的position().top和offset().top

  5. C#利用WebClient 两种方式下载文件(一)

    WebClient client = new WebClient(); 第一种 string URLAddress = @"http://files.cnblogs.com/x4646/tr ...

  6. Jscript 命名规范

    变量命名都以类型前缀+有意义的单词组成,用驼峰式命名法增加变量和函式的可读性.例如:sUserName,nCount. 前缀规范:每个局部变量都需要有一个类型前缀,按照类型可以分为:s:表示字符串.例 ...

  7. meterpreter > screenshot

    meterpreter > screenshotScreenshot saved to: /opt/metasploit/msf3/wLafQYhx.jpegmeterpreter > / ...

  8. http请求各种数据的接受

    一.接受请求的http数据 1.接受客户请求的数据,例如xml数据格式,代码如下: 此代码相当于服务端 public String analyzeWeixinXMLStr(HttpServletReq ...

  9. HBuilder发行App(Android和ios)

    怎样将开发好的app测试和上架,此文包括Android和ios打包.测试和上架的大概过程.内容有些简陋,因为此过程踏坑无数,特此留念. 特此声明:内容不全仅供参考. 介绍两个参考网站: 1. http ...

  10. 测试发布(maven-assembly-plugin看好你哦)

    项目改成了maven管理,现场需要用增量补丁包的形式发布代码: 2015/4/21 以前试过用ant打补丁包,现在试试maven能不能做同样的事情: maven-assembly-plugin看着可以 ...