PAT甲级 1060 Are They Equal (25 分)

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×10​5​​ 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.

Input Specification:

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 10​100​​, and that its total digit number is less than 100.

Output Specification:

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.d[1]...d[N]*10^k (d[1]>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.

Sample Input 1:

3 12300 12358.9

Sample Output 1:

YES 0.123*10^5

Sample Input 2:

3 120 128

Sample Output 2:

NO 0.120*10^3 0.128*10^3

才开始读错题了,详情看注释,大体分为0(值等于0)、0.00..(值为0.000几)、1.23(小数点前面不为0)这三种情况,详情看注释。

 

#include <bits/stdc++.h>
#include <string>
#define ll long long
using namespace std;
string sa,sb;
int n;
string change(string s)
{
    string ans="0.";//初始化
    //dot代表小数点的下标
    //lla代表当为0.000..这样的数时,第一个非0数字的位置
    //e代表科学计数法时,10^的多少次方
    //is0代表这个是不是0或0.000000,即数值为0
    int dot=-1,lla=-1,e;
    bool is0=0,flag=0;
    //flag代表是不是0.00...
    for(int i=0;i<=s.size();i++)
        if(s[i]=='.')
        {
            dot=i;
            break;
        }
    if(dot==-1)
    {
        dot=s.size();
        //判断是不是整数0
        if(s.size()==1&&s[0]=='0')
            is0=1;//为0
        else//是别的整数加上小数点,假设为a,变为a.00000
            s+='.';
    }
    else if(dot==1&&s[0]=='0')
        flag=1;//为0.
    //是0直接加上n个0,直接返回
    if(is0)
    {
        for(int i=1;i<=n;i++)
            ans+='0';
        ans+="*10^0"; 
        return ans;
    } 
    if(flag)
    {
        for(int i=2;i<s.size();i++)
        {
            if(s[i]!='0')
            {
                lla=i-1;
                break;
            }     
        }
        if(lla==-1)
            is0=1;
        //是0,直接加上n个0
        if(is0)
        {
            for(int i=1;i<=n;i++)
                ans+='0';
            ans+="*10^0"; 
            return ans;
        }
        //否则,一定是10^-e次方
        e=lla-1;
        //在最后加上足够多的0,方便处理  
        while(s.size()<205) s+='0';
        //加上n位有效数字
        for(int i=1;i<=n;i++)
            ans+=s[lla+i];
        ans+="*10^";
        string ss="";
        //指数为0时,特别处理,不过应该没有这种情况
        if(e==0) ss="0";
        //将整数转为字符串
        else
		{
			ans+="-";
			while(e)
	        {
	            ss+=(e%10)+'0';
	            e/=10;
	        }
			
		} 
        //记得反转
        reverse(ss.begin(),ss.end());
         
        ans+=ss;
    }
    else
    {
        while(s.size()<205) s+='0';
        bool flag1=0;
        //这种情况下,dot值还代表小数点之前有多少位数
        //如果要求保留的有效数字大于dot,那么要在小数点后面再取一位数
        int N =(dot>=n?n:n+1);
        for(int i=0;i<N;i++)
        {
            if(s[i]!='.')//不是小数点才取
                ans+=s[i];
        }
        ans+="*10^";
        //同0.000...的情况
        string ss="";
        if(dot==0) ss="0";
        while(dot)
        {
            ss+=(dot%10)+'0';
            dot/=10;
        }
        reverse(ss.begin(),ss.end());
        ans+=ss;
    }
    return ans;
}
  
  
int main(void)
{
    //freopen("out.txt","w",stdout);
    string s1,s2;
    cin>>n>>s1>>s2;
    //去除前导0
    while(s1.size()>1)
    {
        if(s1[0]=='0'&&s1[1]!='.')
            s1.erase(s1.begin());
        else
         break;
    }
    //cout<<s1<<endl;
    sa=change(s1);
     
    while(s2.size()>1)
        if(s2[0]=='0'&&s2[1]!='.')
            s2.erase(s2.begin());
        else
			break;
    //cout<<s2<<endl;  
    sb=change(s2);
    if(sa==sb)
    {
        printf("YES ");
        for(int i=0;i<sa.size();i++)
            printf("%c",sa[i]);
    }
    else
    {
        printf("NO ");
        for(int i=0;i<sa.size();i++)
            printf("%c",sa[i]);
        printf(" ");  
        for(int i=0;i<sb.size();i++)
            printf("%c",sb[i]);
    }
    return 0;
}
/*
2 010 010.2
*/
/*
99 0 0.000
*/
/*
YES 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^0
*/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值