科学计数法

#include <iostream>
#include <string.h>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <string>
using namespace std;
int n;
string deal(string str,int& e)
{
	int k=0;
	while(str.size()>0&&str[0]=='0')
	{
		str.erase(str.begin());
	}
	if(str[0]=='.') 
	{
		str.erase(str.begin());
		while(str[0]=='0'&&str.size()>0)
		{
			str.erase(str.begin());
			e--;
		}
	}
	else
	{
		while(k<str.size()&&str[k]!='.')
		{
			k++;
			e++;
		}
		if(k<str.size())
		{
			str.erase(str.begin()+k);
		}
	}
	if(str.size()==0)
	{
		e=0;
	}
	int num=0;
	k=0;
	string res;
	while(num<n)
	{
		if(k<str.size())
			res+=str[k++];
		else
			res+='0';
		num++;
	}
    return res;
}

int main()
{
	string n1,n2;
	cin >> n >> n1 >> n2;
	string s1,s2;
	int e1=0,e2=0;
	s1=deal(n1,e1);
	s2=deal(n2,e2);
	if(s1==s2&&e1==e2)
		cout << "YES 0." << s1 << "*10^" << e1 <<endl;
	else
		cout << "NO 0." << s1 << "*10^" << e1<< " 0." << s2 << "*10^" << e2;
}

数据分为两种,一种>1一种<1
1.无论那种情况,先看是否有前导0,例如00012,000.012去掉小数点前的 前 导0,遇到小数点或者第一个不为0的数停止
2.0<原数据<1:1.去掉小数点,接着看后面是否还有无效0,有的话去掉,例如:0.00012,小数点后的无效0每多一个,指数-1
原数据>1:从str.begin()开始寻找小数点,每后挪一位指数+1,找到小数点后将小数点删除
原数据=0:将零去掉后如果str,size()为0,说明原数据等于0,指数为0
3.按顺序构建res字符串,不够的0补足

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值