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 turnback(string a,int b){
int t = 0;string r;
for(int i=0;i<a.size();i++){
if(a[i]=='.'){
t = i;
continue;
}else{
r+=a[i];
}
}
if(t+b>=r.size()){
for(int i = r.size();i<b+t;i++){
r+='0';
}
return r;
}else{
r = r.substr(0,t)+r.substr(t,b)+'.'+r.substr(t+b,r.size()-t-b);//测试点4
return r;
}
}
往前移动如下:
//往前移动:
string turnfront(string a,int b){
int t = 0;string r;
for(int i=0;i<a.size();i++){
if(a[i]=='.'){
t = i;
continue;
}
else{
r+=a[i];
}
}
string k;
if(t-b>0){
for(int i = 0;i<t-b;i++){
k+=r[i];
}
k+='.';
for(int i=t-b;i<r.size();i++){
k+=r[i];
}
return k;
}else{
k+="0.";
for(int i=0;i+t-b<0;i++){
k+='0';
}
for(int i=0;i<r.size();i++){
k+=r[i];
}
return k;
}
}
易错点:
1. 注意移动时候的小数点位置问题,计算出错就会出现各种测试点的错误;
代码:
#include<bits/stdc++.h>
using namespace std;
string turnback(string a,int b){
int t = 0;string r;
for(int i=0;i<a.size();i++){
if(a[i]=='.'){
t = i;
continue;
}else{
r+=a[i];
}
}
if(t+b>=r.size()){
for(int i = r.size();i<b+t;i++){
r+='0';
}
return r;
}else{
r = r.substr(0,t)+r.substr(t,b)+'.'+r.substr(t+b,r.size()-t-b);//测试点4
return r;
}
}
string turnfront(string a,int b){
int t = 0;string r;
for(int i=0;i<a.size();i++){
if(a[i]=='.'){
t = i;
continue;
}
else{
r+=a[i];
}
}
string k;
if(t-b>0){
for(int i = 0;i<t-b;i++){
k+=r[i];
}
k+='.';
for(int i=t-b;i<r.size();i++){
k+=r[i];
}
return k;
}else{
k+="0.";
for(int i=0;i+t-b<0;i++){
k+='0';
}
for(int i=0;i<r.size();i++){
k+=r[i];
}
return k;
}
}
int main(){
string N;
cin>>N;
if(N[0]=='-'){
int i = 1;string a;
while(N[i]!='E'){
a+=N[i];
i++;
}
i++;
int num = 0;
if(N[i]=='+'){
int t = i+1;
for(int j = t;j<N.size();j++){
num = num*10+stoi(N.substr(j,1));
}
cout<<"-"<<turnback(a,num);
}else{
int t = i+1;
for(int j = t;j<N.size();j++){
num = num*10+stoi(N.substr(j,1));
}
cout<<"-"<<turnfront(a,num);
}
}else{
int i = 1;string a;
while(N[i]!='E'){
a+=N[i];
i++;
}
i++;
int num = 0;
if(N[i]=='+'){
int t = i+1;
for(int j = t;j<N.size();j++){
num = num*10+stoi(N.substr(j,1));
}
cout<<turnback(a,num);
}else{
int t = i+1;
for(int j = t;j<N.size();j++){
num = num*10+stoi(N.substr(j,1));
}
cout<<turnfront(a,num);
}
}
return 0;
}