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


  • 相关阅读:
    474 Ones and Zeroes 一和零
    473 Matchsticks to Square 火柴拼正方形
    472 Concatenated Words 连接的单词
    Django 视图系统
    Django 路由系统
    Django 框架基础
    jQuery
    JavaScript- BOM, DOM
    CSS概念,引入,选择器
    HTML
  • 原文地址:https://www.cnblogs.com/aboutblank/p/2354734.html
Copyright © 2011-2022 走看看