Are They Equal (25)

重点:

通过一个函数获取在有效位数内的标准形式,判断两个标准形式是否相同来判断是否相等。

判断情况:

1、在数字前有很多0,没有小数点

2、小数点前后全是0,没有有效数字

3、主要是第一个有效数字的位置与小数点位置的判断


#include "iostream"
#include "string"

#include <algorithm>
using namespace std;


int N;


string itos(int Num)
{
string s;
bool fushu = false;
if (Num < 0)
{
fushu = true;
Num = -Num;
}
else if (Num == 0)
{
return "0";
}
while (Num != 0)
{
s.push_back(Num % 10 + '0');
Num /= 10;
}
if (fushu)
s.push_back('-');
reverse(s.begin(), s.end());
return s;
}
string get_Standard(string f)
{
string zero(N, '0');
zero = "0." + zero;
auto point = f.find('.');
auto firstValid = f.find_first_not_of("0.");
int e;
if (f > zero && firstValid != string::npos)
{
if (point == string::npos)
{
e = f.size() - firstValid;
}
else
{
e = point - firstValid;
if (e < 0)
{
e += 1;
}
}
string ret = "0.";
for (int i = firstValid; ret.size() < N + 2; ++i)
{
if (i < f.size())
{
if (f[i] != '.')
ret.push_back(f[i]);
}
else
ret.push_back('0');
}
ret.append("*10^");
ret.append(itos(e));
return ret;
}
else
{
return "0." + string(N, '0') + "*10^0";
}
}


int main()
{
cin >> N;
string f1, f2;
cin >> f1>>f2;
string standard1 = get_Standard(f1);
string standard2 = get_Standard(f2);
if (standard1 == standard2)
{
cout << "YES " << standard1;
}
else
cout << "NO " << standard1 << " " << standard2;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值