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 ...
随机推荐
- 使用SDNN (space displacement neural network)进行多字体手写识别
手写单字体的识别,在看过卷积神经网络的mnist例子之后,很容易实现,那么如何实现多字体的同时识别呢? 如下图 LeCun大神所用的是SDNN space displacement neural ne ...
- asp.net core 自定义中间件和service
首先新建项目看下main方法: public static void Main(string[] args) { var host = new WebHostBuilder() .UseKestrel ...
- 关于selenium的智能等待页面加载的问题
我们经常会碰到用selenium操作页面上某个元素的时候,需要等待页面加载完成后,才能操作, 否则页面上的元素不存在,会抛出异常. 或者碰到AJAX异步加载,我们需要等待元素加载完成后,才能操作. 首 ...
- Winform下的语言国际化,几行代码轻松实现
最近做了一些关于winform的项目,需要用到winform的语言国际化,在初使化的时候用起来非常方便.可以参考一下: 核心逻辑: 预览效果演示: OK,以下是核心代码和操作流程 一,添加Langua ...
- Jupyter安装及运行
一.安装(来自http://jupyter.org/install) Ctrl+Alt+T(Manjaro系统),打开控制台,su进入root权限 输入如下命令: python3 -m pip ins ...
- Babylon.js官方性能优化文档中文翻译
在这里列出Babylon.js官方性能优化文档的中英文对照,并在CardSimulate项目里对其中的一些优化方法进行实践. How To 如何 Optimize your scene 优化你的场景 ...
- 火狐插件安装-基于web自动化测试
一.Firebug 安装 1. 打开火狐浏览器—选择右上角“打开菜单”(图一)----附件组件(图二) 图一 图二 2. 点击:扩展(图三)—-------用于所有附加组件的工具(图四)----选 ...
- Netty源码分析第2章(NioEventLoop)---->第6节: 执行select操作
Netty源码分析第二章: NioEventLoop 第六节: 执行select操作 分析完了selector的创建和优化的过程, 这一小节分析select相关操作 跟到跟到select操作的入口 ...
- 学习笔记 | Github
Github教程 \(Github\)是个好东西QwQ 存代码不用U盘爸爸妈妈再也不用担心我的U盘弄丢没有备份啦! 创建github账号 创建仓库 输入命令 git clone https://git ...
- Geatpy遗传算法在曲线寻优上的初步探究
园子里关于遗传算法的教案不少,但基于geatpy框架的并未多见,故分享此文以作参考,还望广大园友多多指教! Geatpy出自三所名校联合团队之手,是遗传算法领域的权威框架(python),其效率之高. ...