您的位置:首页 > 其它

HDU 1060 1061求 n^n 最左边 最右边的数字

2012-08-09 23:20 274 查看
求最左边用log

m=n^n

log10(m)=nlog10(n)

m=10^(nlog10(n))

10的整数幂第一位是1,那么第一位的数字由小数部分决定

代码如下

#include<stdio.h>
#include<math.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
      __int64 n,b;
      double a,c;
      int d;
      scanf("%I64d",&n);
      a=n*log10(n);
      b=(__int64)a;
      c=a-b;
      d=pow(10,c);
      printf("%d\n",d);
    }
    return 0;
}






求最右边的数字 整数快速幂 每次%10就ok了

#include<stdio.h>

int main()
{
   int t;
   scanf("%d",&t);
   while(t--)
   {
      int n;
      scanf("%d",&n);
      int res=1;
      int x=n;
      for(;x;x>>=1)
      {
        if(x&1)
        res=(res*(n%10))%10;
        n=((n%10)*(n%10))%10;
      }
      printf("%d\n",res);
   }
   return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐