随手之题(非水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;
}
四.运行正确截图

posted @ 2019-03-04 11:39  千倍于太阳的光辉  阅读(138)  评论(0编辑  收藏  举报