• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
dwtfukgv
博客园    首页    新随笔    联系   管理    订阅  订阅
HDU 1060 Leftmost Digit (数学log)

题意:给定一个数n,让你求出n的n次方的第一位数。

析:一看这个n快到int极限了,很明显不能直接做,要转化一下。由于这是指数,我们可以把指数拿下来。

也就是取对数,设ans = n ^ n,两边取以10为底对数 lg(ans) = n * lg(10),然后这个整数部分都是10的多次方,

没什么用,也就是说我们要的小数部分,然后再取指数,就OK了。还要注意要用long long因为可能超int了,第一次忘了,WA了。

代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>

using namespace std;
typedef long long LL;

int main(){
    int n, T;  cin >> T;
    while(T--){
        cin >> n;
        double ans = n * log10(n);
        ans -= (LL)ans;//小数部分
        cout << (LL)pow(10, ans) << endl;
    }
    return 0;
}

 

posted on 2016-06-18 11:28  dwtfukgv  阅读(178)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3