1060. Are They Equal (25)

很蛋疼的题目,说的很不清楚,调了很久,各种特殊的格式没有说清楚,
附在网上找到的特殊数的处理方式
各种要求的计数格式:(有效位数 原数 要求的计数格式)
3 0 0.000*10^0
5 0.000000 0.00000*10^0
3 0.1 0.1*10^0
4 0.000010100 0.1010*10^-4
5 1.000201 0.10002*10^1

#include<iostream>
#include<string>
#include<string.h>
using namespace std;

class digit{
private:
    char a[101];
    int pow;
    bool iszero;
public:
    digit(char *str = "NULL", int n = 0) :pow(n){
        strcpy(a, str);
    }
    void Initial(int sign){
        string b;
        b = a;
        int at, bt = 0;
        pow = 0;
        while (bt < b.size() && b[bt] == '0'){
            bt++;
        }
        if (bt<b.size() && b[bt] != '.'){
            a[0] = '0';
            a[1] = '.';
            at = 2;
            while (bt < b.size() && b[bt] != '.'){
                a[at++] = b[bt++];
                pow++;
            }
            bt++;
            while (bt < b.size()){
                a[at++] = b[bt++];
            }
            while (at < sign + 2){
                a[at++] = '0';
            }
            a[at] = '\0';
        }
        else {
            at = 2;
            a[0] = '0';
            a[1] = '.';
            bt++;
            while (bt < b.size() && b[bt] == '0'){
                pow--;
                bt++;
            }
            if (bt >= b.size())
                pow = 0;
            while (bt < b.size()){
                a[at++] = b[bt++];
            }
            while (at < sign + 2){
                a[at++] = '0';
            }
            a[at] = '\0';
        }
    }

    bool IsSame(digit &b, int &sign){
        if (pow != b.pow)
            return false;
        int count = 0, i;
        for (i = 2; i < strlen(a) && i < strlen(b.a) && count < sign; i++, count++)
            if (a[i] != b.a[i])
                break;
        if (count == sign)
            return true;
        else return false;
    }
    void show(int sign){
        int i;
        bool flag = false;
        cout << "0.";
        for (i = 2; i < sign + 2 && i < strlen(a); i++){
            cout << a[i];
            if (a[i] != '0')
                flag = true;
        }
        if (flag){
            while (i < sign + 2){
                cout << '0';
                i++;
            }
            cout << "*10^" << pow;
        }
        else {
            while (i < sign + 1){
                cout << '0';
                i++;
            }
            cout << "*10^" << pow;
        }
    }
    friend istream & operator>>(istream &is, digit &b){
        is >> b.a;
        return is;
    }
};
int main(){
    freopen("1.in", "r", stdin);
    digit a, b;
    int n;
    cin >> n;
    cin >> a >> b;
    a.Initial(n);
    b.Initial(n);
    bool flag = a.IsSame(b, n);
    if (flag){
        cout << "YES" << ' ';
        a.show(n);
        cout << endl;
    }
    else {
        cout << "NO" << ' ';
        a.show(n);
        cout << ' ';
        b.show(n);
        cout << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值