题目链接:http://codeforces.com/problemset/problem/691/C
C. Exponential notation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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
Copy
16
output
Copy
1.6E1
input
Copy
01.23400
output
Copy
1.234
input
Copy
.100
output
Copy
1E-1
input
Copy
100.
output
Copy
1E2
题解:模拟题,要自己写才不会乱!!!
本题做法有很多,自己用的双端队列。
#include<iostream>
#include<cstring>
#include<deque>
using namespace std;
int x;
int main()
{
string s;
while(cin>>s)
{
deque<char>que;
int len=s.size();
int q=0;
for(int i=0;i<len;i++) //q保证输入数据不为0
{
if(s[i]=='.')
continue;
if(s[i]!='0')
{
q=1;
break;
}
}
if(q==0) //如果q为0,则输出0即可
{
cout<<"0"<<endl;
continue;
}
int flag=0; //flag表示小数点是否出现
for(int i=0; i<len; i++)
{
if(s[i]=='0'&&que.empty()) //将前导0去掉
continue;
if(s[i]=='.')
{
flag=1;
x=que.size(); //记录小数点之间的位数
}
que.push_back(s[i]);//放入双向队列
}
if(flag==0) //小数点没出现的情况下,只需将后导0去除后,进行模拟即可
{
cout<<que.front();
int t=que.size();
while(que.back()=='0')
que.pop_back();
que.pop_front();
if(que.size()) //考虑100特例
cout<<".";
while(!que.empty())
{
cout<<que.front();
que.pop_front();
}
if(t>1) //考虑特例
cout<<"E"<<t-1<<endl;
else
cout<<endl;
}
else //小数点出现情况
{
while(que.back()=='0')//将后导0去除
que.pop_back();
if(x==0) //如果小数点前面全为0
{
que.pop_front(); //将小数点去掉
while(que.front()=='0')//决定谁是要输出的第一个数,并决定从第一个小数点起有几位额外的0
{
x++;
que.pop_front();
}
cout<<que.front();
que.pop_front();
if(que.size()>=1)
cout<<".";
while(!que.empty())
{
cout<<que.front();
que.pop_front();
}
x++; //举例更新
cout<<"E"<<"-"<<x<<endl;
}
else //一般情况
{
if(que.back()=='.')//如果最后一个为小数点,则将其去除即可
{
que.pop_back();
while(que.back()=='0') //之后要去除后导0
que.pop_back();
}
if(que.size()!=1) //如果经过操作后之剩下1位数了,就不用输出小数点了
{
cout<<que.front();
que.pop_front();
cout<<".";
}
while(!que.empty())
{
if(que.front()=='.')//将原先小数点去除
{
que.pop_front();
continue;
}
cout<<que.front();
que.pop_front();
}
if(x!=1) //x=0即不用输出E
cout<<"E"<<x-1<<endl;
else
cout<<endl;
}
}
}
return 0;
}