【java 大数】hdu java 大数

34 篇文章 1 订阅
7 篇文章 0 订阅


HDU 1002 A + B Problem II

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.text.DecimalFormat;
import java.util.Scanner;

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

HDU 1047 Integer Inquiry

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {
	public static void main(String[] agrs) {
		Scanner hehe = new Scanner(System.in);
		while (hehe.hasNextInt()) {
			BigInteger a, b;
			BigInteger zero = new BigInteger("0");
			int t, i;
			t = hehe.nextInt();
			i = 1;
			while (i <= t) {
				b = zero;
				while (true) {
					a = hehe.nextBigInteger();
					if (a.compareTo(zero) == 0)
						break;
					b = b.add(a);
				}
				System.out.println(b);
				if (i < t)
					System.out.println("");
				i++;
			}
		}
	}
}

HDU 1042 N!

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {
	public static void main(String[] agrs) {
		Scanner hehe = new Scanner(System.in);
		while (hehe.hasNextInt()) {
			BigInteger one = new BigInteger("1");  
			BigInteger i, sum, n;
			i = new BigInteger("1");
			sum = new BigInteger("1");
			n = hehe.nextBigInteger();
			int cmp = i.compareTo(n);
			while (cmp <= 0) {
				sum = sum.multiply(i);
				i = i.add(one);
				cmp = i.compareTo(n);
			}
			System.out.println(sum);
		}
	}
}


HDU 1063 Exponentiation

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {
	public static void main(String[] agrs) {
		Scanner jx = new Scanner(System.in);
		int n, i;
		BigDecimal r, a;
		BigDecimal one = new BigDecimal("1");
		while (jx.hasNextBigDecimal()) {
			r = jx.nextBigDecimal();
			n = jx.nextInt();
			a = one;
			for (i = 0; i < n; i++)
				a = a.multiply(r);
			a = a.stripTrailingZeros(); // BigDecimal stripTrailingZeros()
										// 返回数值上等于此小数,
										// 但从该表示形式移除所有尾部零的 BigDecimal
			String str = a.toPlainString(); // String toPlainString() 返回不带指数字段的此
											// BigDecimal 的字符串表示形式。
											// 通俗来讲就是直接显示,不用科学计数法表示。
			if (str.startsWith("0."))
				str = str.substring(1); // 返回新字符串,此字符串的子字符串该,子字符串从指定索引处字符开始直此字符串末尾
			System.out.println(str);
		}
	}
}

HDU 1316 How Many Fibs?

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {
	public static void main(String[] agrs) {
		Scanner hehe = new Scanner(System.in);
		BigInteger f[] = new BigInteger[501];
		BigInteger zero = new BigInteger("0");
		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 (hehe.hasNextBigInteger()) {
			BigInteger a = hehe.nextBigInteger();
			BigInteger b = hehe.nextBigInteger();
			if (a.compareTo(zero) == 0 && b.compareTo(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);
		}
	}
}

HDU 1715 大菲波数

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {
	public static void main(String[] agrs) {
		Scanner hehe = new Scanner(System.in);
		BigInteger f[] = new BigInteger[1001];
		BigInteger zero = new BigInteger("0");
		f[1] = new BigInteger("1");
		f[2] = new BigInteger("1");
		for (int i = 3; i <= 1000; i++)
			f[i] = f[i - 1].add(f[i - 2]);
		while (hehe.hasNextInt()) {
			int n = hehe.nextInt();
			for (int i = 0; i < n; i++) {
				int x = hehe.nextInt();
				System.out.println(f[x]);
			}
		}
	}
}

HDU 1753 大明A+B

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {
	public static void main(String[] agrs) {
		Scanner hehe = new Scanner(System.in);
		while (hehe.hasNextBigDecimal()) {
			BigDecimal a = hehe.nextBigDecimal();
			BigDecimal b = hehe.nextBigDecimal();
			BigDecimal c = a.add(b);
			c = c.stripTrailingZeros();
			String ss = c.toPlainString();
			if (ss.startsWith("0."))
				ss = ss.substring(1);
			System.out.println(ss);
		}
	}
}


转自HERE

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值