Leftmost Digit(杭电1060)(求N^N的最高位)

Leftmost Digit

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 13574    Accepted Submission(s): 5216

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,令M=N^N; 

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

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

4,C函数:log10(),计算对数,pow(a,b)计算a^b

5,由于10的任何整数次幂首位一定为1,所以,M的首位只和N*log10(N)的小数部分有关,

   即只用求10^b救可以了;

6,最后对10^b取整,输出取整的这个数就行了。(因为0<=b<1,所以1<=10^b<10对

其取整,那么的到的就是一个个位,也就是所求的数)。
*/
//hdoj系统就是坑,各种CE,各种W,多亏康晓辉指点。
#include<stdio.h>
#include<math.h>
int main()
{
	int test;
	double n,k,a;
	scanf("%d",&test);
	while(test--)
	{
		scanf("%lf",&n);
		a=n*log10(n);
		k=a-(__int64)a; //这里强制转换需要用int64才能A,否则会w或者CE!!!
		printf("%d\n",int(pow(10,k)));
	}
	return 0;
} 
时间: 2024-08-01 07:53:28

Leftmost Digit(杭电1060)(求N^N的最高位)的相关文章

杭电 1060

Leftmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12947    Accepted Submission(s): 4953 Problem Description Given a positive integer N, you should output the leftmost digit of N^N.

杭电2023 求平均成绩(及一些易见的错误)

链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2023 首先,想说下,这题对我来说可能是一个阴影.因为在自己学校的程序竞赛中,这是第二题,当时自己没ac,结果那叫一个自卑啊!然后今天a题目的时候那种恐惧感又来了,很影响情绪.然后,自己现在完全通过自己的努力,但也用了起码3小时的纠错时间,将代码ac了.附上ac代码: #include <iostream> #include<math.h> #include <iomani

&lt;hdu - 1600 - 1601&gt; Leftmost Digit &amp;&amp; Rightmost Digit 数学方法求取大位数单位数字

1060 - Leftmost Digit 1601 - Rightmost Digit 1060题意很简单,求n的n次方的值的最高位数,我们首先设一个数为a,则可以建立一个等式为n^n = a * 10^x;其中x也是未知的: 两边取log10有:lg(n^n) = lg(a * 10^x); 即:n * lg(n)  - x = lg(a); 现在就剩x一个变量了,我们知道x是值n^n的位数-1,a向下取整就是我们要求的数: 所以 按着上面的推导式翻译成代码就可以了(注意:数值的范围和之间的

杭电 1754 I Hate It(线段树求最值)

http://acm.hdu.edu.cn/showproblem.php?pid=1754 I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 38601    Accepted Submission(s): 15270 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某

求平均成绩 【杭电-HDOJ-2023】 附题+详解

/* 求平均成绩 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 62086    Accepted Submission(s): 14888 Problem Description 假设一个班有n(n<=50)个学生,每人考m(m<=5)门课,求每个学生的平均成绩和每门课的平均成绩,并输出各科成绩均大于等于平均成绩的学生数量. In

HDU 1060 Leftmost Digit (数论)

Leftmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13680    Accepted Submission(s): 5239 Problem Description Given a positive integer N, you should output the leftmost digit of N^N.

HDU 1060 Leftmost Digit (数学/大数)

Leftmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 14954    Accepted Submission(s): 5775 Problem Description Given a positive integer N, you should output the leftmost digit of N^N.

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI