1073. Scientific Notation (20)

1.根据科学计数法,还原原来的整数。

2.根据E后面是正数还是负数,决定小数点左移还是右移。

3.我的算法是,先提取出正负号和E之间的数字(已经去掉小数点),即+1.234567E+04,经过提取后变为1234567,后续直接对这个字符串进行处理。

4.右移时,需要注意3种情况:

1)如+1.200E+03,为1200   ,刚好不需要补零和小数点;

2)如+1.234567E+04 为12345.67 , 需要补小数点;

3)如+1.2E+04 为12000 ,需要补零。

5.左移时,需要考虑两种情况:

1)如+1.2345E-00,只需要补上小数点;

2)-1.2345E-01  ,-1.2345E-02,- 1.2345E-03 ,需要在12345中,补上0和小数点。

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
HOU, Qiming

Scientific notation is the way that scientists easily handle very large numbers or very small numbers. The notation matches the regular expression [+-][1-9]”.”[0-9]+E[+-][0-9]+ which means that the integer portion has exactly one digit, there is at least one digit in the fractional portion, and the number and its exponent’s signs are always provided even when they are positive.

Now given a real number A in scientific notation, you are supposed to print A in the conventional notation while keeping all the significant figures.

Input Specification:

Each input file contains one test case. For each case, there is one line containing the real number A in scientific notation. The number is no more than 9999 bytes in length and the exponent’s absolute value is no more than 9999.

Output Specification:

For each test case, print in one line the input number A in the conventional notation, with all the significant figures kept, including trailing zeros,

Sample Input 1:

+1.23400E-03

Sample Output 1:

0.00123400

Sample Input 2:

-1.2E+10

Sample Output 2:

-12000000000

AC代码:

//#include<string>
//#include <iomanip>
//#include<stack>
//#include<unordered_set>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<unordered_map>
#include<set>
#include<queue>
#include<map>
#include<vector>
#include <algorithm>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
#include<stack>
using namespace std;

/*
+1.2345E+00
+1.234567E+04
+1.2E+04
+1.200E+03
-1.200E+03

+1.2345E-00
+1.2345E-01
+1.2345E-02
+1.2345E-03

-1.2345E-00
-1.2345E-01
-1.2345E-02
-1.2345E-03
*/
int main(void)
{
	string str;
	cin >> str;

	string sign = "-";
	if (str[0] == '+') sign = "";
	int ePos = 0;//E的位置
	string ans = "";
	for (int i = 1; i < str.size(); i++)
	{
		if (str[i] == 'E')
		{
			ePos = i;
			break;
		}
		else if (str[i] != '.')
			ans += str[i];//去掉小数点和正负号,E后面的部分
	}
	bool right = (str[ePos+1] == '+');//如果E后面是+号,那么右边+0,即小数点左移
	int tenNum = 0;
	for (int i = ePos + 2; i < str.size(); i++)
		tenNum = tenNum * 10 + str[i] - '0';
	if (right)
	{
		int zeroSum = tenNum - (ans.size() - 1);
		if (zeroSum == 0)//如+1.200E+03,为1200
			ans = sign + ans;
		else if (zeroSum < 0)
		{//如+1.234567E+04 为12345.67,ans长度为7,tenNum为4,小数点右移4位
			string tmp = "";
			tmp = ans.substr(0, 1 + tenNum) + "." + ans.substr(1 + tenNum);
			ans = sign + tmp;
		}
		else if (zeroSum > 0)
		{//如+1.2E+04 为12000,补充零的个数为1,即ans为12,长度减1后为1. 然后4-1=3,补3个0
			for (int i = 0; i < zeroSum; i++)
				ans += '0';
			ans = sign + ans;
		}
	}
	else
	{//小数点左移
		if (tenNum == 0)//如果+1.2345E-00
			ans =sign+ str.substr(1, ePos-1);
		else
		{//tenNum>0
			for (int i = 0; i < tenNum - 1; i++)
			{
				ans = '0' + ans;
			}
			ans = sign + "0." + ans;
		}
	}
	cout << ans << endl;
	return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注