杭电acm 1060

来源:互联网 发布:软件咨询服务公司 编辑:程序博客网 时间:2024/06/05 18:35


/*

N^N=10^(N*logN),即可以转化求10^(N*logN)的首位数字。 

对于10^(X),X为一个实数,可以分解成一个整数加一个小数的和,X = Z + P。即10^(X) = 10^(Z + P) = 10^Z * 10^P,其中(0 <= P < 1) 

显然这里的10^Z是不会影响到10^(X)的首位数字,即关键问题是要求10^P的首位数字。

因为0 <= P < 1 所以 1 <= 10^(P) < 10,只要我们求出10^(P)的值,然后取整 所得到的值就是10^(X)的首位数字

*/




思路很巧妙  以后的变成可以借鉴。

import java.util.Scanner;import java.math.*;public class Main{    public static void main(String []args){        Scanner sc = new Scanner(System.in);        int fir=sc.nextInt();        while(fir>=1){            int a=sc.nextInt();            double np=a*Math.log10(a);            double p=np-Math.floor(np);            int res=(int)Math.pow((double)10, p);            for(;res>10;){                res/=10;            }            System.out.println(res);            fir--;        }    }}
注意 一个浮点数的转化  使用floor函数和直接int转化是不一样的。

0 0
原创粉丝点击