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 ...
随机推荐
- 断路器Ribbon
断路器:就是对服务访问不到的情况做出自己的错误,也就是故障转移(将当前出现故障的请求重新返回特定消息) 改造消费者项目(RibbonDemo) 1.在pom.xml中引入hystrix的jar包 &l ...
- noip 初赛复习重点知识点
一.进制转化 将k进制数转化为十进制数: 设k进制数为(abcd)k,则对应十进制数为 (小数同理,乘k的负幂次) 将十进制数转成k进制数: 设十进制数为x: t1=x/k,t2=x mod k t1 ...
- python+selenium十五:CSS与Jquery
在css中,id用#表示,class用.表示,要定位标签直接写标签名,其他属性就用[xxx='xxx'] 一.css定位 1.属性定位:可以通过任意属性定位,不局限于id.class.name.tag ...
- windows 7 下用git
参考:http://my.oschina.net/longxuu/blog/141699
- idea首次创建新模块的详细操作
依赖网址:https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api/3.1.0 https://mvnrepository. ...
- 添加依赖:https://mvnrepository.com/
该网站搜索
- 解决Django + DRF:403 FORBIDDEN:CSRF令牌丢失或不正确,{"detail":"CSRF Failed: CSRF cookie not set."}
我有一个Android客户端应用程序尝试使用Django + DRF后端进行身份验证.但是,当我尝试登录时,我收到以下响应: 403: CSRF Failed: CSRF token missing ...
- DapperHelper 帮助类
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using ...
- MySql中 delimiter 详解
转载于:http://blog.csdn.net/yuxin6866/article/details/52722913 其实就是告诉MySQL解释器,该段命令是否已经结束了,mysql是否可以执行了. ...
- C# 之 数字格式化
格式规范的完整形式:{index [,width][:formatstring]} index是此格式程序引用的格式字符串之后的参数,从零开始计数:width(可选) 是要设置格式的字段的宽度,wid ...