ZOJ 2277 The Gate to Freedom(n^n)

The Gate to Freedom

Time Limit: 2 Seconds       Memory Limit: 32768 KB

Background

It is dark at night.
It is silence at night.
It is she in the dark.
It is she in the silence.

Then a light appeared. A huge gate came into our sights, called

The Gate to Freedom


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?"


题意:求n^n结果最左边的数


思路方法:之前有些过N^N求最后一位的数字,那是各种姿势都可做。可是这道题似乎就没那么简单的说。但是。。。

设: n^n = k * 10^b  (4^4 = 1.6 * 10^1)

两边对10取对数:n*log10(n) = log10(k * 10^b) = log10(k)  + b

可得:log10(k) = n*log10(n) - b

然后因为0<k<10,所以0<log10(k)<1,然后可以发现: b = floor(n*log10(n)),也就是 n*log10(n) 的整数部分(floor()用于向下取整,返回值为double)


计算可得:x = n*(log10(n))-floor(n*(log10(n)))

所以:k = floor(pow(10,x)),取第一位就是答案

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
typedef long long LL;
using namespace std;
int main(){
    double n;
    while(scanf("%lf",&n)!=EOF){
        double x = n*(log10(n))-floor(n*(log10(n)));
        double k = floor(pow(10,x));
        printf("%.0f\n",k);
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/const_qiu/article/details/47169865
ZOJ
N!
n
N*
今日推荐