PAT - Level B 1024 Science and Technology Law

1024. Scientific Notation(20)

time limit
100 ms
memory limit
65536 kB
code length limit
8000 B
Judgment procedure
Standard
author
HOU, Qiming

Scientific notation is a convenient way for scientists to represent very large or small numbers that satisfy the regular expression [+-][1-9]"."[0-9]+E[+-] [0-9]+, that is, the integer part of the number has only 1 digit, and the fractional part has at least 1 digit, and the sign of the number and its exponent part must be clearly given even for positive numbers.

Now give the real number A in scientific notation format, please write a program to output A in ordinary number notation, and ensure that all significant digits are preserved.

Input format:

Each input contains 1 test case, a real number A in scientific notation. The storage length of the number does not exceed 9999 bytes, and the absolute value of its exponent does not exceed 9999.

Output format:

For each test case, output A in plain number notation on a single line, with all significant bits guaranteed to be preserved, including trailing zeros.

Input sample 1:
+1.23400E-03
Sample output 1:
0.00123400
Input sample 2:
-1.2E+10
Sample output 2:
-12000000000

Idea: Use the strtok() method to separate the read strings, and write the rest of the methods behind the code to help understand

#include<cstdio>
#include<cstring>
using namespace std;

int main(){
	char arr[10005];
	gets(arr);
	char *p = strtok(arr, "E"); // first string obtained by E separator
	char *p1 = strtok(NULL, "E"); // The obtained second character strtok function method is only a string for the first time and then null
	int len ​​= strlen(p1);
	int cnt = 0;
	for(int i = 1; i < len; i++) cnt = cnt * 10 + p1[i] - '0'; // get how many 10s there are
	if(p1[0] == '-'){    
		if(cnt > 0){ // If there is a negative sign and there is at least one 10, the integer part is shifted back by at least one place and 0 is output first.
			if(p[0] == '-') printf("-");
			printf("0.");
		}
		len = strlen(p); //Calculate the length of the number in front of E, output cnt 10, that is, cnt 0, and output p in order
		for(int i = 0; i < cnt-1; i++) printf("0");
		for(int i = 1; i < len; i++){
			if(i == 2) continue;
			printf("%c", p[i]);
		}
	}else{
		if(cnt > 0){ // If it is a positive sign, output p first
			if(p[0] == '-') printf("-");
			printf("%c", p[1]); output the integer part first
		}
		len = strlen (p);
		int x = len - 3 - cnt; //length minus the first sign decimal point and the integer part totaling 3 digits and the length of the remaining decimal part
		if(x <= 0){ // If the length of the decimal point to the right is greater than the length of the fractional part of p
			for(int i = 3; i < len; i++) printf("%c", p[i]);
			for(int i = 0; i < -x; i++) printf("0");
		}else{
			for(int i = 3; i < 3 + cnt; i++) printf("%c", p[i]);
			printf(".");
	        for(int i = 3 + cnt; i < len; i++) printf("%c", p[i]);
		}
	}
	return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325755442&siteId=291194637