zoukankan      html  css  js  c++  java
  • 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);
            }
        }
    }
  • 相关阅读:
    Json对象和Json字符串
    主界面的构造函数报错
    WPF DataGrid绑定的数据源更新后界面信息不更新
    WPF 数据绑定 ( 经验 )
    WPF 简单快速地去掉Button控件的边框
    字节数组与字符串形式的数字(序列号)之间的相互转换
    将十进制数字转换为字节数组
    WPF 调用线程无法访问此对象,因为另一个线程拥有该对象。
    cmd中一些命令
    Notepad++运行快捷键的设置
  • 原文地址:https://www.cnblogs.com/littlepage/p/12264673.html
Copyright © 2011-2022 走看看