1001. A+B Format (20)

時(shí)間限制
400 ms
內(nèi)存限制
65536 kB
代碼長(zhǎng)度限制
16000 B
判題程序
Standard
作者
CHEN, Yue

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

AC代碼:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main(int argc, const char * argv[]) {
    int a, b, sum;
    cin >> a >> b;
    sum = a + b;
    
    string str;
    str = to_string(sum);
    if(str[0] == '-') {
        cout << '-';
        str.erase(0,1);
    }
    reverse(str.begin(), str.end() );
    int len = (int)str.size();
    for(int i = 3; i < len; i += 3) {
        str.insert(i, ",");
        i++;
        len++;
    }
    reverse(str.begin(), str.end() );
    cout << str << endl;
    return 0;
}

string字符串操作:

  • erase
    原型:
    (1)string& erase ( size_t pos = 0, size_t n = npos );
    (2)iterator erase ( iterator position );
    (3)iterator erase ( iterator first, iterator last );
    用法:
    (1)erase(pos,n); 刪除從pos開始的n個(gè)字符,比如erase(0,1)就是刪除第一個(gè)字符
    (2)erase(position);刪除position處的一個(gè)字符(position是個(gè)string類型的迭代器)
    (3)erase(first,last);刪除從first到last之間的字符(first和last都是迭代器)

  • insert
    原型:
    string &insert(int p0, const char *s);
    string &insert(int p0, const char *s, int n);
    string &insert(int p0,const string &s);
    string &insert(int p0,const string &s, int pos, int n);
    //前4個(gè)函數(shù)在p0位置插入字符串s中pos開始的前n個(gè)字符
    string &insert(int p0, int n, char c);//此函數(shù)在p0處插入n個(gè)字符c
    iterator insert(iterator it, char c);//在it處插入字符c,返回插入后迭代器的位置
    void insert(iterator it, const_iterator first, const_iterator last);//在it處插入[first,last)之間的字符
    void insert(iterator it, int n, char c);//在it處插入n個(gè)字符c

  • to_string函數(shù) C++11 新增方法
    string to_string (int val);
    string to_string (long val);
    string to_string (long long val);
    string to_string (unsigned val);
    string to_string (unsigned long val);
    string to_string (unsigned long long val);
    string to_string (float val);
    string to_string (double val);
    string to_string (long double val)

  • reverse函數(shù)
    數(shù)組轉(zhuǎn)置 reverse(v.begin(),v.end())

提供另一種解法

//
//  main.cpp
//  pat
//
//  Created by yaojies on 16/8/16.
//  Copyright ? 2016年 yaojies. All rights reserved.
//

#include <iostream>
#include <iomanip>
using namespace std;

int main(int argc, const char * argv[]) {
    int numA, numB, result;
    void functionOfPrint(int result);
    cin >> numA >> numB;
    result = numA + numB;
    //print "-" if result < 0
    if(result<0){
        cout << "-";
        result = -result;
    }
    functionOfPrint(result);
    cout << endl;
    return 0;
}
void functionOfPrint(int result){
    if(result>999){
        functionOfPrint(result/1000);
        cout << ",";
        cout << setfill('0')<<setw(3) << result%1000;
    }else{
        cout << result;
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容