PAT (Advanced Level) Practise 1001 解题报告
问题描述
A+B Format (20)
时间限制 400 ms
内存限制 65536 kB
代码长度限制 16000 B
判题程序 Standard
作者 CHEN, Yue
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
大意是:
计算a+b,并对结果添加千位分隔符(-1000000< a,b< 1000000)。
解题思路
题目中a,b的数据范围都比较小,所以使用了效率较低,但代码量少的做法
- 计算a+b,并转换为字符串;
- 检验是否带有负号;
- 逐位输出字符,在满足条件:剩余字符数为3的倍数 的位置输出分隔符。
代码
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{
int i,j,k,n,m,s,t,a,b;
char c[10];
cin>>a>>b;
s=a+b;
if (s<0) sprintf(c,"%d",-s);
else sprintf(c,"%d",s);
if (s<0) cout<<"-";
n=strlen(c);
for (i=0;i<n;i++)
{
cout<<c[i];
if (((n-i-1)%3==0)&&(i<n-1)) cout<<",";
}
return 0;
}
提交记录

之后会继续更新自查后的代码。。
PAT (Advanced Level) Practise 1001 解题报告的更多相关文章
- PAT (Advanced Level) Practise 1004 解题报告
GitHub markdownPDF 问题描述 解题思路 代码 提交记录 问题描述 Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 1600 ...
- PAT (Advanced Level) Practise 1002 解题报告
GitHub markdownPDF 问题描述 解题思路 代码 提交记录 问题描述 A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 ...
- PAT (Advanced Level) Practise 1003 解题报告
GitHub markdownPDF 问题描述 解题思路 代码 提交记录 问题描述 Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题 ...
- 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 Level) Practise:1001. A+B Format
[题目链接] Calculate a + b and output the sum in standard format -- that is, the digits must be separate ...
- PAT (Advanced Level) Practise - 1094. The Largest Generation (25)
http://www.patest.cn/contests/pat-a-practise/1094 A family hierarchy is usually presented by a pedig ...
- 1079. Total Sales of Supply Chain (25)【树+搜索】——PAT (Advanced Level) Practise
题目信息 1079. Total Sales of Supply Chain (25) 时间限制250 ms 内存限制65536 kB 代码长度限制16000 B A supply chain is ...
- PAT-PAT (Advanced Level) Practise 1001. A+B Format (20) 【二星级】
题目链接:http://www.patest.cn/contests/pat-a-practise/1001 题面: 1001. A+B Format (20) Calculate a + b and ...
- 1076. Forwards on Weibo (30)【树+搜索】——PAT (Advanced Level) Practise
题目信息 1076. Forwards on Weibo (30) 时间限制3000 ms 内存限制65536 kB 代码长度限制16000 B Weibo is known as the Chine ...
随机推荐
- java多线程快速入门(二十二)
线程池的好处: 避免我们过多的去new线程,new是占资源的(GC主要堆内存) 提高效率 避免浪费资源 提高响应速度 作用:会把之前执行某个线程完毕的线程不会释放掉会留到线程池中给下一个调用的线程直接 ...
- SpringData使用与整合
SpringData 整合源码:链接: https://pan.baidu.com/s/1_dDEEJoqaBTfXs2ZWsvKvA 提取码: cp6s(jar包自行寻找) author:Simpl ...
- 【linux】shell代码,获取当前路径,创建文件夹
#!/bin/bash CURRENT_PATH=`` cd $CURRENT_PATH MY_LOG=/var/log MY_DB=/var/lib/db [ ! -d $MY_LOG ] & ...
- Ubuntu shutdown now 关机后 开机黑屏
一重装gdm3 失败 sudo apt-get remove --purge nvidia-* # 卸载nvidia相关组件 sudo apt purge gdm gdm3 # 卸载gdm和 ...
- python爬虫快递查询系统(源码)
import requestsimport json def get_express_type(postid): '''根据快递单号来智能判断快递类型''' url = 'http://www.kua ...
- ecilpse运行Servlet程序是找不到路径的原因
当工作空间路径有空格时,空格会被转成%20,将导致路径无法识别,于是就找不到路径了.
- Nginx 提示host not found in upstream 错误解决方法
Nginx DNS resolver配置实例,本文讲解在proxy_pass 和 upstream server 通信的时候需要手动指定 resolver,本文就给出了配置实例. nginx 通过 ...
- IDEA导入JAR的源代码
- Html列表分页算法
public class PageHelper { /// <summary> /// 标签 /// </summary> public string Tag { get; s ...
- CAS5.3.X 配置备忘
## # 普通MD5用户jdbc验证 ## #配置数据库连接 cas.authn.jdbc.query[0].driverClass=com.mysql.cj.jdbc.Driver cas.auth ...