1001. 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 <vector>


using namespace std;

int main()
{
    vector<char> s;
    long int a,b,sum;
    cin>>a>>b;
    sum = a+b;
    if(sum<0){
        cout<<"-";
        sum=-sum;
    }
    else if(sum==0){
        cout<<0;
    }
    int x=1;
    int count1 = 1;
    while(sum!=0){
        x=sum%10;
        if(sum!=0&&count1%4)
        s.push_back(x+48);
        else if(sum!=0&&count1%4==0){
            s.push_back(',');
            s.push_back(x+48);
            count1++;
        }
        count1++;
        sum=sum/10;
    }
    int n=s.size();
    for(int i=n-1;i>=0;i--){
        cout<<s[i];
    }
}

 

1.刚开始思路是存整形数组然后再考虑输出格式,发现做起来比较麻烦

2.后来想到可以直接存字符数组,然后边存边把,放进去以便输出

3.第一次出现问题是在存‘,’的时候忘记把这一次计算的x也存入数组,导致数组中少保存了数字

4.第二次出现问题是count1%4==0的时候忘记把count1再加一个1,如果不加的话导致count1和字符数组中的下标不能对应,第二个及第二个以后的逗号会出现混乱

5.第三次出现问题是之前一直用的x==0判断是否结束!简直蠢!

6.第四次出现问题是sum=sum/10放的位置太靠前,导致最后一次sum=0的时候存入数组中的数据发生错误。

 

posted @ 2016-11-09 17:01  Mel年  阅读(306)  评论(0编辑  收藏  举报