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. block本质探寻一之内存结构

    一.代码——命令行模式 //main.m #import <Foundation/Foundation.h> struct __block_impl { void *isa; int Fl ...

  2. PHP/Laravel轻松上传超大文件

    我们知道,在以前,文件上传采用的是直接传整个文件的方式,这种方式对付一些小文件是没有问题的.而当需要上传大文件时,此种方式不仅操作繁琐,需要修改web服务器和后端语言的配置,而且会大量占用服务器的内存 ...

  3. 查看git提交细节

    git log git show fdf39277f54dd0484a9fefc012463924544e07af

  4. python基础学习1-第一个网络爬虫程序

    #!/usr/bin/env python # -*- coding:utf-8 -*- 煎蛋网抓妹子图 import urllib.request import os import random d ...

  5. CF 1114 D. Flood Fill

    D. Flood Fill 链接 题意: 一个颜色序列,每个位置有一个颜色,选择一个起始位置,每次可以改变包含这个位置的颜色段,将这个颜色段修改为任意一个颜色, 问最少操作多少次.n<=5000 ...

  6. 编译、安装rdesktop 1.8.3

    来自:https://blog.csdn.net/songshimvp1/article/details/48290885 1.安装GCC: 安装C/C++编译器apt-get install gcc ...

  7. UWP 五星评价(不跳转到龟速商店)

    之前写过一篇文章  UWP 五星好评  代码如下 var pfn = Package.Current.Id.FamilyName; await Launcher.LaunchUriAsync(new ...

  8. 一个针对string的较好的散列算发djb2

    var djb2HashCode = function(key) { var hash = 5831; for(var i = 0; i < key.length; i++) { hash = ...

  9. react.js插件开发,x-dailog弹窗浮层组件

    react.js插件开发,x-dailog弹窗浮层组件 我认为,每一个组件都应该有他自带的样式和属性事件回调配置.所以我会给x-dialog默认一套简单的样式,和各种默认的配置项.所有react插件示 ...

  10. Shader Variants 打包遇到的问题

    1. 遇到的问题 最常见的是打包到手机后效果与PC上不一致,具体情况比如: 光照贴图失效 雾失效 透明或者cutoff失效 以上首先需要检查的地方是Shader变体的编译设置 2. 超级着色器编译成N ...