C - 3 CodeForces - 691C(模拟,科学计数法,字符串)

You are given a positive decimal number x.

Your task is to convert it to the “simple exponential notation”.

Let x = a·10b, where 1 ≤ a < 10, then in general case the “simple exponential notation” looks like “aEb”. If b equals to zero, the part “Eb” should be skipped. If a is an integer, it should be written without decimal point. Also there should not be extra zeroes in a and b.

Input
The only line contains the positive decimal number x. The length of the line will not exceed 106. Note that you are given too large number, so you can’t use standard built-in data types “float”, “double” and other.

Output
Print the only line — the “simple exponential notation” of the given number x.

Examples
Input
16
Output
1.6E1
Input
01.23400
Output
1.234
Input
.100
Output
1E-1
Input
100.
Output
1E2

题意:和晚训E题差不多,都是科学计数法的转换,只不过这道题更加恶心,细节更多。
思路:模拟,主要是细节的处理

#include <bits/stdc++.h>

using namespace std;

int main()
{
    char str[1000005];
    int num = 0;
    scanf("%s",str);
    int len = strlen(str);
    int index_L = -1,index_R = -1,index_P = -1;
    for(int i = 0;i < len;i++)
    {
        if(str[i] != '0' && str[i] != '.')
        {
            index_L = i;
            break;
        }
    }
    for(int i = len - 1;i >= 0;i--)
    {
        if(str[i] != '0' && str[i] != '.')
        {
            index_R = i;
            break;
        }
    }
    for(int i = 0;i < len;i++)
    {
        if(str[i] == '.')
            index_P = i;
    }
    if(index_P == -1)//没有小数点
    {
        index_P = len;
    }

    if(index_L == -1)
        cout<<"0"<<endl;
    else
    {
        if(index_P > index_L)//统计E后面的数字
            num = index_P - index_L - 1;//E的数目
        else
            num = index_P - index_L;//E后面接负数
        cout<<str[index_L];//输出第一个数
        if(index_L != index_R)//数的有效位数超过1则输出小数点
            cout<<".";
        for(int i = index_L + 1;i <= index_R;i++)//输出之后除了小数点的数
        {
            if(str[i] != '.')
                cout<<str[i];
        }
    }
    if(num != 0)//E后面接0的话就不输出
        cout<<"E"<<num<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/89042173