PAT-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 −106≤a,b≤106. 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
就是计算a+b然后输出的水题,要注意符号判断,如果为0的话就输出一个0就可以了.
做任何题都要小心结果会不会就是一个0,还有一个0不算是前导0.
#include <iostream>
#include <bits/stdc++.h>
#define de(x) cout<<#x<<" "<<(x)<<endl
using namespace std;
int a[10];
int cur=0;
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int sum=n+m;
bool sign_flag=true;
bool zero_flag=false;
if(sum==0)zero_flag=true;
if(sum<0)sign_flag=false,sum=-sum;///
while(sum>0)
{
a[cur++]=sum%10;
sum/=10;
}
if(!sign_flag)printf("-");
int cnt=0;
for(int i=cur-1;i>=0;i--)
{
printf("%d",a[i]);
cnt++;
if(i%3==0&&i!=0)
{
printf(",");
}
}
if(zero_flag)printf("0");
printf("\n");
return 0;
}
PAT-1001 A+B Format (20 分) 注意零的特例的更多相关文章
- PAT 1001 A+B Format (20分) to_string()
题目 Calculate a+b and output the sum in standard format -- that is, the digits must be separated into ...
- 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 ...
- 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 ...
- PAT甲级——1001 A+B Format (20分)
Calculate a+b and output the sum in standard format – that is, the digits must be separated into gro ...
- 【PAT甲级】1001 A+B Format (20 分)
题意:给两个整数a,b,计算a+b的值并每三位用逗号隔开输出(−1e6≤a,b≤1e6) AAAAAccepted code: #include<bits/stdc++.h> us ...
- PAT A1001 A+B Format (20 分)
AC代码 #include <cstdio> #include <algorithm> using namespace std; const int maxn = 11; in ...
- PAT 1001 A+B Format (20 point(s))
题目: 我一开始的思路是: 用math.h中的log10函数来计算位数(不建议这么做,因为会很慢,而且会出一点别的问题): 用pow函数根据要插入分号的位置来拆分a+b成一个个数字(例如res / p ...
- 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 ...
随机推荐
- C++ STL介绍——简介
目录 1.什么是STL 2.STL中六大组件 2.1 容器(Container) 2.2 迭代器(Iterator) 2.3 算法(Algorithm) 2.4 仿函数(Functor) 2.5 适配 ...
- Linux 连接memcache 拒绝连接,防火墙关闭,selinux disabled 仍然不行,最后在外站找到原因,为服务器添加memcache访问权限
最后啊,不行,直接装memcached https://www.runoob.com/memcached/memcached-install.html 附上连接:https://www.presta ...
- OpenJudge计算概论-买房子
/*================================================================= 买房子 总时间限制: 1000ms 内存限制: 65536kB ...
- MySQL 8.0: From SQL Tables to JSON Documents (and back again)
MySQL 8.0: From SQL Tables to JSON Documents (and back again) | MySQL Server Bloghttps://mysqlserver ...
- el-table的type="selection"的使用
场景:el-table,type="selection"时,重新请求后,设置列表更新前的已勾选项 踩坑:在翻页或者changPageSize之后,table的data会更新,之前勾 ...
- 骑行川藏--新都桥&塔公草原
新都桥 塔公草原 新都桥,位于四川省甘孜藏族自治州康定市西部地区,距市区81公里: 别名:东俄罗,一个镇名.海拔约3300米,没有突出的标志性景观,沿线有10余公里被称为“摄影家走廊”. 神奇光线,无 ...
- 123456---com.twoapp.xiaoxiaofeixingyuan---小小飞行员
com.twoapp.xiaoxiaofeixingyuan---小小飞行员
- JQ操作select项
jQuery获取Select元素,并选择的Text和Value: 1. $("#select_id").change(function(){//code...}); //为Se ...
- git:early EOF the remote end hung up unexpectedly index-pack failed RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054
执行: git config http.sslVerify "false" 如果提示: fatal: not in a git directory 执行: git init
- 【C/C++开发】运算符重载
c++的一大特性就是重载(overload),通过重载可以把功能相似的几个函数合为一个,使得程序更加简洁.高效.在c++中不止函数可以重载,运算符也可以重载.由于一般数据类型间的运算符没有重载的必要, ...