当前位置: 代码迷 >> 综合 >> HDOJ 1018 Big Number (大数位数)
  详细解决方案

HDOJ 1018 Big Number (大数位数)

热度:85   发布时间:2023-10-21 20:05:16.0

HDACM1018
此题有点坑,一看Big Number 以为是调用大数类的方法来做,可是总是超时,后来发现,求一个数的位数不就是用log10(num)+1来求的么,
而 num=1*2*3*4*…*n;
log10(num)+1
=log10(1*2*3*4*…*n)+1
=log10(1)+log10(2)+log10(3)+log10(4)+…+log10(n)+1
即调用Math.log10()函数即可解决

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();while (n-->0) {int i = sc.nextInt();double sum = 0;for (int j = 1; j <= i; j++) {sum += Math.log10(j);}System.out.println(1+(int)sum);}}}
}
  相关解决方案