PAT-1001. A+B Format (20)

2014-11-24 01:07:51 · 作者: · 浏览: 2
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  
#include  
using namespace std;  
int main(){  
    long a,b,c;  
    int i,flag;  
    bool is_positive;  
    char result[9];  
  
    while(cin>>a>>b)  
    {  
        c = a+b;  
        if( c>0 )   
            is_positive = true;  
        else   
            is_positive = false;  
      
        flag = 0;         
        if(abs(c) >
= 1000){ c = abs(c); //取绝对值 for( i=0; c>0; i++){ result[i] = c%10 + '0'; //数字转换成字符 c = c/10; if(flag == 2 && c != 0 ) { i++; result[i] = ','; //每三个加一个逗号 flag = 0; } else flag++; } if( !is_positive ) //结果为负数时 { result[i] = '-'; cout<=0; i--){ cout<