1001. A+B Format (20)
原题连接:https://www.patest.cn/contests/pat-a-practise/1001
题目如下:
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
______________________________________________________________________________________________________________________________________________________
这道题难点在于如何三位输出,我是利用数组存放每一个数,然后倒序输出,当下标为3的倍数的时候,加入“,”,但有好几个测试点没通过……代码如下:
#include<stdio.h>
#define Max 8 int main()
{
long a,b,sum;
int i,c[Max];
scanf("%d %d",&a, &b);
sum=a+b; if (sum<){printf("-");sum=-sum;}
i=;
c[i]=sum%;
sum/=;
while (sum>)
{
i++;
c[i]=sum%;
sum/=;
}
//printf("%d\n",i);
for (;i>=;i--)
{
printf("%d",c[i]);
if (i%==&&i!=)printf(",");
}
return ;
}
_________________________________________________希望大神能帮我看看我的错误在哪了……
在网上参考了其他人的解法,是直接对sum这个数选取,利用了技巧:取一个数sum(假设该数共有a位)的前n位的时候,用sum/10的(a-n)次方;取它的后n位的时候,用sum%10的n次方,然后就是分情况讨论,因为题中所给a,b是在区间【-1000000,1000000】,将|sum|分为大于1000000,小于1000000而大于1000,大于等于0小于1000的。
代码如下:
#include<stdio.h> int main()
{
int a,b,sum;
scanf("%d %d",&a,&b);
int i;
sum=a+b;
if (sum<){printf("-");sum=-sum;}
if (sum>=)printf("%d,%03d,%03d",sum/,(sum/)%,(sum%));
else if (sum>=)printf("%d,%03d",sum/,sum%);
else printf("%d",sum);
return ;
}
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 ...
- 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 ...
随机推荐
- HP网络打印机--如何添加打印机
HP网络打印机采用web服务形式,应添加打印机-通过Internet的打印机--填写网址http://192.168.1.10:80(从其他win7电脑-计算机-网络-网络设备中双击添加打印机,然后在 ...
- python 3.5: TypeError: a bytes-like object is required, not 'str'
出现该错误往往是通过open()函数打开文本文件时,使用了'rb'属性,如:fileHandle=open(filename,'rb'),则此时是通过二进制方式打开文件的,所以在后面处理时如果使用了s ...
- 用.net 发送邮件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...
- PHP两种基础的算法:冒泡、快速排序法》》》望能够帮助到大家
首先扯个淡@@@@@@@@@ 算法是程序的核心,一个程序的好坏关键是这个程序算法的优劣. 冒泡排序法 原理:在要排序的一组数中,对当前还未排好的序列,从前往后对相邻的两个数依次进行比较和调整,让较大的 ...
- dotnet Core Asp.net 项目搭建
Asp.Net Core 介绍 Asp.Net Core 目前最新版本 1.0.0-preview2-003131 Asp.Net Core官网:https://dotnet.github.io/ A ...
- linux中find批量删除空文件夹
空文件夹 列出用find 删除管道即可 find -type d -empty | xargs -n 1 rm -rf 注意最后不能rm -f,这样删不了目录,必须-r
- webpack初试
前言: 知道这完儿,没用过.关于webpack有很多介绍了,就不多说了.放几个链接,方便新手理解.这是给纯没用过的人了解的.这里只是简单介绍一下webpack的基本用法.大多内容都是来自webpack ...
- word使用笔记(1)
开始字母的格式也是宋体,只要全选后设置字体为Times New Roman即可,会自动跳过中文 新安的Mathtype字体有点奇怪,在样式里设置了一下,果然自定义为宋体了,改回Times New Ro ...
- 如何实现VoIP中大并发应用
后台服务器实现高并发方式: 说明: 黄色皆为运营商或第三方对接系统的VoIP设备等. 前置服务器A与B为热备容灾模式,当A异常,立即跳转到B. 应用服务器做实时容灾处理. 数据库做实时容灾处理. 媒体 ...
- 如何在Meteor中使用npm模块?
首先,请在AtmosphereJs上搜索有无相关的封装包.尽量采用已有的封装包,而不是自己封装. 有两种方法在项目中使用来自npm的模块. 封装为Meteor包并在项目中添加包.使用meteor cr ...