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

Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.

Output

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.先对和 sum 求绝对值,定义一个布尔型变量记录sum的正负
  2.对 sum 进行条件判断
    1)sum <1000 ,判断正负,直接打印
    2)1000<= sum <1000000 ,判断正负,打印一个逗号
    3)sum >=1000000 ,判断正负,打印两个逗号
  3.对于正负的判断使用三目运算符
//两种均可
cout<<(flag?"-":"\0")<<sum;  
cout<<(flag?"-":"")<<sum;
    若非负,则打印空字符"\0"(不是空格),若为负,打印"-"
    **注意:若程序为 flag?'-':'\0' ,则'\0'打印出的为空格,所以要写出"-"和"\0"
  4.关于 '\0' 和 "\0"
    \0 为结束字符,对应的 ASCII值为0,在ASCII中占一个字节,所以打印 '\0'会显示空格
    在 \0 后面的字符全为空,所以仅包含 \0 的字符串为空串,即 "\0" 为空字符串,和 "" 效果一样
    在使用 strlen() 函数求字符串的长度时,结束字符'\0'不会被计算在内;当使用 sizeof() 函数求字符串占用的内存空间时,结束字符'\0' 被计算在内
  5.注意在打印时,为0的位置上要补0 C++ 代码如下:
 #include <bits/stdc++.h>
using namespace std;
int main() {
int a,b,sum;
bool flag=false;
cin>>a>>b;
sum=a+b;
if(sum<){
sum=abs(sum);
flag=true;
}
if(sum<) cout<<(flag?"-":"")<<sum;
else if(<=sum&&sum<)
cout<<(flag?"-":"")<<(sum/)<<','<<setw()<<setfill('')<<(sum%);
else {
cout<<(flag?"-":"")<<(sum/);
cout<<","<<setw()<<setfill('')<<(sum%/);
cout<<','<<setw()<<setfill('')<<(sum%%);
}
system("pause");
return ;
}

【PAT】1001. A+B Format (20)的更多相关文章

  1. PAT 甲级 1001 A+B Format (20)(20 分)

    1001 A+B Format (20)(20 分) Calculate a + b and output the sum in standard format -- that is, the dig ...

  2. PAT 甲级1001 A+B Format (20)(C++ -思路)

    1001 A+B Format (20)(20 分) Calculate a + b and output the sum in standard format -- that is, the dig ...

  3. PAT甲 1001. A+B Format (20) 2016-09-09 22:47 25人阅读 评论(0) 收藏

    1001. A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Calculate ...

  4. 【PAT甲级】1001 A+B Format (20 分)

    题意:给两个整数a,b,计算a+b的值并每三位用逗号隔开输出(−1e6​​≤a,b≤1e6​​) AAAAAccepted code: #include<bits/stdc++.h> us ...

  5. PAT甲级 1001. A+B Format (20)

    题目原文: Calculate a + b and output the sum in standard format -- that is, the digits must be separated ...

  6. 【PAT】B1044 火星数字(20 分)

    /* 火星文有两位,第二位为0不输出 */ #include<stdio.h> #include<algorithm> #include<string.h> #in ...

  7. 【PAT】B1068 万绿丛中一点红(20 分)

    一开始因为看见这题就头疼,可费了点时间. 要考虑所有元素,如果忽略外圈元素,最后一个样例过不去. 而且要求只出现一次的元素, 我没有使用map,因为当时还没学,用map储存元素会节约好多代码 #inc ...

  8. 【PAT】1018 锤子剪刀布 (20)(20 分)

    1018 锤子剪刀布 (20)(20 分) 大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示: 现给出两人的交锋记录,请统计双方的胜.平.负次数,并且给出双方分别出什么手势的胜算 ...

  9. 【PAT】1014. 福尔摩斯的约会 (20)

    1014. 福尔摩斯的约会 (20) 大侦探福尔摩斯接到一张奇怪的字条:“我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hy ...

随机推荐

  1. selenium测试 - open Firefox

    环境:Python2.7+selenium3+Firefox47   问题1: 在打开火狐浏览器时报错:‘geckodriver‘ executable needs to be in PATH fro ...

  2. TopCoder SRM420 Div1 500pt RedIsGood

    桌面上有R 张红牌和B 张黑牌,随机打乱顺序后放在桌面上,开始一张一张地翻牌,翻到红牌得到1 美元,黑牌则付出1 美元.可以随时停止翻牌,在最优策略下平均能得到多少钱. R,B ≤ 100000. 输 ...

  3. python学习(十七) 爬取MM图片

    这一篇巩固前几篇文章的学到的技术,利用urllib库爬取美女图片,其中采用了多线程,文件读写,目录匹配,正则表达式解析,字符串拼接等知识,这些都是前文提到的,综合运用一下,写个爬虫示例爬取美女图片.先 ...

  4. bzoj 3209 bzoj1799 数位dp

    3209: 花神的数论题 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2267  Solved: 1040[Submit][Status][Disc ...

  5. 基于OpenResty和Node.js的微服务架构实践

    什么是微服务? 传统的单体服务架构是单独服务包,共享代码与数据,开发成本较高,可维护性.伸缩性较差,技术转型.跨语言配合相对困难.而微服务架构强调一个服务负责一项业务,服务可以单独部署,独立进行技术选 ...

  6. 手动搭建高可用的kubernetes 集群

    之前按照和我一步步部署 kubernetes 集群的步骤一步一步的成功的使用二进制的方式安装了kubernetes集群,在该文档的基础上重新部署了最新的v1.8.2版本,实现了kube-apiserv ...

  7. echo 不换行

    原文 http://blog.sina.com.cn/s/blog_4da051a6010184uk.html echo -n 不换行输出   $echo -n "123" $ec ...

  8. shizhong

    <script charset="Shift_JIS" src="http://chabudai.sakura.ne.jp/blogparts/honehonecl ...

  9. linux shell学习四

    Shell分支语句 case 值 in 模式1) command1 command2 command3 ;; 模式2) command1 command2 command3 ;; *) command ...

  10. [DeeplearningAI笔记]序列模型1.1-1.2序列模型及其数学符号定义

    5.1循环序列模型 觉得有用的话,欢迎一起讨论相互学习~Follow Me 1.1什么是序列模型 在进行语音识别时,给定了一个输入音频片段X,并要求输出片段对应的文字记录Y,这个例子中的输入和输出都输 ...