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. poj2513--并查集+欧拉路+字典树

    经典好题,自己不知道哪里错了交上去是RE,可能是数组开的不好吧,字典树老碰到这种问题.. 先马上别人的代码,有空对拍看看 #include <cstdio> #include <cs ...

  2. python http请求类

    # -*- coding: UTF-8 -*- # coding="utf-8" import httplib2 import json from urllib.parse imp ...

  3. Jmeter卡住解决方案

    windows环境下,修改jmeter.bat: set HEAP=-Xms256m -Xmx256m set NEW=-XX:NewSize=128m -XX:MaxNewSize=128m 改为: ...

  4. 正则 ?<= 和 ?= 用法,范例

    (exp) 匹配exp,并捕获文本到自动命名的组里(?<name>exp) 匹配exp,并捕获文本到名称为name的组里,也可以写成(?'name'exp)(?:exp) 匹配exp,不捕 ...

  5. 通过awk 和 sed 将多余的列剔除

    通过awk 和 sed 将多余的列剔除 名词注释: awk -F 指定分隔符 OFS 指定输出分隔符 sed sed "s/|/test/2" a.log 将第二个 | 线替换为 ...

  6. C# 把ABCD转换成数字

    每倒题得选项可能是多选或者单选. public static string LetterTransformationNum(string answer, int type) { string num ...

  7. Html中,id、name、class、type的区别

    <input type="text" name="name" id="name" class="txt"> ...

  8. C#异常断电后重新启动项目出现配置未初始化错误

    转到如截图中所示路径,将其下的数据删掉,就可以启动了.

  9. Git reset与checkout的区别

    reset: 将暂存区的文件回撤到工作区,文件内容不会有任何变化 checkout: 将工作区文件恢复到上一次commit时的内容,将会丢失修改了但未加入暂存区的内容

  10. maven安装和eclipse集成

    maven作为一个项目构建工具,在开发的过程中很受欢迎,可以帮助管理项目中的bao依赖问题,另外它的很多功能都极大的减少了开发的难度,下面来介绍maven的安装及与eclipse的集成. maven的 ...