hdoj1002解法

這道題最操蛋的就是他輸出格式的問題,真操蛋,做完這道題就是想罵人

一直提示Presentation Error

不說了,直接看代碼吧

#include<iostream>
using namespace std;

int main(){
    int n,j=0,a1,a2,up,down;//up代表進位的數,down代表本位數
    cin>>n;
    while(n--){
    char c;
    string s1,s2,s3="" , s4 = "0";
    cin >> s1 >> s2;
    if(j!=0)      //這裏注意格式,否則會報Presentation Error的
    cout<<endl;
    cout<<"Case "<<++j<<":"<<endl;
    cout<<s1<<" + "<<s2<<" = ";
    int h1 , h2 , h , hmax , hh;
    h1 = s1.length();
    h2 = s2.length();
    hmax = max(h1,h2);
    hh = hmax;
    if(h1>h2){
        h = h1-h2;
        for(int i=0;i<h;i++){
            s2 =s4+s2;
        }
    }else{
        h = h2-h1;
        for(int i=0;i < h;i++){
            s1 = s4+s1;
        }
    }
    up = down = 0;
    while(hmax--){
        a1 = s1[hmax] - '0';
        a2 = s2[hmax] - '0';
        down = a1+a2+up;
        if( down > 9){
            up = down/10;
            down = down%10;
        }else{
            up = 0;
        }
        c = down + '0';
        s3 +=c;
    }
    if(up > 0){
        c = up + '0';
        s3 += c;
        hh++;
    }
    for(int i=hh-1;i>=0;i--){
        cout<<s3[i];
    }
    cout<<endl;
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章