hdu 1060 .

水鸟第一篇解题报告,大牛们直接忽略即可

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1060

 

 

Leftmost Digit

Problem Description

Given a positive integer N, you should output the leftmost digit of N^N.

 

Input

The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).

 

Output

For each test case, you should output the leftmost digit of N^N.

 

Sample Input

2 3 4

 

Sample Output

2 2 

Hint

In the first case, 3 * 3 * 3 = 27, so the leftmost digit is 2. In the second case, 4 * 4 * 4 * 4 = 256, so the leftmost digit is 2.

 

 

Author

Ignatius.L

题目大意:1第一行输入一个数T表示有T组测试数据;

          2接下来的T行,每行输入一个整数N(1<=N<=1,000,000,000).

          3输出结果为对应的每个NN次方的最左边的那位数(即最高位)

           (注意:每行对应输出一个结果(即要换行))。

解题思路(来自大神,当然代码差不多也全是他帮忙改的的):

          1,令M=N^N; 

          2,分别对等式两边取对数得 log10(M)=N*log10(N),M=10^(N*log10(N));                  

          3,令N*log10(N)=a+b,a为整数,b为小数;

          4C函数:log10(),计算对数,pow(a,b)计算a^b(注意:因为这是一个数学函数,所以这里需用头文件#include<math.h>)

          5由于10的任何整数次幂首位一定为1,所以,M的首位只和N*log10(N)的小数部分有关,即只用求10^b救可以了;

          6,最后对10^b取整,输出取整的这个数就行了。(因为0<=b<1,所以1<=10^b<=10对其取整,那么的到的就是一个个位,也就是所求的数)。

需要注意的地方:

          1关于取整:可以用强制类型转换(int10^b,也可以用floor函数floor10^b),

但要注意的问题是floor函数是double型的,若用floor函数,则在输出时要用"%.0lf\n",

(有关floor函数和ceil函数,详见http://baike.baidu.com/view/2873705.htm)

 相关代码:

代码1(要用G++交,若用C++则会报错需加上头文件#include<iostream>,大神说C++较严)

 

#include <stdio.h>  

#include <math.h>  

int main()  

{  

    int T,n;  

    scanf("%d",&T);  

    while(T--)  

    {  

        scanf("%d",&n);  

        double m=n*log10(n);            

        double a=pow(10,m-floor(m));    

        printf("%.0lf\n",floor(a));  

    }  

    return 0;  

}  

 

 

 

 

 

posted @ 2012-05-08 15:12  free斩  Views(325)  Comments(0Edit  收藏  举报