Java大数处理-HDU(1042、1250)

题目(1042):

N!

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 95064    Accepted Submission(s): 28266

Problem Description

Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! 

Input

One N in one line, process to the end of file.

Output

For each N, output N! in one line.

Sample Input

1

2

3

Sample Output

1

2

6

解题思路:

本题的意思就是求N的阶乘,看起来简单但事实上用C语言写的话很麻烦,10000!超出了int的范围,用Java中的BigInteger来做就十分的方便。

代码:

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

public class Main{

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
             String str= sc.next();
             BigInteger n=new BigInteger(str);
             BigInteger ans = new BigInteger("1");
             BigInteger one = new BigInteger("1");
             BigInteger zero = new BigInteger("0");
             while(!n.equals(zero)) {
                 ans = ans.multiply(n);
                 n = n.subtract(one);
             }
          System.out.println(ans);
        }
    }
}

题目(1250):

Hat's Fibonacci

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13400    Accepted Submission(s): 4508

Problem Description

A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1.
F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4)
Your task is to take a number as input, and print that Fibonacci number.

Input

Each line will contain an integers. Process to end of file.

Output

For each case, output the result in a line.

Sample Input

100

Sample Output

4203968145672990846840663646

Note:

No generated Fibonacci number in excess of 2005 digits will be in the test data, ie. F(20) = 66526 has 5 digits.

解题思路:

本题思路也是很简单的,根据规律列计算式,同样是属于Java大数类型。

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

public class Main{

    public static void main(String[] args) {
         Scanner scanner=new Scanner(System.in);
         while(scanner.hasNext()){
             int a=scanner.nextInt();
             BigInteger[] num=new BigInteger[10000];
             num[1]=BigInteger.ONE;
             num[2]=BigInteger.ONE;
             num[3]=BigInteger.ONE;
             num[4]=BigInteger.ONE;
             for(int i=5;i<=a;i++)
                    num[i]=(num[i-1]).add(num[i-2]).add(num[i-3]).add(num[i-4]);
                System.out.println(num[a]);
         }
    }
}

Java中大数用法:

1、new一个大数对象:
BigInteger a=new BigInteger(“123”); //第一种,参数是字符串
BigInteger a=BigInteger.valueOf(123); //第二种,参数可以是int、long

2、大数的四则运算
a. add(b); //加法
a.subtract(b); //减法
a.divide(b); //除法
a.multiply(b); //乘法

3、大数比较大小
a.equals(b); //如果a、b相等返回true否则返回false
a.compareTo(b); //a小于b返回-1,等于返回0,大于返回1

4、求大数的位数
//先转换成字符串在求字符串的长度
a.toString().length(); 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值