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 ...
随机推荐
- 20155229 《信息安全系统设计基础》 week10 课上测试ch06
1( 单选题 | 1 分) 下面代码中,对数组x填充后,采用直接映射高速缓存,所有对x和y引用的命中率为() A . 1 B . 1/4 C . 1/2 D . 3/4 正确答案: D 解析:填充消除 ...
- easybcd删除win10启动项如何恢复?
制作windows启动盘: u盘启动 开始安装,选择左下角的"修复计算机": 运行命令 bcdboot c:\windows /l zh-cn 从系统盘C:\Windows目录中复 ...
- 排序算法:快速排序解析及Python实现
关键词:分而治之.递归.计算速度.基准值 1. 什么是分而治之? 1.1 分而治之(divide and conquer)一种递归式方法 1.2 找出基线条件,这种条件必须尽可能简单 1.3 不断将问 ...
- CF终于上紫了。。。
纪念一下...
- 【操作系统】C语言编写的FAT16文件系统
[操作系统]C语言编写的FAT16文件系统 这是操作系统的期末课程设计作业之一,主要功能是在物理内存中虚拟出一个1M大小的FAT16的文件系统,然后把它读入内存中,进行具体的文件操作,具体的实用性不大 ...
- nginx 部署前期一定要关闭selinux
nginx 报错: 1389#1389: *40 "/home/data1/index.html" is forbidden (13: Permission denied), cl ...
- netty+proto使用简要记录
1. maven环境配置protobuf 2.生成.proto文件 3.将.proto转为java文件 打开电脑的cmd,进入.proto所在文件位置,输入命令:protoc.exe --java_o ...
- Windows7 jmeter3.1安装(咋个安装?)
这是一个比较详细的安装教程0.0,不懂可以私我,有错也可以私我 1.0 首先我们得有jdk,我选择的是1.8版本的jdk, QQ群:550654190,(进群答案:李熠)群文件里有. 进去后下载 ...
- k8s踩坑记第1篇--rc无法创建
六一快乐!!! 什么是k8s,我不想解释,百度资料有很多,本系列只踩坑,不科普. 问题描述: 做Hello World的例子,结果get pods一直显示没有资源? 应用配置代码: apiVersio ...
- prometheus-operator 监控 Rabbitmq集群
首先我们监控服务需要知道prometheus-operator是如何去工作的,才好去写相关的yaml配置,这里我划分成了5个部分,如果容器服务本身就以k8s来编排的,那就只需要三步,这里因为我的rab ...