博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pat甲级题目1001 A+B Format详解
阅读量:7067 次
发布时间:2019-06-28

本文共 1131 字,大约阅读时间需要 3 分钟。

pat1001 A+B Format (20 分)

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 Specification:

Each input file contains one test case. Each case contains a pair of integers a and b where −. The numbers are separated by a space.

Output Specification:

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的和并从右往左每三位加一个","正序输出,大致思路为将计算的数转换为字符后,然后一位一位输出,同时判断何时该加“,”。             难点在于这个地方,存在以下两种情况:1、若整个a+b和的位数是3的倍数,则重点考虑如何判断最后一位之后不加“,”如“324,113”不能为“324,113,”。                                                  2、若整个a+b和的位数不是3的倍数,则重点考虑如何实现从右往左每三位加一个“,”。
关键代码:
1 (i+1)%3==strlen(s)%3&&i!=strlen(s)-1  //s为数字转换后的字符数组
完整代码:
1 #include
2 #include
3 int main() 4 { 5 int a,b,i; 6 char s[10]; 7 scanf("%d %d",&a,&b); 8 sprintf(s, "%d", a+b); //将数字转换为字符数组 9 for(i=0;i

 

 
 
 

转载于:https://www.cnblogs.com/ourpat/p/11100052.html

你可能感兴趣的文章
linux 防爆破方法
查看>>
2、通过ipmitool工具修改IPMI的WEB密码
查看>>
云盘关闭,教你用蒲公英搭建私有云
查看>>
Spring Cloud 入门教程5、服务容错监控:Hystrix Dashboard
查看>>
很好的学习平台
查看>>
hibernate学习笔记3
查看>>
SQL Server 2005 日常运维检查操作手册
查看>>
利用jquery和jsonp来获取跨站数据,并实现cookie共享
查看>>
我的友情链接
查看>>
写sql语句时将时间格式“20110725”转化为格式2012年07月25日
查看>>
[Hadoop in China 2011] 蒋建平:探秘基于Hadoop的华为共有云
查看>>
heartbeat高可用+lvsDR
查看>>
方丈被害子女有没有权利继承遗产?
查看>>
java入门第一季5、6
查看>>
[转载] 闻一多——七子之歌
查看>>
针对tomcat日志乱码问题
查看>>
免费的协作和协同办公软件平台onlyoffice轻松部署
查看>>
WiFi覆盖下的生活 享受便利的同时 别忘记了安全
查看>>
关于ios 8 7 下的模态窗口大小的控制 代碼+場景(mainstoryboard)( Resizing UIModalPresentationFormSheet )...
查看>>
Linux软件包的管理--YUM
查看>>