Codeforces 691C. Exponential notation 模拟

B - Exponential notation
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit

Status
Description
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.

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

模拟题要写注释才不会乱= =

#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<set>
using namespace std;
string s;
int main()
{
    while (cin>>s)
    {
        //去.前面的0
        int a=0;
        for (int i=0; i<(int)s.length(); i++)
        {
            if (s[i]!='0')
            {
                a=i;
                break;
            }
        }
        if (a>0) s.erase(0,a);
        //找.
        int pos=s.find('.');
        //有.
        if (pos!=(int)string::npos)
        {
            //第一个
            if (pos==0)
            {
                s.erase(0,1);
                //去前面的0并计数
                int temp=0;
                int a=0;
                for (int i=0; i<(int)s.length(); i++)
                {
                    if (s[i]!='0')
                    {
                        a=i;
                        break;
                    }
                    else
                        temp++;
                }
                if (a>0) s.erase(0,a);
                //个位数
                if (s.length()==1)
                {
                    cout<<s<<'E'<<0-(temp+1)<<endl;
                }
                //多位数
                else
                {
                    s.insert(1,1,'.');
                    int b=0-(temp+1);
                    //去.后面的0
                    int c=-1;
                    for (int i=(int)s.length()-1; i>=0; i--)
                    {
                        if (s[i]!='0')
                        {
                            c=i+1;
                            break;
                        }
                    }
                    s.erase(c);
                    //.是最后一个
                    if (s[s.length()-1]=='.') s.erase(s.length()-1,1);
                    if (b==0)
                        cout<<s<<endl;
                    else
                        cout<<s<<'E'<<b<<endl;
                }
            }
            //最后一个
            else if(pos==(int)s.length()-1)
            {
                s.erase(pos,1);
                //个位数
                if (s.length()==1)
                {
                    cout<<s<<endl;
                }
                //多位数
                else
                {
                    s.insert(1,1,'.');
                    int b=s.length()-2;
                    //去.后面的0
                    int c=-1;
                    for (int i=(int)s.length()-1; i>=0; i--)
                    {
                        if (s[i]!='0')
                        {
                            c=i+1;
                            break;
                        }
                    }
                    s.erase(c);
                    if (s[s.length()-1]=='.') s.erase(s.length()-1,1);
                    if (b==0)
                        cout<<s<<endl;
                    else
                        cout<<s<<'E'<<b<<endl;
                }
            }
            //中间
            else
            {
                //去.前面的0
                int a=0;
                for (int i=0; i<(int)s.length(); i++)
                {
                    if (s[i]!='0')
                    {
                        a=i;
                        break;
                    }
                }
                if (a>0) s.erase(0,a);
                //去.后面的0
                int c=-1;
                for (int i=(int)s.length()-1; i>=0; i--)
                {
                    if (s[i]!='0')
                    {
                        c=i+1;
                        break;
                    }
                }
                s.erase(c);
                //重新插入.
                pos=s.find('.');
                s.erase(pos,1);
                s.insert(1,1,'.');
                int b=pos-1;
                //去.后面的0
                c=-1;
                for (int i=(int)s.length()-1; i>=0; i--)
                {
                    if (s[i]!='0')
                    {
                        c=i+1;
                        break;
                    }
                }
                s.erase(c);
                //.是最后一个
                if (s[s.length()-1]=='.') s.erase(s.length()-1,1);
                if (b==0)
                    cout<<s<<endl;
                else
                    cout<<s<<'E'<<b<<endl;
            }
        }
        //没有.
        else
        {
            //去前面的0
            int a=0;
            for (int i=0; i<(int)s.length(); i++)
            {
                if (s[i]!='0')
                {
                    a=i;
                    break;
                }
            }
            if (a>0) s.erase(0,a);
            //个位数
            if (s.length()==1)
            {
                cout<<s<<endl;
            }
            //多位数
            else
            {
                s.insert(1,1,'.');
                int b=s.length()-2;
                //去.后面的0
                int c=-1;
                for (int i=(int)s.length()-1; i>=0; i--)
                {
                    if (s[i]!='0')
                    {
                        c=i+1;
                        break;
                    }
                }
                s.erase(c);
                //.是最后一个
                if (s[s.length()-1]=='.') s.erase(s.length()-1,1);
                if (b==0)
                    cout<<s<<endl;
                else
                    cout<<s<<'E'<<b<<endl;
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值