A1073/B1024

 Scientific Notation (20分)

单词:

  • scientific notation:科学计数法   notation:符号,记号
  • trailing:尾部的
  • the fractional portion:小数部分
  • the integer portion has exactly one digit, there is at least one digit in the fractional portion
  • 整数部分有 1 小数部分至少有 1

总结:

  1. 数据范围:①数字存储≤9999 bytes,1个char变量=1 byte → 数字长度 ≤ 9999。②指数绝对值≤9999(可用int型变量)。
  2. 思路:输入部分为(小数部分)E(指数部分)。第一步定位E,再对(小数部分)(指数部分)分情况讨论。
  3. 小数部分x:确定小数点位置。
  4. 指数部分e:
  • ①e<0,小数点前移|e|位,0.0...0xxx,输出“0.”,再输出abs(e)-1个0。
  • ②e>0,小数点后移e位,x...x.x...x,输出x中e个字符,在对应位置补小数点;
  • x...x..0...0,若x输出完而仍需后移(e>0),无小数点,末尾需要补0

注意:stoi()函数:stringint

  • 示例:stoi(strin)代表将n进制的字符串str从str[i]开始的部分转化为十进制。
  • 字符串str中含非数字的字符,则只会识别从开头到第一个非法字符之前,如果第一个字符就是非法字符则会报错。
  • ∴本题用字符E之后的子串,而不能写作int e = stoi( a,pos+1,10);这种。

代码:

//A1073 B1024
#include <iostream>
using namespace std;
#include <cstring>

int main(){
	string a;
	cin>> a;
	int pos=0;
	while( a[pos]!='E' )
		pos++;
	string x = a.substr(1,pos-1);//底数x 不含符号
	int e = stoi( a.substr(pos+1) ); //指数e  a.substr(i)从下标i到结尾 
	
	if( a[0]=='-' ) cout<<"-";
	if(e<0){
		cout<<"0."; 
		for(int i=e+1; i<0; i++){
			cout <<"0";	
		}
		cout<<x[0];
		cout<<x.substr(2);
	}else{

		cout<<x[0];
		//小数部分  从x[2]开始 

		for( int i=2; i<x.length(); i++){
			if( e==0 ){
				cout<<".";
				cout<<x.substr(i);
				break;  //需要break跳出循环 否则会超时
			}
            //if( e==0 ) cout<<"."; 原来写法 测试点4超时 
			cout<<x[i]; 
			e--;	
		}
		
		while( e ){
			cout<<0;
			e--;
		}
	}
		
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值