前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >A+B Format (20)

A+B Format (20)

作者头像
week
发布2018-08-27 09:21:48
2760
发布2018-08-27 09:21:48
举报
文章被收录于专栏:用户画像

一、题目要求

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

Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.

Output

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

代码语言:javascript
复制
-1000000 9

Sample Output

代码语言:javascript
复制
-999,991
代码语言:javascript
复制
2、代码
代码语言:javascript
复制
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
  string s;
  ostringstream os;
  long a,b,sum;
  int len;
  cin>>a;
  cin>>b;
  sum=a+b;
  os<<sum;
  s=os.str();
  len= s.length();
  if(sum<0)
  {
	len--;
	s=s.substr(1,len);
  }
  for(int index =len-3; index > 0; index -= 3){
     s.insert(index, ",");//插入逗号
  }
  if(sum<0)
  {
     s="-"+s;
  }
 
  cout<<s<<endl;
  return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016年02月03日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档