PAT 甲级 A1060 (2019/02/22)

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int n;
string Deal(string S, int &e){
    int k = 0;  //s的下标
    while(S.length() > 0 && S[0] == '0'){
        S.erase(S.begin());     //去掉S的前导0 
    }
    if(S[0] == '.'){        //若去掉前导0后是小数点,则说明S是小于1的学生 
        S.erase(S.begin());     //去掉小数点
        while(S.length() > 0 && S[0] == '0'){
            S.erase(S.begin()); //去掉小数点后面非零位前的所有零 
            e--;    //每去掉一个零,指数e减1      
        } 
    }else{      //若去掉前导0后不是小数点,则找到后面的小数点删除 
        while(k < S.length() && S[k] != '.'){   //寻找小数点 
            k++;
            e++;    //只要不遇到小数点,就让指数e++ 
        }
        if(k < S.length()){ //while结束后k<S.length(),说明遇到了小数点 
            S.erase(S.begin() + k);     //删除小数点 
        } 
    }
    if(S.length() == 0){
        e = 0;      //如果去掉前导0后S的长度为0,则这个数为0 
    }
    int num = 0;    //
    k = 0;
    string res;
    while(num < n){     //只要精度还没到n 
        if(k < S.length())  
            res +=S[k++];   //只要还有数字,就加到res末尾 
        else 
            res += '0';     //否则res末尾添加0 
        num++;      //精度加1 
    }
    return res;
} 
int main(){
    string S1, S2, S3, S4;  
    cin >> n >> S1 >> S2;
    int e1 = 0, e2 = 0;     //e1 e2为S1 S2的指数 
    S3 = Deal(S1, e1);
    S4 = Deal(S2, e2);
    if(S3 == S4 && e1 == e2)        //若主体&指数相同,则输出“YES” 
        cout << "YES 0." << S3 << "*10^" << e1 << endl;      
    else            //这里对于YES,NO应该是字母全部大写
        cout << "NO 0." << S3 << "*10^" << e1 << " 0." << S4 << "*10^" << e2 << endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/zjsaipplp/p/10425225.html