HDOJ 2117 Just a Numble

来源:互联网 发布:中国也门撤侨事件 知乎 编辑:程序博客网 时间:2024/06/06 13:18

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 = 1;            int t = 0;            for (int i = 0; i < m; i++) {                if (sum==0) {                    t=0;                    break;                }                sum = sum*10;                t = sum/n;                sum = sum%n;            }            System.out.println(t%10);        }        sc.close();    }}
原创粉丝点击