Pat 甲级 训练真题集 1073!

1073. Scientific Notation (20)

​ 时间限制

​ 100 ms

​ 内存限制

​ 65536 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

先找到E的所在点,然后分小数部分与指数部分,然后分第一位和E后面的一位的正负,4种情况考虑到即可

代码如下:

#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int main(){
	string s;
	cin>>s;
	int index=s.find_first_of('E');
	char c=s[index+1];
	string s_zhishu(s,index+2,s.size()-index);
	string s_xiaoshu(s,0,index);
	s_xiaoshu.erase(2,1);
	s_xiaoshu.erase(0,1);
	if(s[0]=='+'){	
		if(c=='+'){
			stringstream ss;
			ss<<s_zhishu;
			int a;ss>>a;
		//	cout<<s_zhishu<<" "<<a<<endl;
			if(a>=s_xiaoshu.size()-1){
				cout<<s_xiaoshu;
				for(int i=s_xiaoshu.size();i<=a;++i){
					printf("0");
				}
			}else{
				s_xiaoshu.insert(a+1,".");
				cout<<s_xiaoshu;
			}	
		}else{
			stringstream ss;
			
			ss<<s_zhishu;
			int a;ss>>a;
		//	cout<<s_zhishu<<" "<<a<<endl;
			printf("0.");
			
			for(int i=0;i<a-1;++i){
				printf("0");
			}
			cout<<s_xiaoshu;
		}
	}else{
		printf("-");
		if(c=='+'){
			stringstream ss;
			ss<<s_zhishu;
			int a;ss>>a;
		//	cout<<s_zhishu<<" "<<a<<endl;
			if(a>=s_xiaoshu.size()){
				cout<<s_xiaoshu;
				for(int i=s_xiaoshu.size();i<=a;++i){
					printf("0");
				}
			}else{
				s_xiaoshu.insert(a+1,".");
				cout<<s_xiaoshu;
			}	
		}else{
			stringstream ss;
			ss<<s_zhishu;
			int a;ss>>a;
			printf("0.");
			for(int i=0;i<a-1;++i){
				printf("0");
			}
			cout<<s_xiaoshu;
		}
	}
	return 0;
}

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦