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 ...
随机推荐
- TCP\UDP客户—服务器程序设计基本框架流程图
- Java-数组队列
Java-数组队列 1.为何要创建一个数组队列? 数组的优点和缺点: 优点:访问速度是所有数据结构中最快的一种. 缺点:大小固定,如果要存储的数据个数不确定的时候? 数组空间不够,导致越界异 ...
- UML类图6种主要关系区别和联系
UML类图关系图示,因为长得都很类似,所以大家总会混淆,本文主要目的就是分析一下6种主要的关系,找到联系与区别,便于记忆. 6种主要的关系如图1所示.继承与实现.组合与聚合.关联与依赖可分别划分为一组 ...
- 使用SSH命令从一台Linux远程登陆到另一台Linux
命令格式: ssh 用户名@IP 示例: ssh root@192.168.1.10 回车,然后根据提示输入登陆密码即可.
- 在Mac检查安装的.net core 版本
在 Termianl which dotnet #查看dotnetcore的runtime路径 dotnet #dotnet 命令行工具 -h 查看帮助 #常用命令 # --info Display ...
- MongoDB DBA 实践1-----Windows
一.先决条件 1.支持的平台 在3.4版中更改: MongoDB不再支持32位x86平台. MongoDB需要x86-64架构并支持以下内容: Windows 7 / Server 2008 R2 W ...
- mybatis第二天_拓展——与spring整合以及逆向工程
一.整合思路 1.SqlSessionFactory对象应该放到spring容器中作为单例存在. 2.传统dao的开发方式中,应该从spring容器中获得sqlsession对象. 3.Mapper代 ...
- Oracle单节点_Grid_Infrastructure_DB_安装过程图解(三/三)
接上文: Oracle单节点_Grid_Infrastructure_DB_安装过程图解(二/三)
- 字符串Hash/树Hash学习笔记
哈希 Tags:字符串 作业部落 评论地址 一.概述 百度百科: 散列表(Hash table/哈希表),是根据关键码值(Key value)而直接进行访问的数据结构. 哈希表常用于比较两个字符串是否 ...
- 2-5 re模块练习题
1 练习: 1.验证手机号是否合法 2.验证邮箱是否合法 3.开发一个简单的python计算器,实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2 ...