【PAT乙級、C++】1024 科學計數法 (20分)

MYMarcoreus發表於2020-12-05

雖然AC了,但是我寫完了自己卻看不懂!!!!!!!!!!!

# include <iostream>
# include <cmath>

using namespace std;

string str;
string num;

int main()
{
    
    cin >> str;
    
    int i;
    for(i=0;i<str.size() && str[i] != 'E';++i){
        if(str[i] >= '0' && str[i] <= '9'){
            num += str[i];
        }
    }
    
    int move = stoi(str.substr(i+1));
    if(move < 0){
        if(str[0] == '-')
            cout << '-';
        cout << "0.";
        for(int i=0;i<(-move)-1;i++)
            cout << 0;
        cout << num;
    }
    else{
        if(str[0] == '-')
             cout << '-';
        cout << num[0];
        if(num.size() <= move+1){
            cout << num.substr(1);
            for(int i=0;i<move-num.size()+1;++i){
                cout << 0;
            }
        }
        else{
            cout << num.substr(1,move);
            cout << '.';
            cout << num.substr(move+1);
        }
    }
    
    
    return 0;
}

相關文章