ZOJ Problem Set - 2277
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2277
Problem
There’re some words on the gate: “This gate will lead you to freedom. First, you have to open it. I have a problem for you to solve, if you answer it correctly, the gate will open!”
“Tell me, young boy, what is the leftmost digit of N^N?”
Input
This problem contains multiple test cases.
Each test case contains an integer N (N<=1,000,000,000).
Output
For each test case, output the leftmost digit of N^N.
Sample Input
3
4
Sample Output
2
2
给你一个N,求N^N第一位数字是几。
这个题用lg10写。
比如5 = 10lg5
首先:这个题可以换成所以 N^N = 10N*lgN
其次:比如108.3 = 108 * 100.3, 108第一位就是1,所以第一位数是几取决于 100.3,算这个 100.3就行了。
代码如下:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
double n;
while(scanf("%lf",&n)!=EOF)
{
double a = n*(log10(n)); 如果对应上边那个例子,这个a = 5.8;
ll b = (n*log10(n)); b = 5;
int x = (int)pow(10, a-b); 10^0.8 就是结果了
printf("%d\n",x);
}
return 0;
}