GiHub

markdown PDF


问题描述

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的数据范围都比较小,所以使用了效率较低,但代码量少的做法

  1. 计算a+b,并转换为字符串;
  2. 检验是否带有负号;
  3. 逐位输出字符,在满足条件:剩余字符数为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 解题报告的更多相关文章

  1. PAT (Advanced Level) Practise 1004 解题报告

    GitHub markdownPDF 问题描述 解题思路 代码 提交记录 问题描述 Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 1600 ...

  2. PAT (Advanced Level) Practise 1002 解题报告

    GitHub markdownPDF 问题描述 解题思路 代码 提交记录 问题描述 A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 ...

  3. PAT (Advanced Level) Practise 1003 解题报告

    GitHub markdownPDF 问题描述 解题思路 代码 提交记录 问题描述 Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 第二周学习总结-Java

    2018年7月22日 暑假第二周马上就要结束了,这一周我继续学习了java. 本周学到了一些Java的修饰词,比如static.private.public等,这些修饰词用法与c++类似,很容易掌握. ...

  2. bat 直接编译vs项目

    直接项目.sln拖到bat上: @ECHO OFFset path=%~dp1%~nx1 C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.e ...

  3. FCN 项目部分代码学习

    下面代码由搭档注释,保存下来用作参考. github项目地址:https://github.com/shekkizh/FCN.tensorflowfrom __future__ import prin ...

  4. python的相关基本操作

    1.安装第三方库:pip install requests 2.升级:pip install --upgrade library_name 3.升级所有已安装的库: pip list --outdat ...

  5. servlet保存会话数据---利用隐藏域

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletExcep ...

  6. mac下安装Brew 警告:Warning: /usr/local/bin is not in your PATH.

    终端输入命令 export PATH=/usr/local/bin:$PATH

  7. Ubuntu16.04安装vim8

    Ubuntu16.04安装vim8 在Ubuntu16.04下编译安装vim8,并配置vim-plug插件管理器,以及安装YouCompleteMe等插件. 安装依赖 sudo apt-get ins ...

  8. Sql Server并发和事务

    锁的作用范围通常在事务中,事务是建立在并发模式下. 从SQL Server 2005开始,加入了一种新的并发模式-----乐观并发.不管使用哪种并发模式,如果多个会话同时修改相同的数据,都会产生资源争 ...

  9. [转] AES,SHA1,DES,RSA,MD5区别

    AES:更快,兼容设备,安全级别高: SHA1:公钥后处理回传 DES:本地数据,安全级别低 RSA:非对称加密,有公钥和私钥 MD5:防篡改 相关: 公开密钥加密(英语:public-key cry ...

  10. Spring MVC基础知识整理➣View与Controller数据交互

    概述 Spring MVC是由View—Controller—Model组成,其中View和Controller的数据交互,成为了关注的核心点.MVC中,我们将View中的数据传递到Controlle ...