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 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
思路
这题也很简单哈,主要就是分情况讨论:
- 因为保证科学计数法的整数部分只有一位数,因此,如果此时它的指数是负数,那么直接补
指数-1
个“0”(小数点到整数部分的首个数字之间的“0”),然后再插入小数点,再插入0即可(即:0.00xxx); - 如果它的指数是正数,那么就要分情况讨论啦:
1.如果科学计数法的小数部分的长度比指数的大小还大,那么说明计算之后的结果小数点还在,这个时候只要计算把小数点插在哪儿就行了~
2.如果科学计数法的小数部分的长度比指数的大小要小,那么就说明要进行补0的操作,0的个数应该是指数-小数部分长度
(我这里的代码写得有问题哈,我只写了在后面补指数-1
个“0”,这应该是不对的);
3.如果科学计数法的小数部分的长度等于指数的大小,那么直接删了“E”后面的内容(包括“E”)以及小数点就行了。
代码
#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;
int main()
{
string ori;
cin>>ori;
bool Positive = false;
if(ori[0]=='+') Positive = true;
ori.erase(ori.begin());//去字符
int PosPoint = ori.find(".");
int PosE = ori.find("E");
string xiaoshubufen = ori.substr(PosPoint+1, PosE-2);
ori.erase(ori.begin()+1);//去小数点
PosE = ori.find("E");//更新一下字符E的位置
string a = ori.substr(0, PosE);
string b = ori.substr(PosE+1);
int zhishu = stoi(b);
if(zhishu<0){
for(int i=1;i<=abs(zhishu)-1;i++){
a.insert(0, "0");
}
a.insert(0, ".");
a.insert(0, "0");
if(Positive==false) a.insert(0, "-");
}
else{
if(Positive==false) a.insert(0, "-");
if(xiaoshubufen.length()<zhishu){
for(int i=1;i<=zhishu-1;i++){
a += "0";
}
}
else if(xiaoshubufen.length()>zhishu){
a.insert(zhishu+2 ,".");
}
}
cout<<a;
return 0;
}