pat甲级题目1001 A+B Format详解
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的倍数,则重点考虑如何实现从右往左每三位加一个“,”。
关键代码:
(i+)%==strlen(s)%&&i!=strlen(s)- //s为数字转换后的字符数组
完整代码:
#include<stdio.h>
#include<string.h>
int main()
{
int a,b,i;
char s[];
scanf("%d %d",&a,&b);
sprintf(s, "%d", a+b); //将数字转换为字符数组
for(i=;i<strlen(s);i++){
printf("%c",s[i]);
if(s[i]=='-') continue; //如果为"-"号继续循环不执行以下部分
if((i+)%==strlen(s)%&&i!=strlen(s)-) //要保证上述所说的情况都要满足
printf(",");
}
}
pat甲级题目1001 A+B Format详解的更多相关文章
- 【PAT甲级】1001 A+B Format (20 分)
题意:给两个整数a,b,计算a+b的值并每三位用逗号隔开输出(−1e6≤a,b≤1e6) AAAAAccepted code: #include<bits/stdc++.h> us ...
- 【转】Java魔法堂:String.format详解
Java魔法堂:String.format详解 目录 一.前言 二.重载方法 三.占位符 四.对字符.字符串进行格式化 五.对整数进行格式化 六. ...
- 【转】declare-styleable的使用(自定义控件) 以及declare-styleable中format详解
原文网址:http://www.cnblogs.com/622698abc/p/3348692.html declare-styleable是给自定义控件添加自定义属性用的 1.首先,先写attrs. ...
- PAT甲级题目1-10(C++)
1001 A+B Format(20分) Calculate a+b and output the sum in standard format -- that is, the digits must ...
- PAT甲级练习题1001、1002
1001 A+B Format (20 分) Calculate a+b and output the sum in standard format -- that is, the digits ...
- 自定义控件的自定义的属性attrs.xml下的declare-styleable中format详解
最近在摸索自定义控件,查找到一些自定义属性的一些资料,解决转载记载下来:看了此详解才方便理解! 我们在做项目的时候,由于android自带的属性不能满足需求,android提供了自定义属性的方法,其中 ...
- VC++ CTime Format 详解
参考链接: VC++中CTime类Format参数详解 CTime/COleDateTime::Format方法的使用 http://stat.ethz.ch/R-manual/R-devel/lib ...
- 二分算法题目训练(一)——Shell Pyramid详解
HDU2446——Shell Pyramid 详解 Shell Pyramid 题目描述(Google 翻译的) 在17世纪,由于雷鸣般的喧嚣,浓烟和炽热的火焰,海上的战斗与现代战争一样.但那时,大炮 ...
- 二分算法题目训练(四)——Robin Hood详解
codeforces672D——Robin Hood详解 Robin Hood 问题描述(google翻译) 我们都知道罗宾汉令人印象深刻的故事.罗宾汉利用他的射箭技巧和他的智慧从富人那里偷钱,然后把 ...
随机推荐
- Codeforces #576 Rectangle Painting 1 | div1D | div2F | DP | Rustlang
原题链接 大意 n*n正方形 有黑有白 每次可以选择一个 矩形把它全变成白色,代价是max(长,宽) 求吧 整个正方形 全变白 的最小代价 数据范围 n <= 50 题解 首先如果 我们刷了两个 ...
- Scrapy框架: 通用爬虫之CrawlSpider
步骤01: 创建爬虫项目 scrapy startproject quotes 步骤02: 创建爬虫模版 scrapy genspider -t quotes quotes.toscrape.com ...
- Scala中下划线的总结
1. 方法转化为函数 2. 集合中的每一个元素 3. 获取元组Tuple中的元素 4. 模式匹配 5. 队列 6. 导包引入的时候 7. 初始化变量 引用自:https://blog.csdn.net ...
- 高精度求A*B(FFT)
A * B Problem Plus 链接:http://acm.hdu.edu.cn/showproblem.php?pid=1402 Time Limit: 2000/1000 MS (Java/ ...
- Spring学习笔记(12)——aop
先了解AOP的相关术语:1.通知(Advice):通知定义了切面是什么以及何时使用.描述了切面要完成的工作和何时需要执行这个工作.2.连接点(Joinpoint):程序能够应用通知的一个"时 ...
- linux下有趣的工具
1.toilet(在CentoOS7 安装) yum install -y https://raw.githubusercontent.com/sliqua-hosting/repo/master/c ...
- sqlldr - exit error code 2 in unix (merged)
http://www.orafaq.com/forum/t/146582/ Thank you for your reply. Load has been successful all the tim ...
- 403 ,502 到正确的nginx 配置
配置完一定要reboot ,之前我一直用的 ./nginx -s reload ,这次我不知道为啥不行... 再没有reboot 之前一直在用的旧的配置.所以一直在报403forbbdin. rebo ...
- 77th ploty 制图神器
https://blog.csdn.net/u012897374/article/details/77857980
- react 使用axios
1.配置axios代理 使得axios 可以不写根路径 package.json "proxy":"http://localhost:4000", 2.使用 ...