PAT 1001A+B Format
题目速览
1.解题的思路过程
- 认真读题,题目为A+BFormat,简单的计算a+b问题,特殊在于输出的形式。
- 输入形式为每个输入文件包含一个测试样例,每个测试样例仅包含一对整型a与b。
- 数字大小范围为-1000000到1000000,确定使用整型类型表示和。
- 输出较为特殊,结果必须使用标准形式输出,即结果为四位数及以上的必须从最后开始每三位数字前添加一个逗号,增强结果的可读性。
- 问题的代码实现
采用取余的方法从最后开始将三位三位取出来。
题目结果最大值为2,000,000最小值为-2000000,最多需要使用两个逗号。
我认为较简单的方法是采用分类,分为不需要逗号(-1000<sum<1000),需要一个逗号(-1,000,000<sum<1,000,000),和其他需要两个逗号。
考虑到有的三位是000,010等情况,取余后仅出现0,10等,printf使用%03d(前面自动补零)。
2.编码过程中调试
检测题目给出样例-1000000 9时出现bug,输出结果为-999,-991,意识到当为负数时,分次输出每次都为负数。我使用绝对值函数将后面的结果变为非负,如此便只留下最开头的负号,与输出相符。
3.上线测试(提交代码)后的bug发现与修改过程
代码
#include<stdio.h>
#include<math.h>
int main()
{
int a,b,sum;
scanf("%d%d",&a,&b);
sum=a+b;
if(sum<1000&&sum>-1000)
printf("%d",sum);
else if(sum<1000000&&sum>-1000000)
printf("%d,%03d",sum/1000%1000,abs(sum%1000));
else
printf("%d,%03d,%03d",sum/1000000%1000,abs(sum/1000%1000),abs(sum%1000));
return 0;
}
后来我看了其他同学的代码,得到了启发,当和为负数可以先取绝对值,打印出负号,这样代码更为简单。
#include<stdio.h>
int main()
{
int a,b,sum;
scanf("%d%d",&a,&b);
sum=a+b;
if(sum<0)
{
sum=-sum;
printf("-");
}
if(sum<1000)
printf("%d",sum);
else if(sum<1000000)
printf("%d,%03d",sum/1000%1000,sum%1000);
else
printf("%d,%03d,%03d",sum/1000000%1000,sum/1000%1000,sum%1000);
return 0;
}
在PAT上的提交记录列表截图
今天做了一题类似的输出三位分组问题,题目数据保证输入输出为整型。这次开了数组来存储数据,第一次没有考虑到0的输出导致错误。
while(result)
{
num[i++]=result%10;
result/=10;
}
for(j=i-1;j>=0;j--)
{
cout<<num[j];
if(j%3==0 && j!=0)
{
cout<<",";
}
}
PAT 1001A+B Format的更多相关文章
- 第二次作业 编程题 PAT 1001A+B Format
Github的object-oriented仓库:1001.A+BFormat(20) 1.解题的思路过程 在之前学习C语言时曾经碰到过类似的将数字转换成字符输出的情况,这道题目要求输出的数字每三个间 ...
- [PAT]A+B Format[简单]
1001 A+B Format (20)(20 分) Calculate a + b and output the sum in standard format -- that is, the dig ...
- 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甲级 1001. A+B Format (20)
题目原文: Calculate a + b and output the sum in standard format -- that is, the digits must be separated ...
- 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 1001 A+B Format
题目链接:传送门 题目简述: 1. 给定两个整数值a,b: 2.范围-1000000 <= a, b <= 1000000: 3.按指定格式输出结果 例:-100000 9 输出: -99 ...
- PAT甲级真题打卡:1001.A+B Format
题目: Calculate a + b and output the sum in standard format -- that is, the digits must be separated i ...
- PAT (Advanced Level) Practice 1001 A+B Format (20 分)
题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400 Calculate a+b and ...
- PAT甲级 1001 A+B Format
题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400 1001 A+B Format ( ...
随机推荐
- Docker run centos 中文乱码,时区不对 问题解决
开心得写代码,结果用Docker部署发现中文全是问号... 调了半天编码,最后发现不是代码得问题..坑爹.. dokcer 的 centos镜像不带中文,时区也不是中国,所以要自己设置.. #设置时区 ...
- Xcode8 log问题
去除一堆log的方法: Xcode8--->Product---- Edit Scheme... -> Run -> Arguments, 在Environment Variable ...
- [C#]简单离线注册码生成与验证
本文使用RSA非对称加密和Base64简单地实现离线注册码的生成与验证功能. 主要思路就是提供者持有密钥,通过RSA加密客户机标识或时间标识,再通过Base64加密成不太难看的注册码,然后分发给客户机 ...
- Centos系统下卸载、安装MySQL及用户的创建、授权和使用(详细。。。。)
由于经常使用linux系统,并且大数据环境搭建中经常会使用到mysql,不像windows系统下的安装,今天有点空写一篇,下面我给大家演示一遍. 主要有三部分内容: 1.MySQL的卸载 2.MySQ ...
- POJ3126(KB1-F BFS)
Prime Path Description The ministers of the cabinet were quite upset by the message from the Chief ...
- 可缺省的CSS布局——张鑫旭
一.技术不难.意识很难 有些东西的东西的实现,难的不是原料.技术:而是想不到,或者说意识不到. 例如下面这个简单而又神奇的魔术: 是吧.搞通了,才发现,哦~原来这么回事,很简单的嘛,我也可以实现的!其 ...
- 第二十六天- C/S架构 通信流程 socket
1.C/S架构 C/S架构:Client与Server ,中文意思:客户端与服务器端架构,这种架构也是从用户层面(也可是物理层面)来划分的.这里客户端一般指需先安装再执行的应用程序,对操作系统依赖性较 ...
- markdown 语法备忘
markdwon语法, 增加以下CSS代码,可以对markdwon语法产生的文件进行分页操作. <div style="page-break-after:always;"&g ...
- Python中操作HTTP请求的urllib模块详解
urllib 是 Python 标准库中用于网络请求的库.该库有四个模块,分别是urllib.request,urllib.error,urllib.parse,urllib.robotparser. ...
- CSS-带尖角的对话框
效果图: box1的代码: .box{ position: relative; width: 200px; height: 200px; border: 2px solid #000; backgro ...