1060. Are They Equal 解析

这个题目考虑到的情况挺多的感觉。

首先科学记数法0.X 除了0以外X都不为零。

然后输入的数据也是很怪。。需要考虑进去。。

比如 0.001 这样的,小数但是小数点后面不是有效数而是0的。

再比如 00100 这样的,整数但是前面有0的。(被坑死。。。当时怎么都查不出来)

再再比如0 应该记为 0.00……0*10^0..

还有精确位数为4 数字为120这样的 还需要补0.

都考虑到了应该就好了。

补一个case3: 1 0.01 0.009

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <set>

using namespace std;

int n;
struct node {
	string num;
	string ans;
	int dec;
	int fac;
	int e;
	node(){ans="0.";dec = 0;fac =0;e =0;};
};
node a,b;

string int2str(int n){
	if(n == 0)
		return "0";
	string t;
	while(n!=0){
		t.push_back('0'+ n%10);
		n/=10;
	}
	t.reserve();
	return t;
}


void str_cov(node & n1){
	int dec = 0 ,fac = 0 , e = 0;
	int c = 0;

	
		

	if(n1.num == "0"){
		while(c<n){
			n1.ans.push_back('0');
			c++;
		}
	}
	else{		
		int i = 0;
		if(n1.num.size() > 2 && n1.num[0] =='0' && n1.num[1] != '.')
			while(n1.num[i] == '0')
				i++;

		if(n1.num.size() > 2 && n1.num[0] =='0' && n1.num[1] == '.'){//0.0X型的小数
			i = 2;
			while(n1.num[i] == '0'){
				e--;i++;
			}
			while(c < n ){
				if(i < n1.num.size()){
					n1.ans.push_back(n1.num[i]);
					i++;
				}
				else//补0
					n1.ans.push_back('0');
				c++;
			}
		}

		else{
			while(i < n1.num.size() && n1.num[i] != '.'){
				if(c<n)
					n1.ans.push_back(n1.num[i]);
				e++;c++;i++;
			}
			if(i < n1.num.size() && n1.num[i] == '.')
				i++;

			while(c < n){//小数部分还有
				if(i < n1.num.size()){
					n1.ans.push_back(n1.num[i]);
					i++;				
				}
				else
					n1.ans.push_back('0');
				c++;
			}
		}
	}

	n1.ans += "*10^";
	if(e < 0){
		n1.ans.push_back('-');
		e = -e;
	}
	n1.ans += int2str(e);
	
}

int main(){

	cin >> n >> a.num >> b.num;

	str_cov(a);
	str_cov(b);

	if(a.ans != b.ans)
		cout << "NO " << a.ans << " " << b.ans << endl;
	else
		cout << "YES " << a.ans << endl;
	
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值