PAT-乙级-1024. 科学计数法 (20) 求问为什么测试点4通不过???

我的代码(冗长勿喷):

求问为什么测试点4通不过???

#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int main(){
  string A;
  cin>>A;
  int posE;
  for(posE=0; posE<A.length(); posE++)
  {
    if(A[posE]=='E')
    {
      break;
    }
  }
  string base,expS;
  base = A.substr(1, posE-1);
  expS = A.substr(posE+2, A.length());
  int exp=0;
  int L=expS.length();
  for(int i=0; i<L; i++)
  {
    int c=expS[L-i-1] - '0';
    exp+=c*pow(10, i);
  }
  if(exp==0)
  {
    if(A[0]=='+')
    {
      cout<<base<<endl;
    }
    else
    {
      cout<<A[0]<<base<<endl;
    }
  }
  else if(A[posE+1]=='-')
  {
    if(A[0]=='-')
    {
      cout<<A[0];
    }
    cout<<"0.";
    int add = exp -1;
    for(int i=0; i<add; i++)
    {
      cout<<"0";
    }
    for(int i=0; i<base.length(); i++)
    {
      if(base[i]!='.')
      {
        cout<<base[i];
      }
    }
  }
  else if(A[posE+1]=='+')
  {
    int lenOfBase=base.length();
    int right =lenOfBase -2;
    if(exp >= right)
    {
      int add = exp - right;
      if(A[0]=='-')
      {
        cout<<A[0];
      }
      for(int i=0; i<base.length(); i++)
      {
        if(base[i]!='.')
        {
          cout<<base[i];
        }
      }
      for(int i=0; i<add; i++)
      {
        cout<<"0";
      }
    }
    else
    {
      int index;
      for(index=0; index<exp+2; index++)
      {
        if(base[index]!='.')
        {
          cout<<base[index];
        }
      }
      cout<<".";
      for(; index<base.length(); index++)
      {
        cout<<base[index];
      }
    }
  }
  return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值