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); } } }