zoukankan      html  css  js  c++  java
  • hdu1060

    之前做过的一道题,现在还是拿出来谢谢自己的心得,不是为了叠加博客的内容,而是为了这样每做一道题会一种方法,坚持下去(多吃饭,多运动,多敲代码,多写博客,多陪女朋友,)为了自己的健康,自己开心快乐

    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.
    代码
    #include<iostream>
    #include<cmath>
    using namespace std;
    int main()
    {
     int n,t;
     double a1,a2;
     cin>>t;
     while(t--){
      cin>>n;
      a1=n*(log10(n*1.0));
      //cout<<a1<<endl;
      a2=a1-(__int64)a1;
      //cout<<a2<<endl;
      cout<<(int)pow(10.0,a2)<<endl;
     }
     return 0;
    }
    m=n^n,两边分别对10取对数得 log10(m)=n*log10(n),
    得m=10^(n*log10(n)),由于10的任何整数次幂首位一定为1,
    所以m的首位只和n*log10(n)的小数部分有关
  • 相关阅读:
    组合与计数
    20160929训练记录
    奇特而有用的定理
    图论 500 题
    《长安十二时辰》愿你看尽世间百态,心中仍有热血
    洛谷 [P1337] 平衡点
    洛谷 [P3496] BLO
    洛谷 [P2341] 受欢迎的牛
    洛谷 [P3723] 礼物
    洛谷 [P3338] 力
  • 原文地址:https://www.cnblogs.com/ACWQYYY/p/4486146.html
Copyright © 2011-2022 走看看