首页 > 其他分享 >Are They Equal (25)

Are They Equal (25)

时间:2022-10-24 02:22:05浏览次数:36  
标签:25 string int position1 Equal equal position2 numbers They

题目描述

If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0.123*105 with simple chopping.  Now given the number of significant digits on a machine and two float numbers, you are supposed to tell if they are treated equal in that machine.

输入描述:

Each input file contains one test case which gives three numbers N, A and B, where N (<100) is the number of significant digits, and A and B are the two float numbers to be compared.  Each float number is non-negative, no greater than 10100, and that its total digit number is less than 100.


输出描述:

For each test case, print in a line "YES" if the two numbers are treated equal, and then the number in the standard form "0.d1...dN*10^k" (d1>0 unless the number is 0); or "NO" if they are not treated equal, and then the two numbers in their standard form.  All the terms must be separated by a space, with no extra space at the end of a line.
Note: Simple chopping is assumed without rounding.

输入例子:

3 12300 12358.9

 


输出例子:

YES 0.123*10^5

tip:题意比较简单,但情况比较多,需要理清才能做,一开始好多特殊情况都没想到,一定要仔细

#include<bits/stdc++.h>
using namespace std;
int n;
int p_position1=-1; //a的小数点位置
int p_position2=-1; //b的小数点位置
int v_position1=-1; //a的第一位有效数字位置
int v_position2=-1; //b的第一位有效数字位置
string a,b;
void find_index(string a,string b){
    int s1=a.length(),s2=b.length();
    int mmax=max(s1,s2);
    for (int i=0;i<mmax;++i){
        if (i<s1){
            if (a[i]=='.')
                p_position1=i;
            if (a[i]!='0'&&a[i]!='.'&&v_position1==-1)
                v_position1=i;
        }
        else
            a.push_back('0');
    }
    if (p_position1==-1){//表示为正整数
        p_position1=s1;
    }

    for (int i=0;i<mmax;++i){
        if (i<s2){
            if (b[i]=='.')
                p_position2=i;
            if (b[i]!='0'&&b[i]!='.'&&v_position2==-1)
                v_position2=i;
        }
        else
            b.push_back('0');
    }
    if (p_position2==-1)
        p_position2=s2;
}
string output(string a,int p_position,int v_position){
    string result="0.";
    if (v_position!=-1){ //该数不为0
        int length=a.length();
        for (int i=v_position;i<v_position+n;++i){
            if (i<length)
                result.push_back(a[i]);
            else
                result.push_back('0');
        }
        result.append("*10^");
        if (p_position1<v_position1)//该数为纯小数
            result.append(to_string(p_position1-v_position1+1));
        else
            result.append(to_string(p_position1-v_position1));
    }
    else{
        for (int i=0;i<n;++i)
            result.push_back('0');
        result.append("*10^0");
    }
    return result;
}
int main(){
    cin>>n>>a>>b;
    find_index(a,b);
    string result1=output(a,p_position1,v_position1);
    string result2=output(b,p_position2,v_position2);
    if (result1==result2){
        cout<<"YES "<<result1;
    }
    else{
        cout<<"NO "<<result1<<" "<<result2;
    }
}

 

标签:25,string,int,position1,Equal,equal,position2,numbers,They
From: https://www.cnblogs.com/coderhrz/p/16820231.html

相关文章