1073. Scientific Notation (20)
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-03Sample Output 1:
0.00123400Sample Input 2:
-1.2E+10Sample Output 2:
-12000000000
注意最后的输出的最大长度可能达到20000,比如指数为-9999,底数的长度也为9999,则长度将近20000.题目大体分是那种情况讨论,即指数>0,<0,==0。
#include <cstdio> #include <cstring> #include <string.h> char input[11000]; char output[30000]; char temp_zeors[30000]; int main(){ gets(input); int index_output = 0; if (input[0] == '-'){ output[index_output++] = '-'; } else{ output[0] = '\0'; } int index_E = 0; int index_expop = 0; while(input[index_E] != 'E'){ index_E ++; } int exp; sscanf(&input[index_E + 1], "%d", &exp); if(exp == 0){ // strcpy(&output[index_output], &input[1]); for(int i = 1; i < index_E; i++){ output[index_output++] = input[i]; } output[index_output] = 0; } else if(exp < 0){ memset(temp_zeors, '0', strlen(input)); temp_zeors[1] = '.'; int j = -exp + 1; for(int i = 1; i < index_E; i++){ if(i != 2){ temp_zeors[j++] = input[i]; } } temp_zeors[j] = 0; strcpy(&output[index_output], temp_zeors); } else{ int zero_length = exp - (index_E - 2 - 1); int j = index_output; if(zero_length >= 0){ for(int i = 1; i < index_E; i++){ if(i != 2){ output[j++] = input[i]; } } for(int i = 0; i < zero_length; i++){ output[j++] = '0'; } output[j] = 0; } else{ for(int i = 1; i < index_E; i++){ if(i == 2) continue; if(i == index_E + zero_length){ output[j++] = '.'; } output[j++] = input[i]; } output[j] = 0; } } printf("%s\n", output); return 0; }