• PAT-1060 Are They Equal (科学计数法)


    1060. Are They Equal 


    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

    题目大意:给出两个数,要求判断两数在保留小数点后n位并以科学计数法表示时是否相等。


    主要思想:该题的核心步骤主要是找出一个数的小数点位置(没有则设置为字符串长度),第一个有效数位置(非0和非小数点),然后根据这两个值计算出指数值。在比较的时候应分两部分,底数和指数,都相等时才相等,其中底数的小数点后n位部分存于另一目标数组。(注意:诸如0.000的为特殊形式,有效位置等于字符串长度,此时应把指数设为0,防止出现0.00与0.000不等的错误)

    #include <cstdio>
    #include <string.h>
    void print_str(char * s, int n, int count);
    int main(void) {
        int n, i;
        char s1[105], s2[105], res1[105], res2[105];
        
        scanf("%d %s %s", &n, s1, s2);
        int count1 = strlen(s1), count2 = strlen(s2);       //小数点位置
        int p = 0, q = 0;                                   //第一个有效数字的位置
        
        //分别计算两个数的 小数点位置,首个有效数位置,幂指数
        for (i = 0; i < strlen(s1); i++) {
            if (s1[i] == '.') {
                count1 = i;
                break;
            }           
        }
        for (i = 0; i < strlen(s2); i++) {
            if (s2[i] == '.') {
                count2 = i;
                break;
            }            
        }
        while (s1[p] == '0' || s1[p] == '.')    p++;
        while (s2[q] == '0' || s2[q] == '.')    q++;
        int k1 = count1 - p;                                //k1, k2 表示指数
        if (k1 < 0)             k1++;
        int k2 = count2 - q;
        if (k2 < 0)             k2++;
        if (p == strlen(s1))    k1 = 0;                     //0.000.. 的情况
        if (q == strlen(s2))    k2 = 0;
        
        //将两字符串的n位 底数部分 复制到结果数组
        int index1 = 0, index2 = 0;
        while (index1 < n) {
            if (p < strlen(s1) && s1[p] != '.')
                res1[index1++] =  s1[p];
            else if (p >= strlen(s1))
                res1[index1++] = '0';
            p++;
        }
        res1[index1] = '';
        while (index2 < n) {
            if (q < strlen(s2) && s2[q] != '.')
                res2[index2++] =  s2[q];
            else if (q >= strlen(s2))
                res2[index2++] = '0';
            q++;
        }
        res2[index2] = '';
        if (!strcmp(res1, res2) && k1 == k2)
            printf("YES 0.%s*10^%d
    ", res1, k1);
        else
            printf("NO 0.%s*10^%d 0.%s*10^%d
    ", res1, k1, res2, k2);
            
        return 0;
    }


  • 相关阅读:
    字典树略解
    NOIP2018普及组初赛解题报告
    Codeforces 23A You're Given a String...
    远程消息推送的简单方法
    IOS5,6,7的新特性
    面试问题1
    IOS推送消息的步骤
    C面试问题
    label的自适应文本,让文本自适应
    TCP连接的三次握手,TCP/UDP区别联系,socket连接和http连接的区别
  • 原文地址:https://www.cnblogs.com/zhayujie/p/7534848.html
Copyright © 2020-2023  润新知