1073 Scientific Notation(字符串下标和字符个数的问题,多思考几次,不要乱)

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 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<iostream>
using namespace std;
int main(){
	string ab;
	cin>>ab;
	int len=0;
	for(int i=0;ab[i]!='E';i++){
		len++;
	}
	string s=ab.substr(1,len-1);//substr的格式是substr(起始位,长度)。第二个是长度,不要搞成终止位了 
	int n=stoi(ab.substr(len+1));//1.stoi;2.substr如果是复制到最后一位,长度就可以忽略不写 
	if(ab[0]=='-') cout<<"-";//这个真的很巧妙,后面就不用管正负问题了 
/*一开始我还在想,个位数要分0和非0两种情况好麻烦...后面发现科学计数法个位数不为0...实属想多了...*/
	if(n<0){//指数为负的情况 
		cout<<"0.";
		n=abs(n)-1;//小数点第一次往前移的时候前面就是“0.”,所以第一次移动的时候不需要在小数点后面加0 
		while(n--) cout<<"0";
		for(int i=0;i<len-1;i++)
			if(s[i]!='.') cout<<s[i];	
	}
	else{//指数大于等于0的情况 
		cout<<s[0];
		int i=2,cnt=n;
		for(;i<s.size()&&cnt>0;i++,cnt--) cout<<s[i];
		if(cnt==0){
			if(i<s.size()) cout<<".";
//这个情况真的很刁钻,就是i=s.size()和cnt=同时成立,这样你就会在最后多输出一个小数点,这个测试点真的刁钻 
			for(int j=i;j<s.size();j++) cout<<s[j];
		} 
		else
			while(cnt--) cout<<"0";
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值