zoukankan      html  css  js  c++  java
  • 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的时候存入数组中的数据发生错误。

  • 相关阅读:
    HDU 1878 欧拉回路(DFS)
    HDU 2181 哈密顿绕行世界问题(DFS)
    HDU 1181 变形课(DFS)
    HDU 1029 Ignatius and the Princess IV(map应用)
    HDU 1242 Rescue(BFS)
    HDU 1027 Ignatius and the Princess II(STL)
    将项目部署到本地IIS时出现的奇怪问题
    [转]30个你必须记住的css选择器
    打开一个从网络上下载的chm文件时出现“已取消到该网页的导航”
    [转]使用Modernizr 检测HTML5和CSS3浏览器支持功能
  • 原文地址:https://www.cnblogs.com/Qmelbourne/p/6047656.html
Copyright © 2011-2022 走看看