PAT-1060. Are They Equal (25)

Posted Tobu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT-1060. Are They Equal (25)相关的知识,希望对你有一定的参考价值。

1060. Are They Equal (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

提交代码

 

这道题的坑点不少,开始只想到例子的数字,通过在牛客网上测试,一步步发现了其他不同的case,0,0.0005;000001;

基本思路就是求出两个字符串,进行比较。

c++的string可以直接通过“+”拼接,同时也可以直接拼接一个字符,可以说是非常方便的。

string的substr()函数,substr(a,b):a表示初试位置,b表示长度;

以下是代码

 

#include <bits/stdc++.h>

using namespace std;

int N;
string A, B;

int main()
{
    cin>>N>>A>>B;
    int ka = 0;
    int na = N;
    int kka = 0;
    for(int i = 0; i < A.length(); i++) {
        if(i != 0 && A[i] == . && A[i-1] == 0) {
            kka = i-1;
            break;
        }
        if(A[i] != 0) {
            kka = i;
            break;
        }
    }
    A = A.substr(kka, A.length()-kka);
    if(A[0] == 0){
        for(int i = 2; i < A.length(); i++) {
            if(A[i] != 0) {
                ka = 2-i;
                break;
            }
        }
    }
    else {
        for(int i = 0; i < A.length(); i++) {
            if(A[i] == .) {
                ka = i;
                break;
            }
        }
        if(ka == 0) {
            ka = A.length();
        }
    }
    string strA = "0.";
    int initA = 0;
    if(ka < 0) {
        initA = 2-ka;
        na += initA;
    }
    for(int i = initA; i < na; i++) {
        //cout<< "i:"<< i<< endl;
        if(i < A.length()) {
            if(A[i] == .) {
                na++;
            }
            else {
                strA += A[i];
            }
        }
        else {
            strA += "0";
        }
    }
    strA += "*10^";
    if(ka < 0) {
        strA += "-";
        ka = -ka;
        //cout<< "ka:"<< ka<< endl;
    }
    string sa;
    if(ka == 0) sa = "0";
    while(ka != 0) {
        sa += ka%10+0;
        ka /= 10;
    }
    for(int i = sa.length()-1; i >= 0; i--) {
        strA += sa[i];
    }
    //strA += ka + ‘0‘;

    int kb = 0;
    int nb = N;
    int kkb = 0;
    for(int i = 0; i < B.length(); i++) {
        if(i != 0 && B[i] == . && B[i-1] == 0) {
            kkb = i-1;
            break;
        }
        if(B[i] != 0) {
            kkb = i;
            break;
        }
    }
    B = B.substr(kkb, B.length()-kkb);
    if(B[0] == 0) {
        for(int i = 2; i < B.length(); i++) {
            if(B[i] != 0) {
                kb = 2-i;
                break;
            }
        }
    }
    else {
        for(int i = 0; i < B.length(); i++) {
            if(B[i] == .) {
                kb = i;
                break;
            }
        }
        if(kb == 0) {
            kb = B.length();
        }
    }
    string strB = "0.";
    int initB = 0;
    if(kb < 0) {
        initB = 2-kb;
        nb += initB;
    }
    for(int i = initB; i < nb; i++) {
        //cout<< "i:"<< i<< endl;
        if(i < B.length()) {
            if(B[i] == .) {
                nb++;
            }
            else {
                strB += B[i];
                //cout<< "B:"<< B[i]<< endl;
            }
        }
        else {
            strB += "0";
        }
    }
    strB += "*10^";
    if(kb < 0) {
        strB += "-";
        kb = -kb;
    }
    string sb;
    if(kb == 0) sb = "0";
    while(kb != 0) {
        sb += kb%10+0;
        kb /= 10;
    }
    for(int i = sb.length()-1; i >= 0; i--) {
        strB += sb[i];
    }
    //strB += kb + ‘0‘;

    if(strA == strB) {
        cout<< "YES "<< strA;
    }
    else {
        cout<< "NO "<< strA<< " "<< strB;
    }
    return 0;
}

 

以上是关于PAT-1060. Are They Equal (25)的主要内容,如果未能解决你的问题,请参考以下文章

1060. Are They Equal (25)

1060. Are They Equal (25)

1060. Are They Equal (25)

1060 Are They Equal (25 分)

PTA(Advanced Level)1060.Are They Equal

PAT Advanced 1060 Are They Equal (25分)