• 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;
    }
    
  • 相关阅读:
    Shell xargs
    I2C总线图
    JS判断输入的字符串是否为数字
    CDN
    ④.linux基础之"字符集"
    01创建证书和环境准备
    梦的蒲公英
    java web项目部署遇到的jar cannot read的问题
    textbox icon jquery 插件
    解决双硬盘安装windows出现“安装程序无法定位现有系统分区,也无法创建新的系统分区”错误
  • 原文地址:https://www.cnblogs.com/fusiwei/p/13955491.html
Copyright © 2020-2023  润新知