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 ...
随机推荐
- 删除Oracle Online Redo 测试
删除Oracle Online Redo 测试 SQL> select * from v$log; GROUP# THREAD# SEQUENCE# BYTES BLOCKS ...
- office2010安装需MSXML版本6.10.1129.0详解解
https://blog.csdn.net/qq_40824474/article/details/82390606 office2010安装提示报错 由于下列原因,安装程序无法继续,需要计算机安装M ...
- 第一篇 深入嵌入式之Linux裸机
{ 个人心得: 嵌入式底层重要的是在CPU(各种架构)或SOC基础上,利用u-boot初始化系统,并启动OS,建立实时多任务环境.文件系统等,再根据功能要求设计上层程序:而对硬件的需有足够掌握. } ...
- 20155236范晨歌_MSF基础应用
20155236范晨歌_MSF基础应用 20155236范晨歌_MSF基础应用 目录 概述 MS08-067漏洞攻击 MS11-050漏洞攻击 MS10-087漏洞攻击 辅助模块 概述 MSF的六种模 ...
- 2991:2011 求2011^n的后四位。
2991:2011 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 已知长度最大为200位的正整数n,请求出2011^n的后四位. 输入 第一行为一个正整数k ...
- java日志框架log4j详细配置及与slf4j使用教程
一.log4j基本用法 首先,配置log4j的jar,maven工程配置以下依赖,非maven工程从maven仓库下载jar添加到“build path” 1 2 3 4 5 <dependen ...
- Macaca上手体验
在研究了一段时间Appium后,尝试对另一个框架做实验——Macaca,阿里的开源测试框架,该框架不只适合移动端,同样适用于web端,可谓是方便的很啊~ 同时支持js.java.python.封装好的 ...
- .net 控件生命周期
这里列举出来了11个生命周期,一般的控件生命周期会经历这11个生命周期,但是有一些特别的控件比如页面控件System.Web.UI.Page等. 具体代码参考如下: /// <summary&g ...
- Unity依赖注入(笔记)
一.介绍 控制反转(Inversion of Control,简称IoC):整洁架构思想,不允许内部获知外部的存在,这就导致了我们必须在内层定义与外层交互的接口,通过依赖注入的方式将外层实现注入到内部 ...
- Ubuntu18.04安装Python3.6.8
Ubuntu18.04预装了Python3.6.5 终于不再预装Python2.7了 但是系统预装的Python分散安装在各个目录里 以后改起来非常不方便 所以本次安装Python3.6.8 Pyth ...