基础知识
利用java读取in.txt中的数据
in.txt 文件放到目录中与src目录同级的位置
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("in.txt" ));
System.setIn(bis);
Scanner cin = new Scanner(System.in );
JAVA大数类练手
南洋理工28
南洋理工28 我们都知道如何计算一个数的阶乘,可是,如果这个数很大呢,我们该如何去计算它并输出它? 输入一个整数 m(0 < m <= 5000),输出他的阶乘 样例输入; 50; 样例输出; 30414093201713378043612608166064768844377641568960512000000000000 题目链接:http: //acm.nyist.net/JudgeOnl ine/problem.php?pid=28
import java.io.*; import java.math.*; import java.util.*;
public class Main {
public static void main (String[] args) throws Exception {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("in.txt" )); System.setIn(bis);
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
BigInteger ans = BigInteger.ONE;
for (int i = 1 ; i <= n; i++) ans = ans.multiply(BigInteger.valueOf(i));
System.out.println(ans);
}
}
南洋理工45
import java.io.*; import java.math.*; import java.util.*;
public class Main {
public static void main (String[] args) throws Exception {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("in.txt" ));
System.setIn(bis);
Scanner in = new Scanner(System.in );
int test = in .nextInt();
while (test-- > 0 ){
int n; n = in .nextInt();
BigInteger a = new BigInteger("4" );
for (int i = 1 ; i < n; ++i) a = a.multiply(BigInteger.valueOf(4 ));
System.out .println(a.subtract(BigInteger.valueOf(1 )).divide(BigInteger.valueOf(3 )));
}
}
}
南洋理工73
南洋理工73 比较两个数的大小 样例输入 111111111111111111111111111 88888888888888888888; -1111111111111111111111111 22222222; 0 0; 样例输出; a > b; a < b; 题目链接:http: //acm.nyist.net/JudgeOnl ine/problem.php?pid=73
import java.io.*; import java.math.*; import java.util.*;
public class Main {
public static void main (String[] args) throws Exception {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("in.txt" )); System.setIn(bis); Scanner cin = new
Scanner(System.in );
while (cin.hasNext()) {
BigInteger a = cin.nextBigInteger();
BigInteger b = cin.nextBigInteger();
if (a.equals(BigInteger.ZERO) || b.equals(BigInteger.ZERO)) break ;
int flag = a.compareTo(b);
if (flag == -1 ) System.out .println("a<b" );
else if (flag == 0 ) System.out .println("a==b" );
else System.out .println("a>b" );
}
}
}
南洋理工103
南洋理工103 大数相加 样例输入 2; 1 2; 112233445566778899 998877665544332211; 样例输出 Case 1:; 1 + 2 = 3; Case 2:; 112233445566778899 + 998877665544332211 = 1111111111111111110; 题目链接:http: //acm.nyist.net/JudgeOnline/problem.php?pid=103
import java.io.*; import java.math.*; import java.util.*;
public class Main {
public static void main (String[] args) throws Exception {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("in.txt" )); System.setIn(bis); Scanner cin = new
Scanner(System.in );
int n = cin.nextInt();
for (int i = 0 ; i < n; i++) {
BigInteger a = cin.nextBigInteger();
BigInteger b = cin.nextBigInteger();
BigInteger ans = a.add(b);
System.out .println("Case " + (i + 1 ) + ":" );
System.out .println(a + " + " + b + " = " + ans);
}
}
}
南洋理工114
南洋理工114 递推求值 数列A满足An = An-1 + An-2 + An-3, n >= 3; 编写程序,给定A0, A1 和 A2, 计算A99 样例输入 1 1 1; 样例输出 69087442470169316923566147; 题目链接: http: //acm.nyist.net/JudgeOnl ine/problem.php?pid=114
import java.io.*; import java.math.*; import java.util.*;
public class Main {
public static void main (String[] args) throws Exception {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("in.txt" )); System.setIn(bis); Scanner cin = new
Scanner(System.in);
BigInteger[] a = new BigInteger[105 ];
while (cin.hasNext()) {
for (int i = 0 ; i <= 2 ; i++) a[i] = cin.nextBigInteger();
for (int i = 3 ; i < 100 ; i++) a[i] = a[i - 1 ].add(a[i - 2 ]).add(a[i - 3 ]);
System.out.println(a[99 ]);
}
}
}
南阳理工155
南阳理工155 高精度幂 现在要你解决的问题是:对一个实数R( 0.0 < R < 99.999 ),要求写程序精确计算 R 的 n 次方(Rn),其中n 是整数并且 0 < =n <= 25。 样例输入 95.123 12; 0.4321 20; 5.1234 15; 6.7592 9; 98.999 10; 1.0100 12; 样例输出 548815620517731830194541.899025343415715973535967221869852721; .00000005148554641076956121994511276767154838481760200726351203835429763013462401; 43992025569.928573701266488041146654993318703707511666295476720493953024; 29448126.764121021618164430206909037173276672; 90429072743629540498.107596019456651774561044010001; 1.126825030131969720661201; 题目链接:http: //acm.nyist.net/JudgeOnl ine/problem.php?pid=155
import java.io.*; import java.math.*; import java.util.*;
public class Main {
public static void main (String[] args) throws Exception {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("in.txt" )); System.setIn(bis); Scanner cin = new
Scanner(System.in);
while (cin.hasNext()) {
BigDecimal ans = cin.nextBigDecimal();
int n = cin.nextInt();
String res = ans.pow(n).stripTrailingZeros().toPlainString();
if (res.startsWith("0" )) res = res.substring(1 );
System.out.println(res);
}
}
}