一、题目描述
原题链接
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
二、解题思路
字符串处理题,保持脑袋清晰还是不难的。题目给出的科学计数法是一定包含小数点的,所以不需要单独考虑没有小数点的情况。输入字符串后,我们有两个位置是一定要确定的,一个是小数点的位置,一个是E的位置,我们用posdot和posE来表示。随后,我们按照E指数的正负进行分类,如果是负数,就要在小数点后补相应位数的0,如果是0就可以把前面的数直接输出,如果大于0,我们又要讨论一下,这个E指数的大小,是不是大于前面数字的位数,如果大于,则要在后面补0,不需要小数点,但是如果是小于,我们就要在合适的地方输出小数点。
三、AC代码
#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
using namespace std;
int to_num(string s)
{
int re = 0, sze = s.length();int st = 1;for(int i=st; i<sze; i++){
re = re*10 + s[i] - '0';}if(s[0] == '-') re = -re;return re;
}
int main()
{
int posdot, posE;string str;cin >> str;int sze = str.length();if(str[0] == '-') printf("-");for(int i=0; i<sze; i++){
if(str[i] == '.') posdot = i;if(str[i] == 'E') posE = i;}string exp = str.substr(posE+1, sze-posE-1);int e = to_num(exp);if(e < 0){
e = -e;printf("0.");for(int i=0; i<e-1; i++) printf("0");cout << str[posdot-1];for(int i=posdot+1; i<posE; i++) cout << str[i];}else if(e == 0){
for(int i=posdot-1; i<posE; i++) cout << str[i];}else{
int afterdot = posE - posdot - 1;if(e >= afterdot){
cout << str[posdot-1];for(int i=posdot+1; i<posE; i++) cout << str[i];for(int i=0; i<e-afterdot; i++) printf("0");}else{
cout << str[posdot-1];for(int i=0; i<e; i++){
cout << str[posdot+1+i];}printf(".");for(int i=posdot+1+e; i<posE; i++) cout << str[i];}}return 0;
}