随手之题(非水4)

第一次做英文题难免有些入坑,果然程序员对英语的要求比较高,话不多说,直接入题,这竟然是一个令人头皮发麻而又简单的a+b问题!
一.试题1001 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 −10
​6
​​ ≤a,b≤10
​6
​​ . 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
二.分析:1.这次的英文题我竟然还求助了百度翻译,怪自己不好意思的
2.尽管是只要一个a+b的简单方程式,但是根据题目要求需要将答案以三位切分法将答案输出,比如一万八千六十八这个数就需要:18,688该形式输出
3.开始不知道该用什么思路将这样形式的答案输出,后来考虑了一下利用if判断格式将其输出即可,不过这样的方法确实可行而高效!
三.说了这么多,还是附上执行代码更有说服力吧:
#include<bits/stdc++.h> //注意:本题中最好对运算出的结果使用绝对值,因为这样使用if情况分析时会更加方便,由于本处使用绝对值要用到数学库函数,但是本处开头使用c++万能头文件就又方便了许多
using namespace std;
int main(void)
{
int a,b,sum,c;
scanf("%d %d",&a,&b);
sum=a+b;
c=abs(sum);
if(c<1000)
printf("%d",sum);
if(c>=1000&&c<1000000)
printf("%d,%03d",sum/1000,c%1000);//由于需要使用三位数位切分将数输出,那么高位以后的前端还需要加上“03”,以表明三位输出的数
if(c>=1000000)
printf("%d,%03d,%03d",sum/1000000,(sum/1000)%1000,c%1000);//此处同上
return 0;
}
四.运行正确截图
1580454-20190304113857646-2121549576.png

转载于:https://www.cnblogs.com/wonzenkei/p/10469770.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值