PAT 甲级 1001 A+B Format
https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400
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
代码:
#include <bits/stdc++.h>
using namespace std; const int maxn = 1e5 + 10;
char s[maxn], out[maxn];
int a[maxn]; void zlr_itoa(int x) {
if(x == 0) {
s[0] = '0';
s[1] = 0;
return ;
}
int top = 0;
while(x) {
a[top ++] = x % 10;
x = x / 10;
}
int sz = 0;
while(top != 0) {
s[sz++] = (char)(a[-- top] + '0');
s[sz] = 0;
}
} int main() {
int a, b;
scanf("%d%d", &a, &b);
int sum = a + b;
if(sum == 0) {
printf("0\n");
return 0;
}
if(sum < 0) {
printf("-");
sum = sum * (-1);
} zlr_itoa(sum);
int len = strlen(s); if(len < 3) {
printf("%s\n", s);
return 0;
} if(len % 3 == 0) {
for(int i = 0; i < len - 3; i += 3) {
for(int j = i; j < i + 3; j ++)
printf("%c", s[j]);
printf(",");
}
printf("%c%c%c", s[len-3], s[len - 2], s[len - 1]);
} else if(len % 3 == 1) {
printf("%c,", s[0]);
for(int i = 1; i < len - 4; i += 3) {
for(int j = i; j < i + 3; j ++)
printf("%c", s[j]);
printf(",");
}
printf("%c%c%c", s[len-3], s[len - 2], s[len - 1]);
} else if(len % 3 == 2) {
printf("%c%c,", s[0], s[1]);
for(int i = 2; i < len - 5; i += 3) {
for(int j = i; j < i + 3; j ++)
printf("%c", s[j]);
printf(",");
}
printf("%c%c%c", s[len-3], s[len - 2], s[len - 1]);
} printf("\n");
return 0;
}
PAT 甲级 1001 A+B Format的更多相关文章
- PAT甲级 1001 A+B Format
题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400 1001 A+B Format ( ...
- 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)
题目原文: Calculate a + b and output the sum in standard format -- that is, the digits must be separated ...
- 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) 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)
1001. A+B Format (20) Calculate a + b and output the sum in standard format -- that is, the digits m ...
- 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水题飘过
#include<iostream> using namespace std; int main(){ int a, b; while(scanf("%d%d", &a ...
随机推荐
- 20155338《网络对抗技术》 Exp1 PC平台逆向破解
20155338<网络对抗技术> Exp1 PC平台逆向破解 实践目标 1.实践的对象是一个名为pwn1的linux可执行文件. 2.该程序正常执行流程是:main调用foo函数,foo函 ...
- 关于DP和背包
听说过动态规划(DP)的同学应该都知道有背包问题的存在. 首先我们来了解一下动态规划 基本思想: 动态规划算法通常用于求解具有某种最优性质的问题. 在这类问题中, 可能会有很多可行解.每一个解都对应于 ...
- 【转载】基于MFC的ActiveX控件开发(2)
原文:http://iysm.net/?p=118 2.生成并测试控件 好,现在我们就可以先来生成一下这个项目,当然到目前我们只是用系统自动生成的一个控件项目,什么功能都没有,只是一个空框架. 几秒钟 ...
- 01- ajax, 登录验证,json数据,文件上传
1.ajax简介 1.向服务器发送请求的途径 # 向服务器发送请求的途径 1. 浏览器地址栏,默认get请求 2. form表单: get请求 post请求 3. a标签,默认get请求 4. Aja ...
- 初学者下载使用Python遇到的问题看它就行了
首先在python管网(www.python.org)中找到对应的版本与系统,以(window7系统64位python3.7.3为例) 打开电脑--打开浏览器--输入www.python.org--d ...
- TensorFlow Python3.7环境下的源码编译(三)编译
这里要为仅支持 CPU 的 TensorFlow 构建一个 pip 软件包,需要调用以下命令: $ bazel build --cxxopt="-D_GLIBCXX_USE_CXX11_AB ...
- 利用for循环如何判定是水仙花数
水仙花数业内的大家可能听说过,但是对于初学者来讲,对于水仙花数还是比较陌生的. 首先要知道的是水仙花数的计算公式:153=1**3+5**3+3**3: 如何去判定这个数是否为水仙花数,最好的办法就是 ...
- Unity 实现一个简单的 TPS 相机
效果如下: 代码如下: public class TPSCamera : MonoBehaviour { /// <summary> /// 目标对象 /// </summary&g ...
- 关于springcloud的一些问题总结.txt
@Bean public CorsFilter corsFilter() { final UrlBasedCorsConfigurationSource source = new UrlBasedCo ...
- Controller层@PathVariable使用
@PathVariable 映射 URL 绑定的占位符 带占位符的 URL 是 Spring3.0 新增的功能,该功能在SpringMVC 向 REST 目标挺进发展过程中具有里程碑的意义通过 @Pa ...