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;
    }


  • 相关阅读:
    html笔记
    Git入门学习总结
    使用OpenSSH远程管理Linux服务器
    Linux 网卡驱动的安装
    vi的使用
    Linux下常用的数据恢复工具
    网络文件系统(NFS)的使用
    文件系统管理
    磁盘存储管理
    用户权限管理
  • 原文地址:https://www.cnblogs.com/aboutblank/p/2354734.html
Copyright © 2011-2022 走看看