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. if 和case

    select case 'O' when 'O' then (1*5-6) when 'C' then (1*5-6) when 'EC' then (1*5-6) --else null --end ...

  2. why go

    取自:http://www.weixinyidu.com/n_3502636 Go的核心贡献者 Go主要有静态语言.天生并发.内置GC.安全性高.语法简单.交叉编译和编译快速这几个方面的特性.这些特性 ...

  3. 【JavaScript 从零开始】表达式和运算符(1)

    原始表达式 最简单的表达式是"原始表达式"(primary expression).JavaScript中的原始表达式包含常量或直接量.关键字和变量. // 常量或直接量 1.23 ...

  4. [HTML5] Canvas绘制简单形状

    使用canvas来进行绘画,它像很多其他dom对象一样,有很多属性和方法,操作这些方法,实现绘画 获取canvas对象,调用document.getElementById()方法 调用canvas对象 ...

  5. vue项目引入element

    前提工作-安装并配置好以下环境: 1.安装node  2.安装git 1.初始化项目 vue init webpack vue-elementui npm run dev 2.安装element np ...

  6. LeetCode Tries Prefix Tree

    class TrieNode { public: ; TrieNode* child[NR_FANOUT]; int count; // Initialize your data structure ...

  7. Oracle中的锁

    Oracle中的锁 锁是一种机制,多个事务同时访问一个数据库对象时,该机制可以实现对并发的控制 按照用户系统锁可以分为自动锁和显示锁. 自动锁(系统上锁):DML锁.DDL锁.systemlocks锁 ...

  8. 从项目中学习HTML+CSS

    最近由于工作原因以及自己的懈怠,已经很久都没有更新过博客了.通过这段时间,我发现坚持一件事情是真的很难,都说万事开头难,但是在放弃这件事上好像开头了后面就顺理成章的继续下去了.中间即使不怎么情愿也在努 ...

  9. Csv读写类

    <?php /** * CSV 文件读写类 * * 示例: $header = array('name' => '名字', 'age' =>'年龄', 'idcard' => ...

  10. Android:Building " " Gradle project info 问题

    Android Studio新建或者打开项目的时候,一直卡在Building "" Gradle project info 进度上不动,猜测是网络原因下载gradle不成功. 两种 ...