美文网首页
2019-08-27 A1073

2019-08-27 A1073

作者: JO炮 | 来源:发表于2019-08-27 10:27 被阅读0次

    这道题使我一开始感到毫无头绪,但是看过柳神的思路之后恍然大悟,解决这道题的关键是要想办法解决小数点左移和右移的问题,其实这也不是困难的问题,只要将小数点在合适的位置输出即可。

    #include <iostream>
    using namespace std;
    
    int main() {
        string s;
        int i = 0;
        cin >> s;
        while(s[i] != 'E')
            i++;
        string m = s.substr(1, i - 1);
        int n = stoi(s.substr(i + 1));
        if(s[0] == '-')
            printf("-");
        if(n < 0){
            printf("0.");
            for(int j = 0; j < abs(n) - 1; j++)
                cout << '0';
            for(int j = 0; j < m.length(); j++)
            if(m[j] != '.') cout << m[j];
        }
        else {
            cout << m[0];
            int j, t;
            for(j = 2, t = 0; j < m.length() && t < n; j++, t++){
                cout << m[j];
            }
            if(j == m.length()){
                for(int k = 0; k < n - t; k++) cout << '0';
            }else {
                cout << '.';
                for(int k = j; k < m.length(); k++) cout << m[k];
            }
        }   
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:2019-08-27 A1073

          本文链接:https://www.haomeiwen.com/subject/kchnsctx.html