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;
}