Github 1001

题目速览


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

  1. 第二次作业 编程题 PAT 1001A+B Format

    Github的object-oriented仓库:1001.A+BFormat(20) 1.解题的思路过程 在之前学习C语言时曾经碰到过类似的将数字转换成字符输出的情况,这道题目要求输出的数字每三个间 ...

  2. [PAT]A+B Format[简单]

    1001 A+B Format (20)(20 分) Calculate a + b and output the sum in standard format -- that is, the dig ...

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

  4. PAT甲级 1001. A+B Format (20)

    题目原文: Calculate a + b and output the sum in standard format -- that is, the digits must be separated ...

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

  6. pat 1001 A+B Format

    题目链接:传送门 题目简述: 1. 给定两个整数值a,b: 2.范围-1000000 <= a, b <= 1000000: 3.按指定格式输出结果 例:-100000 9 输出: -99 ...

  7. PAT甲级真题打卡:1001.A+B Format

    题目: Calculate a + b and output the sum in standard format -- that is, the digits must be separated i ...

  8. PAT (Advanced Level) Practice 1001 A+B Format (20 分)

    题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400 Calculate a+b and ...

  9. PAT甲级 1001 A+B Format

    题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400 1001 A+B Format ( ...

随机推荐

  1. Linux 文件IO管理 - POSIX

    以下是对POSIX的简短解释: POSIX表示可移植操作系统接口(Portable Operating System Interface of UNIX,缩写为 POSIX ),POSIX标准定义了操 ...

  2. [转]javascript之Object.assign()痛点

    本文转自:http://blog.csdn.net/waiterwaiter/article/details/50267787 最近也一直会用javascript,然后中间使用的一些组件,如Echar ...

  3. vue权限路由实现方式总结二

    之前已经写过一篇关于vue权限路由实现方式总结的文章,经过一段时间的踩坑和总结,下面说说目前我认为比较"完美"的一种方案:菜单与路由完全由后端提供. 菜单与路由完全由后端返回 这种 ...

  4. Tuple解决在视图中通过razor获取控制器传递给视图的匿名对象的报错问题

    C#的编译器总是将匿名类型编译成internal的,当在视图中直接使用控制器传递的匿名对象时就会报错错误代码:控制器代码视图代码执行结果: ****************************** ...

  5. 封装framework注意点

    1.新建一个framework过程: . 2.在工程内新建一些类,注意,使用xib时初始化必须要加上loadnib:,否则会造成xib无效(可能是因为没有加载) 如下: JFViewControlle ...

  6. iOS--支付宝环境集成

    1.下载支付宝SDK以及Demo https://doc.open.alipay.com/doc2/detail?treeId=54&articleId=103419&docType= ...

  7. HDU 3501 Calculation 2------欧拉函数变形

    Calculation 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  8. JS实现继承的几种方式以及优缺点(转载)

    前言 JS作为面向对象的弱类型语言,继承也是其非常强大的特性之一.那么如何在JS中实现继承呢?让我们拭目以待. JS继承的实现方式 既然要实现继承,那么首先我们得有一个父类,代码如下: // 定义一个 ...

  9. C#设计模式--代理模式(学习Learning hard 设计模式笔记)

    class Program { static void Main(string[] args) { //创建一个代理对象 并发出请求 Person proxy = new Friend(); prox ...

  10. Linux学习8-Linux常用命令(4)

    链接命令     命令名称:ln 命令英文原意:link 命令所在路径:/bin/ln 执行权限:所有用户 功能描述:生成链接文件 语法:ln 选项[-s][原文件] [目标文件] 选项: -s 创建 ...