1001. A+B Format (20)题解
作业描述
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的和并以千分位的形式输出。我首先注意到a,b的取值范围为-1000000 <= a, b <= 1000000,可以对a,b和sum的大小进行分类讨论,但是考虑到这个解法对a,b的取值有较大的限制,我选择了新的思路,就是将sum每个位的数字转化为字符型存储在一个数组中,再每隔三位插入一个逗号。
第一次代码
#include<stdio.h>
#include<math.h>
int main()
{
int a,b,sum,c,i,j,count=0;
char s[9];
scanf("%d%d",&a,&b);
sum=a+b;
c=abs(sum);
if(c<1000)
printf("%d",sum);
else
{
for(i=0;i<=9;i++)
{
s[i]=sum%10+48;
sum=sum/10;
count++;
if(sum==0)break;
if(count%3==0)
s[++i]=',';
}
for(;i>=0;i--)
printf("%c",s[i]);
}
return 0;
}
写完代码后提交的结果如下,出现了一些三个答案错误,那应该是有些细节没有考虑到,在审视了题目和代码后发现没有考虑到sum为负数的情况,于是对这种情况进行了补充。
修改后的代码
#include<stdio.h>
#include<math.h>
int main()
{
int a,b,sum,c,i,j,count=0;
char s[9];
scanf("%d%d",&a,&b);
sum=a+b;
c=abs(sum);
if(c<1000)
printf("%d",sum);
else
{
for(i=0;i<=9;i++)
{
s[i]=sum%10+48;
sum=sum/10;
count++;
if(sum==0)break;
if(count%3==0)
s[++i]=',';
}
for(;i>=0;i--)
printf("%c",s[i]);
}
return 0;
}
提交之后全部正确
做题中遇到的问题
这次代码题中遇到的主要问题就是如何将整数转化为字符,后来通过百度知道可以通过+'0'或者+48完成,不过原理没搞懂。
1001. A+B Format (20)题解的更多相关文章
- 1001.A+B Format (20)代码自查(补足版)
1001.A+B Format (20)代码自查(补足版) 谢谢畅畅酱的提醒,发现了代码中的不足,把变量名更改成更合理的名字,并且把注释也换成英文啦! 栋哥提供的代码自查的方式也帮助了我发现很多代码中 ...
- 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) 2016-09-09 22:47 25人阅读 评论(0) 收藏
1001. A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Calculate ...
- 关于‘1001.A+B Format (20)’的解题报告
1001.A+B Format(20) 首先要感谢一下指导我github上传问题的小伙伴们,捣腾了一整天我终于摸到了一点门路,真的谢谢你们. 小豪的github 问题描述: Calculate a + ...
- "1001. A+B Format (20)" 解题报告
Github : git@github.com:Circlecos/object-oriented.git PDF Of Markdown : "1001. A+B Format (20)& ...
- 【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-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 ...
- 1001. A+B Format (20) (%0nd)
1001. A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Calculate ...
随机推荐
- C++STL之unordered_map与QT的QHash对比
刚刚心血来潮,试一试QT和STL哪个好 网上评论都支持STL,我试了试: 贴上代码: #include <QCoreApplication> #include <QHash> ...
- docker 简要学习
一.Docker的安装和启动 使用环境centos7 yum包更新到最新 sudo yum update 安装需要的软件包,yum-util提供yum-config-manager功能 sudo yu ...
- HTML:5meta标签
<h2>一些常用的移动端的meta属性设置</h2><!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 --> & ...
- QuestaSim 中文注释乱码
在QuestaSim按如下顺序打开对应窗口, Tools -> Edit Preferences -> By Name -> Find 输入 encoding搜索对应项,将其valu ...
- re 模块错误 error: bad character range
下午,看到堆栈的内容.于是上机实验了一番 >>> bds = '10+6/5-4*2' # 数学运算表达式 想用 findall 把运算符号提取出来 >>> imp ...
- 2-7 hash
1.Hash Hash函数处理流程Hash,一般翻译做"散列",也有直接音译为"哈希"的,就是把任意长度的输入(又叫做预映射, pre-image),通过散列算 ...
- 20-[JavaScript]-BOM
1.jsBom简介 jsBom = javascript browser object modelBOM指的是浏览器对象模型 Browser Object Model,它的核心就是浏览器. 2.Bom ...
- CF 1041 F. Ray in the tube
F. Ray in the tube 链接 题意: 有两条平行于x轴的直线A,B,每条直线上的某些位置有传感器.你需要确定A,B轴上任意两个整点位置$x_a$,$x_b$,使得一条光线沿$x_a→x_ ...
- 洛谷NOIp热身赛题解
洛谷NOIp热身赛题解 A 最大差值 简单树状数组,维护区间和.区间平方和,方差按照给的公式算就行了 #include<bits/stdc++.h> #define il inline # ...
- JDBC连接数据库及其执行操作
作者:Alvin 功能:数据库连接与实现增删改查 时间:2019年3月4日08点33分 参考文章:https://www.2cto.com/database/201805/743741.html 一. ...