天天看点

HDOJ 2117 Just a Numble

HDACM2117

此题第一反应采用大数去做,然而超时

BigInteger a = BigInteger.TEN.pow(m);
BigInteger b = a.divide(new BigInteger(n+""));
System.out.println(b.mod(BigInteger.TEN));
           

因此,追溯除法的根本,如:

1÷3 = 0…1,1×10÷3 = 0…1,依此除下去直到余数为0说明该数除尽

此题即是采用此方法如:

1÷4 = 0…1,1×10÷4=2…2,2×10÷4=5…0所以可知1/4小数点后第2位为5

1÷123 = 0…1,1×10÷123=0…10,10×10÷123=0…100,

100×10÷123=8…16,16×10÷123=1…37,37×10÷123=3…1

…….依次递推,可知1/123小数点后第123位数的值

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            int m = sc.nextInt();
            int sum = ;
            int t = ;
            for (int i = ; i < m; i++) {
                if (sum==) {
                    t=;
                    break;
                }
                sum = sum*;
                t = sum/n;
                sum = sum%n;
            }
            System.out.println(t%);
        }
        sc.close();
    }
}