码迷,mamicode.com
首页 > 其他好文 > 详细

1001 A+B Format

时间:2019-04-06 09:24:43      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:alc   group   mes   cout   form   ios   ted   a+b   tst   

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 }

 

 

1001 A+B Format

标签:alc   group   mes   cout   form   ios   ted   a+b   tst   

原文地址:https://www.cnblogs.com/xwl3109377858/p/10660646.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!