【java 大数】hdu java 大数,
分享于 点击 35978 次 点评:30
【java 大数】hdu java 大数,
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
相关文章
- 暂无相关文章
用户点评