git链接

作业描述

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)题解的更多相关文章

  1. 1001.A+B Format (20)代码自查(补足版)

    1001.A+B Format (20)代码自查(补足版) 谢谢畅畅酱的提醒,发现了代码中的不足,把变量名更改成更合理的名字,并且把注释也换成英文啦! 栋哥提供的代码自查的方式也帮助了我发现很多代码中 ...

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

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

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

  5. 关于‘1001.A+B Format (20)’的解题报告

    1001.A+B Format(20) 首先要感谢一下指导我github上传问题的小伙伴们,捣腾了一整天我终于摸到了一点门路,真的谢谢你们. 小豪的github 问题描述: Calculate a + ...

  6. "1001. A+B Format (20)" 解题报告

    Github : git@github.com:Circlecos/object-oriented.git PDF Of Markdown : "1001. A+B Format (20)& ...

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

  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. 1001. A+B Format (20) (%0nd)

    1001. A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Calculate ...

随机推荐

  1. Python 包、模块、函数、变量作用域

    Python 项目的组织结构 - 包 -- 模块 --- 类 ---- 函数.变量   Python是利用包和模块来组织一个项目的.   包: 包的物理表现是一个文件夹,但是一个文件夹却不一定是个包, ...

  2. uva 1590 - IP Networks(IP地址)

    习题4-5 IP网络(IP Networks, ACM/ICPC NEERC 2005, UVa1590) 可以用一个网络地址和一个子网掩码描述一个子网(即连续的IP地址范围).其中子网 掩码包含32 ...

  3. 关于postgresql触发器的总结(lab作业系列)

    上题: In this tutorial you will create a stored procedure and triggers to check a complex constraint. ...

  4. spring-data-jpa快速入门(一)——整合阿里Druid

    一.概述 官网:https://projects.spring.io/spring-data-jpa/ 1.什么是spring-data-jpa Spring Data JPA, part of th ...

  5. 20155307 2016-2017-2 《Java程序设计》第9周学习总结

    20155307 2016-2017-2 <Java程序设计>第9周学习总结 教材学习内容总结 JDBC(Java DataBase Connectivity)即java数据库连接,是一种 ...

  6. [Qt扒手2] PyQt5 路径绘画例子

    [说明] 此例扒自 Qt 官网,原例是 C++ 代码,我把它改写成了 Python + PyQt5 版本. 有了前一个例子的成功,这个例子改写的非常之快.记得第一个例子花了我几天的时间,而这个例子只花 ...

  7. virtualenvwrapper安装和常用指令(mac)

    安装: .安装(要有python环境+pip): * sudo pip install virtualenvwrapper .配置: 执行:vi ~/.bash_profile 在~/.bash_pr ...

  8. layer.msg(msg, time, parme)设置图标问题

    layer.msg只提到3个参数,实际上有第4个参数,第4个参数就是msg关闭后执行的回调.   layer.msg('提示', 2, 1, function(){})   第一个参数:提示 第二个参 ...

  9. Python科学计算库-Numpy

    NumPy 是 Python 语言的一个扩充程序库.支持高级大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库,也是学习 python 必学的一个库. 1. 读取文件 numpy.gen ...

  10. 在Visual Studio中使用.lib和.dll的环境搭建

    1 静态库和动态链接库的区别 动态链接库是在运行的时候被调用的,静态库在链接的时候被链接到最终生成的应用程序(.exe)中 静态库需要用到的文件 (.lib .h) 头文件(.h)提供接口,库文件(. ...