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 ...
随机推荐
- JavaScript总结(一)
什么是JavaScript? 首先我们要知道它是什么?JavaScript是一门专门用来对网页进行编程的脚本语言:因为我是学习Java之后再来学习JavaScript的,所以我的第一疑问JavaScr ...
- CF833B The Bakery 线段树,DP
CF833B The Bakery LG传送门 线段树优化DP. 其实这是很久以前就应该做了的一道题,由于颓废一直咕在那里,其实还是挺不错的一道题. 先考虑\(O(n^2k)\)做法:设\(f[i][ ...
- python赋值、浅拷贝、深拷贝区别
在写Python过程中,经常会遇到对象的拷贝,如果不理解浅拷贝和深拷贝的概念,你的代码就可能出现一些问题.所以,在这里按个人的理解谈谈它们之间的区别. 一.赋值(assignment) 在<Py ...
- 【日常训练】Help Victoria the Wise(Codeforces 99C)
题意与分析 这题意思是这样的:在正方体的六面镶嵌给定颜色的宝石(相同颜色不区分),然后问最多有几种彼此不等价(即各种旋转过后看起来一致)的方案. 其实可以乱搞,因为范围只有720.求出全排列,然后每个 ...
- Zigbee系列(路由机制)
参考文档: ug103-02-fundamentals-zigbee.pdf section4 zigbe routing concepts docs-05-3474-21-0csg-zigbee-s ...
- 【转】sshpass-Linux命令之非交互SSH密码验证
sshpass-Linux命令之非交互SSH密码验证 ssh登陆不能在命令行中指定密码.sshpass的出现,解决了这一问题.sshpass用于非交互SSH的密码验证,一般用在sh脚本中,无须再次 ...
- Python构建web应用(进阶版)->对网页HTML优化逻辑显示
本篇是承接上一篇web应用(入门级)的内容往下顺延的,阅读后将会了解HTML逻辑显示优化,如下图所示,从杂乱无章的日志文件到一个整齐的列表显示. —————————————————————————— ...
- 腾讯hr面
腾讯hr面面经 20181018 寒暄几句 hr自我介绍 hr介绍面试和最后出结果的时间周期 进入主题 自我介绍 考研成绩专业第一 聊考研(考研的经过.考研和保研) 本科成绩 考研成绩超长发挥还是正常 ...
- Redis源码阅读(四)集群-请求分配
Redis源码阅读(四)集群-请求分配 集群搭建好之后,用户发送的命令请求可以被分配到不同的节点去处理.那Redis对命令请求分配的依据是什么?如果节点数量有变动,命令又是如何重新分配的,重分配的过程 ...
- FFT(快速傅里叶变换)算法详解
多项式的点值表示(Point Value Representation) 设多项式的系数表示(Coefficient Representation): \[ \begin{align*} \mathr ...