题目链接: B. Barnicle

题意:给出科学计数法 转化成十进制的整数或小数 并输出。

思路:暑假训练赛见过了,当时大腿A掉了,并表示是道水题。

   刷CF再次遇见,毫不留情WA了几次。比如:

        0.e0       0

        1.0e0     1

   突然觉得自己不能再依赖CF这种看着sample dbug的模式了。

附代码:

/// 给出科学计数法 转化成十进制的整数或小数 并输出

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;

string str;

int main() {
    //freopen("in.cpp", "r", stdin);
    while(cin >> str) {
        string ans = "";
        int len = str.length();
        int lose;
        for (int i=len-1; i>=0; --i) {
            if (str[i] == 'e') {
                lose = i;
                break;
            }
        }

        int a = str[0] - '0';
        if (a == 0) {
            int endd = 0;
            for (int i=lose-1; i>=0; --i){
                if (str[i] > '0' && str[i] <= '9') {
                    endd = i;
                    break;
                }
            }
            str.resize(endd+1);
            cout << str << endl;
            continue;
        }

        ans += str[0];
        int b = 0;
        for (int i=lose+1; i<len; ++i) {
            b = b*10 + str[i]-'0';
        }
        int cnt1 = 0;
        bool first = false; //
        for (int i=2; i<lose; ++i) {
            if (cnt1 < b) {
                ans += str[i];
            }
            else {
                if (first == false) {
                    first = true;
                    ans += '.';
                }
                ans += str[i];
            }
            cnt1++;
        }
        while(cnt1 < b) {
            ans += '0';
            cnt1++;
        }
        if (ans[1] == '.') {
            int anslen = ans.length();
            bool ok = false;
            for (int i=2; i<anslen; ++i) {
                if (ans[i] > '0' && ans[i] <= '9') {
                    ok = true;
                    break;
                }
            }
            if (!ok) ans.resize(1);
        }
        cout << ans << endl;
    }
    return 0;
}
posted on 2016-09-09 18:16  小小八  阅读(266)  评论(0编辑  收藏  举报