PAT(A) - 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*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.

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 10100, 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.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.

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

思路分析:这道题的意思是判断两个数字用科学计数法表示是不是相等的。相等输出转化后的值,不相等分别输出转换后的值。

算法过程:

    1.用字符串表示输入的数字。直接用long double等会溢出。

    2.因为要转换成科学计数法的表示形式,所以先找到第一个有效数字的位置。(从左往右数第一个不为0的位置)比如0.00123第一个有效数字是1;5.234第一个有效数字是5。把第一个有效位置记录到ivalIndex,如果没有找到有效数字,那么输入的这个数字就是0或0.0000之类的,ivalIndex = str.size();

    3.找到小数点的位置pointIndex,如果没有小数点,就将小数点的位置安排到pointIndex = str.size();

    4.从ivalIndex开始,填充有效数字,如果不够,就在最后的位置补0

    5.计算指数e = pointIndex - ivalIndex; 如果e小于0,执行e++,要不然结果不对(举个例子试一下就知道了)

    6.然后拼接字符串,作为返回结果。

另外注意:我刚开始用的itoa函数,在自己电脑上可以编译通过,提交到PAT过不去,提示编译错误,itoa未定义。加了好几个头文件也不管用。可能是这个函数被废除了吧。

C++最好还是用地道的stringstream比较好。


#include <iostream>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <cstdio>
#include <sstream>

using namespace std;

string change( int n, string str ) {
    string ans = "0.";      // ans保存最后返回的结果
    int len = str.size();

    int i;
    int ivalIndex = -1;     // 第一个有效数字的位置,初始化-1
    int pointIndex = -1;    // 小数点的位置,初始化-1
    int e;                  // 指数

    // 查找第一个有效数字的位置
    for( i = 0; i < len; i++ ) {
        if( str[i] != '0' && str[i] != '.' ) {
            ivalIndex = i;
            break;
        }
    }

    // 查找小数点的位置
    for( i = 0; i < len; i++ ) {
        if( str[i] == '.' ) {
            pointIndex = i;
            break;
        }
    }

    // 如果没有找到小数点,那么将pointIndex的位置放到字符串末尾
    if( pointIndex == -1 ) pointIndex = len;

    // 如果没找到有效数字,说明这个数字是“0”,说明最后应该返回0.000*10^0这种形式
    if( ivalIndex == -1 ) { // 令ivalIndex和pointIndex相等
        ivalIndex = pointIndex;
    }

    e = pointIndex - ivalIndex; // 计算指数的大小,用两个位置相减
    if( e < 0 ) e++;            // 如果指数是负数,让其加一,不然结果不对

    int c = 0;                  // c是有效数字的个数

    // 填充有效数字
    for( ; ivalIndex < len && c < n; ivalIndex++ ) {
        if( str[ivalIndex] == '.' ) continue;
        ans += str[ivalIndex];
        c++;
    }

    // 如果已经遍历完了字符串,有效数字还没填充完,就在末尾补0
    while( c < n ) {
        ans += "0";
        c++;
    }
    /*
    char se[10];
    itoa( e, se, 10 );
    */
    // 将int型指数转换成字符串
    stringstream se;
    se << e;
    ans = ans + "*10^" + se.str();
    return ans;
}

int main() {
    int n;
    string a, b;
    cin >> n >> a >> b;

    string ansA = change( n, a );
    string ansB = change( n, b );

    if( ansA == ansB ) {
        cout << "YES" << " " << ansA;
    }
    else {
        cout << "NO" << " " << ansA << " " << ansB;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值