Exponential notation 思维题

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

题目大意是说把给的数字进行处理,处理成科学记数法的形式,需要注意的一共有三个点,前缀0,小数点,后缀0,用三个变量来记录第一个不为0的数的位置,小数点的位置,最后一个不为0的数的位置,用这三个量来进行判断,首先是无论哪种情况,第一个数都是直接输出的,之后根据具体情况来确定小数点是不是输出,只有一位的时候就不用输出小数点了,之后再输出小数点后的位数,最后一步是判断E的值,这里需要根据小数点的位置来判断,如果小数点在第一个不为0的数后面,此时E的值为tar2-tar1-1,其余时候为tar2-tar1,如果这个值为0就不要输出。把模拟的细节处理好,还是比较容易的。

AC代码

#include
#include
using namespace std;
string num;
string ans;
int main()
{
	cin>>num;
	int flag=1;
	int tar1=-1,tar2=num.length(),tar3=-1; 
	for(int i=0;itar1)
			cout<<".";
		for(int i=tar1+1;i<=tar3;i++)
			if(num[i]!='.')
				cout<

你可能感兴趣的:(思维题)