PAT Advanced 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 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 (<) 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 1, 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

大数题用Java解决比较简便,我们要注意的是,4和6测试点,这两个case中,

4测试点是 0.00000001和0.1,这样有没有考虑小数的测试点呢?

6测试点是0和00000.000,有没有考虑这样的数字呢?

import java.math.BigDecimal;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt(), expo1 = 0, expo2 = 0;
        BigDecimal b1 = sc.nextBigDecimal();
        BigDecimal b2 = sc.nextBigDecimal();
        BigDecimal num = new BigDecimal("1");
        BigDecimal num2 = new BigDecimal("0.1");
        BigDecimal num3 = new BigDecimal("0");
        BigDecimal ten = new BigDecimal("10");
        while(b1.compareTo(num) > 0) {
            b1 = b1.divide(ten);
            expo1++;
        }
        while(b1.compareTo(num2) < 0 && b1.compareTo(num3) > 0) {
            b1 = b1.multiply(ten);
            expo1--;
        }
        while(b2.compareTo(num) > 0) {
            b2 = b2.divide(ten);
            expo2++;
        }
        while(b2.compareTo(num2) < 0 && b2.compareTo(num3) > 0) {
            b2 = b2.multiply(ten);
            expo2--;
        }
        String s = "";
        for(int i = 0; i < N + 2; i++) s += "0";
        String sub1 = b1.toString(), sub2 = b2.toString();
        if(!b1.toString().contains(".")) sub1 += ".";
        if(!b2.toString().contains(".")) sub2 += ".";
        sub1 = (sub1 + s).substring(0, N + 2);
        sub2 = (sub2 + s).substring(0, N + 2);
        if(expo1 == expo2 && sub1.equals(sub2)) {
            System.out.printf("YES %s*10^%d", sub1, expo1);
        }
        else {
            System.out.printf("NO %s*10^%d %s*10^%d", sub1, expo1, sub2, expo2);
        }
    }
}

原文地址:https://www.cnblogs.com/littlepage/p/12264673.html

时间: 2024-10-08 05:07:34

PAT Advanced 1060 Are They Equal (25分)的相关文章

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

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 with simple chopping. Now given the number of significant digits on a machine and two

PAT Advanced 1013 Battle Over Cities (25分)

It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of

PAT:1060. Are They Equal (25) AC

#include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std; int n; string deal(string s,int &e) //[思维]1:吸收多余0:.2:找到“.”判断与1的大小.3:去除“.”的同时统计10的指数(正负与step2有关) { //4:判断是否删完了,删完了表明数字是0.5:存入前n个数字,不足用

PAT Advanced 1009 Product of Polynomials (25分)

This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N?1?? a?N?1???? N?2?? a?N?2?

PAT Advanced 1086 Tree Traversals Again (25分)

An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop()

PAT Advanced 1012 The Best Rank (25分)

To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algrbra), and E - English. At the mean time, we encourage students by e

PAT 1060. Are They Equal (25)

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 a

PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)

1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given Is PAT&TAP symmetric?, the longest symmetric sub-string is s PAT&TAP s, hence you must output 11. In

pat 1013 Battle Over Cities(25 分) (并查集)

1013 Battle Over Cities(25 分) It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any othe