CF691C Exponential notation

CF691C Exponential notation

洛谷评测传送门

题目描述

You are given a positive decimal number xx .

Your task is to convert it to the "simple exponential notation".

Let x=a·10^{b}x=a⋅10b , where 1<=a<10 , then in general case the "simple exponential notation" looks like "aEb". If bb equals to zero, the part "Eb" should be skipped. If aa is an integer, it should be written without decimal point. Also there should not be extra zeroes in aa and bb .

输入格式

The only line contains the positive decimal number xx . The length of the line will not exceed 10^{6}106 . Note that you are given too large number, so you can't use standard built-in data types "float", "double" and other.

输出格式

Print the only line — the "simple exponential notation" of the given number xx .

题意翻译

给定一个长度为N的数字,转化为 标准的科学计数法形式。N最大可达1e6。要考虑前置0 和 后置 0 的特殊情况。 当指数为0的时候,不输出指数部分。

感谢@liyifeng 提供的翻译


题解:

依题意模拟即可。

代码:

#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1e6+6;
char ori[maxn];
bool flag;
int main()
{
	scanf("%s",ori+1);
	int len=strlen(ori+1);
	int l=1,r=len;
	int ppos=0;
	if(len==1)
	{
		printf("%c
",ori[l]);
		return 0;
	}
	while(ori[l]=='0')
		l++;
	if(ori[r]=='.')
		r--;
	for(int i=l;i<=r;i++)
		if(ori[i]=='.')
		{
			ppos=i;
			flag=1;
			break;
		}
	if(ppos==len)
		flag=0;
	if(ppos==l)
	{
		l++;
		while(ori[r]=='0')
			r--;
		if(ori[r]=='.')
			r--;
		while(ori[l]=='0')
			l++;
		int cnt=0;
		while(cnt<=r-l)
		{
			if(cnt==1)
				printf(".");
			printf("%c",ori[l+cnt]);
			cnt++;
		}
		printf("E%d
",-(l-ppos));
		return 0;
	}
	else if(flag==1)
	{
		while(ori[r]=='0')
			r--;
		if(ori[r]=='.')
			r--;
		while(ori[r]=='0')
			r--;
		int cnt=0;
		while(cnt<=r-l)
		{
			if(cnt==1)
				printf(".");
			if(ori[l+cnt]=='.')
			{
				cnt++;
				continue;
			}
			printf("%c",ori[l+cnt]);
			cnt++;
		}
		if(ppos-l-1==0)
			return 0;
		printf("E%d
",ppos-l-1);
		return 0;
	}
	else if(flag==0)
	{
		if(l==r)
		{
			printf("%c",ori[l]);
			return 0;
		}
		int cnt=0;
		int zhi=r-l;
		while(ori[r]=='0')
			r--;
		while(cnt<=r-l)
		{
			if(cnt==1)
				printf(".");
			printf("%c",ori[l+cnt]);
			cnt++;
		}
		printf("E%d
",zhi);
		return 0;
	}
	return 0;
}
原文地址:https://www.cnblogs.com/fusiwei/p/13955491.html