STL_A1060 Are They Equal (25 分)

https://pintia.cn/problem-sets/994805342720868352/problems/994805413520719872

#include<iostream>
using namespace std;
#include<cstdio>
#include<string>

int n;

string machine(string s,int &e) {
    int k=0; //字符数组s的下标

    //去掉s的前导零
    while(s.length()>0&&s[0]=='0') {
        s.erase(s.begin());
    }
    //去掉s的前导零后是小数点,则说明s是小于1的数
    if(s[0]=='.') {
        s.erase(s.begin());
        while(s.length()>0&&s[0]=='0') {
            s.erase(s.begin());
            e--; //每去掉一个0,指数e减1
        }
    }else {
        while(k<s.length()&&s[k] != '.') {
            k++;
            e++;
        }if(k<s.length()) {
            s.erase(s.begin()+k); //遇到小数点,把它删除
        }
    }if(s.length()==0) {
        e=0;
    }

    int num=0;
    k=0;
    string res;
    while(num<n) {
        if(k<s.length()) res+=s[k++]; //把数字加到res末尾
        else res+='0'; //在res末尾加0
        num++; //精度加1
    }
    return res;
}

int main() {
    string s1,s2,es1,es2;
    cin>>n>>s1>>s2;

    int e1=0,e2=0;
    es1=machine(s1,e1);
    es2=machine(s2,e2);

    if(es1==es2&&e1==e2) {  //注意相等 ==
        cout<<"YES 0."<<es1<<"*10^"<<e1<<endl;
    }else {
        cout<<"NO 0."<<es1<<"*10^"<<e1<<" 0."<<es2<<"*10^"<<e2<<endl;
    }
    return 0;
}

 

猜你喜欢

转载自www.cnblogs.com/zengzhaojin1024/p/11364989.html
0条评论
添加一条新回复