zoukankan      html  css  js  c++  java
  • 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

    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

    -1000000 9
    

    Sample Output

    -999,991
    
    
    #include <iostream>
    #include<string>
    #include <algorithm>
    #include <vector>
    #include <stdio.h>
    using namespace std;
    int main(void)
    {
    vector<string> myvector;
    vector<string>::iterator it;
    int a,b,i=1,flag=0;
    int sum[9];
    char temp[2];
    cin>>a>>b;
    int s=a+b;
    if(s==0)
    {
    cout<<"0";
    return 0;
    }
    if(s<0)
    {
    s=-s;
    flag=1;
    }
    for(; s!=0; s/=10)
    {
    sum[i]=s%10;
    i++;
    }
    int k=0;
    if(flag==1)cout<<"-";
    for(int j=1; j<i; ++j)
    {
    // itoa(sum[j],temp,10);两种方式integer转string
    sprintf(temp,"%d",sum[j]);
    myvector.push_back(temp);
    k++;
    if(k==3&&j<i-1)
    {
    myvector.push_back(",");
    k=0;
    }
    }

    reverse(myvector.begin(),myvector.end());
    for(it=myvector.begin(); it!=myvector.end(); ++it)
    cout<<*it;
    }


  • 相关阅读:
    angular模板
    Growth: 全栈增长工程师指南
    全栈增长工程师实战
    vue 快速搭建项目 iview
    ng-style
    教程视频链接
    内置对象
    对象—封装、继承
    对象—构造函数
    函数-理论
  • 原文地址:https://www.cnblogs.com/aboutblank/p/2354734.html
Copyright © 2011-2022 走看看