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. wireshark 根据域名筛选

    应该去掉引号

  2. find~~~查找文件

    find . -name "klibc" 在当前文件内查找文件 klibc find . -name "*libc*" 在当前文件内查找文件 (模糊查询)

  3. 装饰器--decorator3

    装饰器添加返回值 import time def timer(func): def wrapper(*args,**kwargs): #wrapper包装的意思 start_time = time.t ...

  4. google的几道面试题

    Question1:如何判断两个矩形是否有重叠区域? Answer(java): public boolean checkCross(Rectangle m, Rectangle n) { //求出m ...

  5. 图论&动态规划:虚树

    虚树可以看做是对树形动态规划的一种求解优化 对于需要求答案的点p,只保留对答案有影响的节点,从而减少时间 BZOJ2286 dp[i]=min(val[i],Σdp[j](j为i的儿子)),val[i ...

  6. 详谈AngularJS的Directive

    指令Directive是AngularJS最重要的核心.我用AngularJS用的并不是很深,一直以来也是在使用中摸索,从一开始的什么都不懂,查不到系统的资料,到开始使用一些简单的数据绑定{{}},到 ...

  7. LintCode 510: Maximal Rectangle

    LintCode 510: Maximal Rectangle 题目描述 给你一个二维矩阵,权值为False和True,找到一个最大的矩形,使得里面的值全部为True,输出它的面积 Wed Nov 2 ...

  8. 在Java中,你真的会日期转换吗

    1.什么是SimpleDateFormat 在java doc对SimpleDateFormat的解释如下: SimpleDateFormat is a concrete class for form ...

  9. lintcode 66.67.68 二叉树遍历(前序、中序、后序)

    AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode le ...

  10. 爬虫--Scrapy框架的基本使用

    流程框架 安装Scrapy: (1)在pycharm里直接就可以进行安装Scrapy      (2)若在conda里安装scrapy,需要进入cmd里输入指令conda install scrapy ...