pat1073

19 篇文章 0 订阅

1073. Scientific Notation (20)

时间限制  100 ms  内存限制  32000 kB   代码长度限制  16000 B  判题程序  Standard  作者  HOU, Qiming

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 file 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



#include <cstdio>
#include <string.h>

using namespace std ;


int pow ( int i )
{
  int n ;
  int result = 1 ;
  for ( n = 0 ; n < i ; n++ ) 
   result *= 10 ;

  return result ;
}

int getBase (char base[]  )
{
  int result = 0 ;
  int len = 0 ;

  len = strlen( base ) ;


  for ( int i = 0 ; i < len ; i++ )
  {
    
    result += pow(len - i-1 ) * (base[i]-'0') ;
  }

  return result ;
}


char num[10005], base[5] ;
int num_len= 0 ,base_len= 0 ;
int baseNum = 0 ;
char mark1 , mark2 ;


bool isZero()
{
  int result = 0 ;
   bool flag = true ;

  for ( int i = 0 ; i < num_len ; i++ )
  {
    if ( num[i] != '0' ) 
      flag = false ;
  
    //0.00printf("%c", num[i]) ;
  
  }

  return flag ;
}



void printfNum()
{
  if ( isZero()  )
  {
    printf("0") ;
    return ;
  }

  if(mark1=='-')
    printf("-") ;
  if(mark2 == '+')
  {
    if ( baseNum < num_len )//insert print
    {
      for( int i = 0 ; i < num_len ; i++ )
      {
        if  ( i == num_len )
        {
          printf(".") ;
        }
        printf("%c", num[i]) ;
      }
    }
    else //append 0 output 
    {
      for ( int i = 0 ; i < num_len ; i++ )
      {
        printf("%c", num[i]) ;
      }

      for ( int i = 0 ;  i <=( baseNum - num_len ) ; i++ )
      {
        printf("0") ;
      }

    }
  }
  else if ( mark2 == '-' )
  {
    printf("0.") ;

    for ( int i = 0 ; i < (baseNum-1) ; i++ )
    {
      printf("0") ;
    }

    for ( int i = 0 ; i < num_len ; i++ )
    {
      printf("%c", num[i] ) ;
    }
  }
}

int main( void )
{
  char line[10005] ;
  int len = 0 , i ;

  memset( line , 0 , sizeof (line) ) ;
  memset( num , 0 , sizeof( num ) ) ;
  memset( base , 0 , sizeof ( base)) ;
  scanf( "%s" , line ) ;

  len = strlen( line ) ;

  mark1 =  line[0] ;

  for (  i = 1 ; i < len ; i++ )
  {
    if ( line[i] == '+' || line[i] == '-')
    {
      mark2 = line[i] ;
      break ;
      continue ;
    }
    else if ( line[i] == '.' || line[i] == 'E')
    {
      continue ;
    }

    else
    {
      num[num_len++] = line[i] ;
    }

  }

  for ( i++ ; i < len ; i++ )
  {
    base[base_len++] = line[i] ;
  }

  baseNum = getBase (base) ;

  printfNum();

  //system("pause") ;

  return 0 ;
}

这道题就是一些处理方法的堆积,并没有十分明确的算法思想,有一个错误还没有找,觉得应该是边界点的处理方式还有问题。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值