1060 Are They Equal (25分)

Posted d-i-p

tags:

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

1060 Are They Equal (25分)

题目

技术图片

思路

定义结构体

struct fraction{
    string f;
    int index;
}

把输入的两个数先都转换为科学计数法,统一标准后再做比较,index表示指数

注意点

  1. 0或者0的各种形式
  2. 0.1, 0.01等

代码

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<string>
using namespace std;

#define MAXN 101

struct fraction{
    string f;
    int index;
    fraction(char s[], int len){
        int pos = -1;
        int num = -1;
        int i = 0;
        for(i=0;s[i]!='';i++){
            if(num == -1 && s[i] != '0' && s[i] != '.'){
                num = i;
            }
            if(s[i] == '.'){
                pos = i;
            }
        }
        if(num == -1){
            f.append(len, '0');
            index = 0;
            return ;
        }
        if(pos != -1){
            if(pos < num){
                index = pos - num + 1;
            }else{
                index = pos - num;
            }
        }else{
            index = i - num;
        }
        f = s;
        if(pos != -1){
            f.erase(pos, 1);
            if(num > pos){
                num--;
            }
        }
        f = f.substr(num, f.length());
        if(f.length() < len){
            f.append(len-f.length(), '0');
        }else{
            f = f.substr(0, len);
        }
    }
};

void print(fraction f, int len) {
    printf("0.%s*10^%d", f.f.c_str(), f.index);
}

bool compare(fraction f1, fraction f2, int len){
    if(f1.index != f2.index){
        return false;
    }
    if(f1.f != f2.f){
        return false;
    }
    return true;
}

void toString(fraction f) {
    printf("f = %s, index = %d 
", f.f.c_str(), f.index);
}

int main() {
    int n;
    char s1[MAXN], s2[MAXN];
    scanf("%d %s %s", &n, s1, s2);
    fraction f1(s1, n);
    fraction f2(s2, n);
    if(compare(f1, f2, n) == true){
        printf("YES ");
        print(f1, n);
    } else{
        printf("NO ");
        print(f1, n);
        printf(" ");
        print(f2, n);
    }
}

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

PAT Advanced 1060 Are They Equal (25分)

A1060 Are They Equal (25 分)

1060 Are They Equal (25 分)难度: 一般 / 知识点: 模拟 字符串处理

PAT 甲级 1060 Are They Equal (25 分)(科学计数法,接连做了2天,考虑要全面,坑点多,真麻烦)

1060. Are They Equal (25)

1060. Are They Equal (25)