Java-高精度

1002
http://acm.hdu.edu.cn/showproblem.php?pid=1002

import java.io.*;
import java.util.Scanner;
import java.math.BigInteger;

public class Main
{    
    public static void main(String[] args) 
    {
         Scanner cin = new Scanner(System.in);
         while (cin.hasNextInt())
         {
             BigInteger a,b,c;
             int t;
             t = cin.nextInt();
             for(int i=1;i<=t;i++)
             {
                 a = cin.nextBigInteger();
                 b = cin.nextBigInteger();
                 c = a.add(b);
                 System.out.println("Case "+ i +":");
                 System.out.println(a+" + "+b+" = "+c);
                 if(i<t) System.out.println("");
             }
         }
    }
}

1042
http://acm.hdu.edu.cn/submit.php?pid=1042

import java.io.*;
import java.util.Scanner;
import java.math.BigInteger;

public class Main
{   
    public static void main(String[] args) 
    {
         Scanner cin = new Scanner(System.in);
         while (cin.hasNextInt())
         {
            int t;
            t = cin.nextInt();
            BigInteger sum = BigInteger.ONE;
            for(int i=1;i<=t;i++)
                sum = sum.multiply(BigInteger.valueOf(i));
            System.out.println(sum);
         }
    }
}

1047
http://acm.hdu.edu.cn/showproblem.php?pid=1047

import java.io.*;
import java.util.Scanner;
import java.math.BigInteger;

public class Main
{    
    public static void main(String[] args) 
    {
         Scanner cin = new Scanner(System.in);
         while (cin.hasNextInt())
         {
            int t;
            BigInteger sum,tmp;
            t = cin.nextInt();
            for(int i=1;i<=t;i++)
            {
                sum = BigInteger.ZERO;
                boolean ok = true;
                while (ok)
                {
                    tmp = cin.nextBigInteger();
                    if (tmp.compareTo(BigInteger.ZERO) == 0)
                        ok= false;
                    sum = sum.add(tmp);
                }
                System.out.println(sum.toString());
                if (i < t) System.out.println("");
            }
         }
    }
}

1063
http://acm.hdu.edu.cn/showproblem.php?pid=1063

import java.io.*;
import java.util.Scanner;
import java.math.BigInteger;
import java.math.BigDecimal;  

public class Main
{    
    public static void main(String[] args) 
    {
         Scanner cin = new Scanner(System.in);
         while (cin.hasNextBigDecimal())
         {
            BigDecimal r, ans;
            int n;
            r = cin.nextBigDecimal();
            n = cin.nextInt();

            ans = BigDecimal.ONE;
            for(int i=1;i<=n;i++)
            {
                ans = ans.multiply(r);
            }
            ans = ans.stripTrailingZeros();
            String str = ans.toPlainString();
            if (str.startsWith("0."))
                str = str.substring(1);
            System.out.println(str);
         }
    }
}

1316
http://acm.hdu.edu.cn/showproblem.php?pid=1316

import java.io.*;
import java.util.Scanner;
import java.math.BigInteger;
import java.math.BigDecimal;  

public class Main
{   
    public static void main(String[] args) 
    {
         Scanner cin = new Scanner(System.in);

         BigInteger f[] = new BigInteger [505];
         f[1] = new BigInteger("1");
         f[2] = new BigInteger("2");
         for(int i=3;i<=500;i++)
             f[i] = f[i-1].add(f[i-2]);

         while (cin.hasNextBigInteger())
         {
            BigInteger a, b;
            a = cin.nextBigInteger();
            b = cin.nextBigInteger();

            if (a.compareTo(BigInteger.ZERO) == 0 && b.compareTo(BigInteger.ZERO) == 0)
                break;

            int ans = 0;
            for(int i=1;i<=500;i++)
            {
                if (a.compareTo(f[i]) <= 0 && b.compareTo(f[i]) >= 0)
                    ans++;
                if(b.compareTo(f[i]) < 0) 
                    break;
            }
            System.out.println(ans);
         }
    }
}

1715
http://acm.hdu.edu.cn/showproblem.php?pid=1715

import java.io.*;
import java.util.Scanner;
import java.math.BigInteger;
import java.math.BigDecimal;  

public class Main
{    
    public static void main(String[] args) 
    {
         Scanner cin = new Scanner(System.in);

         BigInteger f[] = new BigInteger [10010];
         f[1] = new BigInteger("1");
         f[2] = new BigInteger("1");

         for(int i=3;i<=1005;i++)
             f[i] = f[i-1].add(f[i-2]);

         while (cin.hasNextInt())
         {
            int t,id;
            t = cin.nextInt();
            for(int i=1;i<=t;i++)
            {
                id = cin.nextInt();
                System.out.println(f[id]);
            }
         }
    }
}

1753
http://acm.hdu.edu.cn/showproblem.php?pid=1753

import java.io.*;
import java.util.Scanner;
import java.math.BigInteger;
import java.math.BigDecimal;  

public class Main
{   
    public static void main(String[] args) 
    {
         Scanner cin = new Scanner(System.in);

         BigDecimal a,b,c;

         while (cin.hasNextBigDecimal())
         {
            a = cin.nextBigDecimal();
            b = cin.nextBigDecimal();

            c = a.add(b);
            c = c.stripTrailingZeros();
            String str = c.toPlainString();
               if (str.startsWith("0."))
                   str = str.substring(1);
            System.out.println(str);
         }
    }
}
发布了535 篇原创文章 · 获赞 16 · 访问量 33万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览