I/O & Java in ACM

基础知识

利用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
  • 南洋理工45 棋盘覆盖 计算4的n次方减1除以3 在一个2k×2k(1<=k<=100)的棋盘中恰有一方格被覆盖,如图1(k=2时),现用一缺角的2×2方格(图2为其中缺右下角的一个),去覆盖2k×2k未被覆盖过的方格,求需要类似图2方格总的个数s。如k=1时,s=1; k=2时,s=5
  • 样例输入 t = 3; k = 1; k = 2; k = 3; 样例输出 1; 5; 21;
  • 题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=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(); //整数去掉小数点和后面的0
            if(res.startsWith("0")) res = res.substring(1); //去掉前导0  
            System.out.println(res);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值