題目
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
思路
這種題不難但麻煩。
用string保存整個數字,則有:
- s[0]是底數符號位
- s[2]是’.’
- 'E’的位置後是指數
若s[0]是負號則輸出’-’,是正號則忽略;
若指數>=0,那麼s[1]必不變,可以先直接輸出,然後根據指數大小輸出小數點後的數字,指數超過小數位數則末尾補0;若不超過則後面加上小數點後再繼續輸出。
若指數<0,那麼前面補了指數個0,且第一個0後面有小數點。接着將底數去掉小數點後順序輸出即可。
代碼
#include <iostream>
#include <string>
using namespace std;
int main(){
string s;
cin >> s;
if (s[0]=='-'){
cout << '-';
}
int pos = s.find('E');
int exp = stoi(s.substr(pos+1));
if (exp >= 0){
cout << s[1];
int i = 3;
while (i<pos && exp--){
cout << s[i];
i++;
}
if (i==pos){
while (exp--){
cout << 0;
}
}
else{
cout << '.';
while (i<pos){
cout << s[i];
i++;
}
}
}
else{
for (int i=0; i<-exp; i++){
cout << 0;
if (i==0){
cout << '.';
}
}
cout << s[1];
for (int i=3; i<pos; i++){
cout << s[i];
}
}
return 0;
}