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 ...
随机推荐
- 关于settimeout 和for循环
for(var i=0;i<3;i++){ setTimeOut(function(){ console.log(i) },500) }; 执行结果:3,3,3 ---------------- ...
- Microsoft ACE OLEDB 12.0 数据库连接字符串
Excel 97-2003 Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myOldExcelFile.xls;Extended ...
- 如何在一台服务器上安装两个mysql或者更多
如何在一台服务器上安装两个mysql 1 前言 上篇写了在一台机器上源码编译安装一个mysql,那么如何在一台机器上源码编译安装两个mysql或者更多呢? 2 环境 mysql ...
- web socket (记录下来方便观看)
Web Sockets HTML5 WebSocket 设计出来的目的就是要取代轮询和 Comet 技术,使客户端浏览器具备像 C/S 架构下桌面系统的实时通讯能力. 浏览器通过 JavaScript ...
- json的注意事项
原文摘自:http://www.cnblogs.com/xcxc/p/3729207.html 在PHP语言中使用JSON和将json还原成数组 PHP原生提供json_encode()和json ...
- LightGBM中GBDT的实现
现在LightGBM开源了,这里将之前的一个文档发布出来供大家参考,帮助更快理解LightGBM的实现,整体思路应该是类似的. LightGBM优雅,快速,效果好,希望LightGBM越来越好:) L ...
- MongoDB 分页查询的方法及性能
最近有点忙,本来有好多东西可以总结,Redis系列其实还应该有四.五.六...不过<Redis in Action>还没读完,等读完再来总结,不然太水,对不起读者. 自从上次Redis之后 ...
- Linux下设置Mysql数据库编码
重装系统后,要装个mysql服务器,发现添加数据时,一直错误,查其原因,原来时编码问题.Google了一下,可都不太一样,可能时数据库版本问题吧. 查看数据库版本: select version(); ...
- SVN基本指令
Svn使用: Versions: Cornerstone: 如果命令行不出来,可以设置commonsline Tools 命令行:svn heip 获取服务器端数据 svn checkout 远端ur ...
- 如何找出标有"App Store 精华","Essentials"的所有软件?
如何找出标有"App Store 精华","Essentials"的所有软件? 中国区: +"App Store 精华" site:http ...