乐逍遥xwl

导航

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相加之后,调整格式再输出的问题,用string比较好处理,可以考虑每三个数为一个分界,注意判断边界......
 
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<map>
 6 using namespace std;
 7 int main()
 8 {
 9     int a,b,sum;
10     cin>>a>>b;
11     sum=a+b;
12     string str;
13     str=to_string(sum);
14     for(int i=0;i<str.size();i++)
15     {
16         if((str.size()-i-1)%3==0&&str[i]!='-'&&i!=str.size()-1)
17             cout<<str[i]<<",";
18         else
19             cout<<str[i];
20     }
21     return 0;
22 }

 

 

posted on 2019-04-06 01:29  乐逍遥xwl  阅读(201)  评论(0编辑  收藏  举报