zoukankan      html  css  js  c++  java
  • HDU 2054 大数比较


    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2054


            A这个水题也没有什么意思,主要巩固前面Java大数类和一些方法的学习。


           题目如果直接用Java.Biginteger.equals()方法的话会WA,在API说明里面,对于2.00,和2.0值相等,标度不等就不会匹配。

           一个方法就是用前面学到的方法stripTrailingZeros(),这里面有个组件 [BigInteger, scale], 我理解的就是比如5000.0000,使用此组件生成的就是等于[50000000,4],右边的4就是科学计数法10的负次方,小数点左移位数4,使用stripTrailingZeros(),去BigInteger参数位的0,去几个零,右边标度减几,因为50000000要去七个0,4-3=-7,生成的[5,-3],也就是5E3,这就是去0原理吧。好像是?不过现在又有问题了?还在问别人,等弄懂了回来补充。

           第二个方法是直接用compareTo()方法,在上篇比较大小的博客 http://blog.csdn.net/major_zhang/article/details/54748204 里面也说的很清楚了,对于2.00,2.0在字符串是返回大于等于1的。对于大整数比较当此 BigDecimal 在数字上小于、等于或大于 val 时,返回 -1、0 或 1。


         

    import java.math.BigInteger;
    import java.util.Scanner;
    import java.math.BigDecimal;
    
    public class Main
    {
    	public static void main(String args[])
    	{
    		Scanner in = new Scanner(System.in);
    		while(in.hasNext()){
    		BigDecimal a1,a2;
    		a1 = in.nextBigDecimal();
    		a2 = in.nextBigDecimal();
    		
    		int flag = a1.compareTo(a2);
    		/**
    		 * a = a.stripTrailingZeros();
    			b = b.stripTrailingZeros();
    			if (a.equals(b)) {
    				System.out.println("YES");
    			} else {
    				System.out.println("NO");
    			}
    		 **/
    		if(flag==0)
    			System.out.println("YES");
    		else {
    			System.out.println("NO");
    		}			
    		
    	}
    }
    }

    C/C++版本:

    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    using namespace std;
    char a[100000],b[100000];
    void change(char s[])
    {
        int i,len;
        len  = strlen(s);
        if(strstr(s,"."))//含有小数点
        {
            for(i = len-1; s[i] == '0'; i--)//去掉小数末尾的0
            {
                s[i] = '';
                len--;
            }
        }
        if(s[len-1] == '.')
            s[len-1] = '';
    }
    
    int main()
    {
        while(scanf("%s%s",a,b)!=EOF)
        {
            change(a);
            change(b);
            /*
            cout<<"a:"<<a<<endl;
            cout<<"b:"<<b<<endl;
            */
            if(strcmp(a,b))
                printf("NO
    ");
            else
                printf("YES
    ");
        }
    
        return 0;
    }



          



  • 相关阅读:
    【设计模式】责任链模式
    python 获取cookie,发送请求
    python requests报错:Exception: ('Connection broken: IncompleteRead(3794 bytes read, 4398 more expect ed)', IncompleteRea
    获取二维数组 所有的组合排列
    tp3.0 验证码生成和使用
    MySQL: mysql is not running but lock exists 的解决方法
    [RxSwift]4.2.0、Observer
    [RxSwift]4.1.6、ControlEvent
    [RxSwift]4.1.5、Signal
    [RxSwift]4.1.4、Driver
  • 原文地址:https://www.cnblogs.com/zhangmingzhao/p/7256689.html
Copyright © 2011-2022 走看看