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,
+1.23400E-03Sample Output 1:
0.00123400Sample Input 2:
-1.2E+10Sample 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 ;
}
这道题就是一些处理方法的堆积,并没有十分明确的算法思想,有一个错误还没有找,觉得应该是边界点的处理方式还有问题。