C. Exponential notation
You are given a positive decimal numberx.
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". Ifb equals to zero, the part "Eb" should be skipped. Ifa is an integer, it should be written without decimal point. Also there should not be extra zeroes ina andb.
Input
The only line contains the positive decimal numberx. The length of the line will not exceed106. 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 numberx.
Example
Input
16
Output
1.6E1
01.23400
1.234
.100
1E-1
100.
1E2
题目大意:给定一些不太规范的数字,让你用规范的科学记数法记录这些数字
解题思路:要充分考虑输入数据的多种情况,确保程序在各种复杂的情况下都不会出错,对整数的处理,对前导零还有末位的零的处理,对全0数字的处理,诸如数据(00.000 00123455000 00.012345600)
实际编写代码时要注意思路简洁清晰,越简洁的(正确的)思路出错概率越低~个人认为自己写的代码相对来讲比较简洁,看到了很多140h+的代码~(/▽\=)
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
int num[1000002];
string s;
int main()
{
int i;
cin>>s;
int len = s.length();
bool first = true;
int num_pos=-1,point_pos = inf;//num_pos记录第一个非零数字出现的位置,point_pos记录小数点出现的位置
int k = 0;//记录下每一个数字的编号
int lastnum = 0;//记录末尾数字所在的位置
for(i=0;i<len;i++)
{
if(s[i]>='1' && s[i]<='9')
{
num[k++] = s[i]-'0';
lastnum = k-1;
if(first)
{
num_pos = i;//记录第一个非零数字出现的编号
first = false;
}
}
else if(s[i]=='0' && !first)
num[k++] = s[i]-'0';//将数字0也按照顺序记录,去除前导0
else if(s[i]=='.')
point_pos = i;//记录小数点出现的位置
}
if(num_pos==-1)//如果一个非零数字都没有出现,直接输出0
{
cout << "0" << endl;
return 0;
}
if(point_pos==inf)//如果没有出现小数点,说明是一个整数,指数部分处理略有不同
{
cout << num[0] ;
if(lastnum>=1)
cout << '.';
for(i=1;i<=lastnum;i++)
cout << num[i];
int ex = len-1-num_pos;
if(ex==0)
cout << endl;
else
cout << "E" << ex << endl;
}
else
{
cout << num[0];
if(lastnum>=1)
cout << '.';
for(i=1;i<=lastnum;i++)
cout << num[i];
int ex = point_pos - num_pos;
if(ex>0) ex--;
if(ex==0)
cout << endl;
else
cout << "E" << ex << endl;
}
//cout << "Hello world!" << endl;
return 0;
}