【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 ...
随机推荐
- 三年java面试题
前言: 楼主毕业三年,从大学时期就开始一直从事java web方面的开发.我在去年的今天有一篇帖子:两年java面试经验.经历了一年的上班,成长了很多.今年因为某些原因辞职了.从2月底辞职,到3月初, ...
- 【树链剖分换根】P3979 遥远的国度
Description zcwwzdjn在追杀十分sb的zhx,而zhx逃入了一个遥远的国度.当zcwwzdjn准备进入遥远的国度继续追杀时,守护神RapiD阻拦了zcwwzdjn的去路,他需要zcw ...
- java8系列
参见地址:https://segmentfault.com/a/1190000012211339
- php数组定义
$arr = array( ,), //是’0' ,不是[‘0’] ,), ,), ,), ); $arr = [ '0' => array(2,3), //是’0' ,不是[‘0’] '1' ...
- Vue.js和angular.js区别
Vue.js:易学 简单 指令以v.xxx 一片HTML代码配合json,在new出来Vue,js实例 个人维护项目 适用于移动端 应用超越了angular angular.js:上手难 指令以ng. ...
- JPA映射持久化对象(Entity)
推荐阅读:JPA criteria 查询:类型安全与面向对象 来源: http://blog.sina.com.cn/s/blog_49fd52cf0100rzjn.html 一个普通的POJO类通过 ...
- Jquery 操作 Select 详解
jQuery是如何控制和操作select的.先看下面的代码 比如<select class="selector"></select> 1.设置value为p ...
- Window启动Zookeeper报错java.lang.NumberFormatException: For input string:
用zkServer start命令报如题的错误,改为直接用zkServer启动则ok 还有在window下,myid文件不能是myid.txt,不能带文件格式 dataDir=D:/zookeeper ...
- Visual Studio Code 如何编写运行 C、C++ 程序?
0. 前言 VS Code 是微软发布一款跨平台的源代码编辑器,其拥有强大的功能和丰富的扩展,使之能适合编写许多语言. 本文面向初学者(但不是纯小白),分享一点我配置C/C++的经验. 本文所有内容均 ...
- Codeforces 221 A. Little Elephant and Function
A. Little Elephant and Function time limit per test 2 seconds memory limit per test 256 megabytes in ...