【PAT】1001. A+B Format (20)
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)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 【PAT甲级】1001 A+B Format (20 分)
题意:给两个整数a,b,计算a+b的值并每三位用逗号隔开输出(−1e6≤a,b≤1e6) AAAAAccepted code: #include<bits/stdc++.h> us ...
- PAT甲级 1001. A+B Format (20)
题目原文: Calculate a + b and output the sum in standard format -- that is, the digits must be separated ...
- 【PAT】B1044 火星数字(20 分)
/* 火星文有两位,第二位为0不输出 */ #include<stdio.h> #include<algorithm> #include<string.h> #in ...
- 【PAT】B1068 万绿丛中一点红(20 分)
一开始因为看见这题就头疼,可费了点时间. 要考虑所有元素,如果忽略外圈元素,最后一个样例过不去. 而且要求只出现一次的元素, 我没有使用map,因为当时还没学,用map储存元素会节约好多代码 #inc ...
- 【PAT】1018 锤子剪刀布 (20)(20 分)
1018 锤子剪刀布 (20)(20 分) 大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示: 现给出两人的交锋记录,请统计双方的胜.平.负次数,并且给出双方分别出什么手势的胜算 ...
- 【PAT】1014. 福尔摩斯的约会 (20)
1014. 福尔摩斯的约会 (20) 大侦探福尔摩斯接到一张奇怪的字条:“我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hy ...
随机推荐
- 树莓派安装python3.5
https://gist.github.com/BMeu/af107b1f3d7cf1a2507c9c6429367a3b Installing Python 3.5 on Raspbian As o ...
- flask学习之解决Internal Server Error问题的方式之一
最近在学习flask web development的时候,遇到了这么一个问题,就是照着书上敲的代码,跑起来是Internal server error,由于中途学的时候为了方便,改用pycharm来 ...
- AndroidManifest.xml 权限 中英对照表
声明: 1.本文转载自:http://www.52pojie.cn/thread-304613-1-1.html 2.如有转载请复制上面连接声明,尊重原创 常用权限对照表 android.permis ...
- hashlib模块--加密
用于加密相关的操作,3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法 import hashlib m = ha ...
- linux 隐藏权限
原文 ------通过chattr设置档案的隐藏权限------ [root@sdc ~]#chattr --helpUsage: chattr [-RV] [-+=AacDdijsSu] [-v v ...
- 个人最常用的vim操作
本文只记录个人工作中最常用到的vim快捷键,不是很全,但是已经覆盖了绝大多数功能. 参考:<鸟哥Linux私房菜>以及https://www.cnblogs.com/momofan/p/5 ...
- 修改Tomcat端口
1.自己的tomcat修改端口号,在tomcat解压缩目录中conf目录下又一个server.xml,将HTTP协议的端口修改为自己想要的即可 <Connector port="808 ...
- Java BLOB 数据的插入与读取 操作
package com.lw.database; import java.io.FileInputStream; import java.io.FileOutputStream; import jav ...
- ubuntu学习命令
1.双系统下挂载windows硬盘 检测ntfs-3g是否安装:locate ntfs-3g 没有则安装: sudo apt-get install ntfs-3g 查看硬盘信息: sudo fdis ...
- sylk文件
症状:excel表出现提示:sylk文件...导致excel表不可读取 原因:文件内容有“ ID ” 字段,估计是固定的识别“ID”或“ID_XXXX” 修改方法:将ID中的任意字母换成小写即可 转载 ...