hdu1060;hdu1018

   

             有关数位的问题 

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. 
         

             



           这道题明明以前做过,到了这次周赛,感觉好熟悉,却想不到方法,悲催!

           于是从今天决定,做过的每道不错的题,都要写下解题报告,不能做过就忘!

           这道题,不要妄想可以求出来N^N,用什么方法都很够呛

           众所诸知,科学计数法:y= a * 10^n;1<=a<10;

           所以题目所求的即为a的整数部分

           已知: 

                num^num=a * 10^n

              so ,x=lg(num^num)=num* lg(num)=n+lg(a);

              且 0<=lg(a)<1;

              即 n为 x的整数部分 (n=floor(x)) ,lg(a)为小数部分;

              a=10^(x-n);

              


              

#include<cstdio>
#include<cmath>

using namespace std;

double num,n,x,lga,a,T;
int t;
int main(){
scanf("%lf",&T);
  while(T--){
   scanf("%lf",&num);
   x=num*log10(num) ;
   lga=x-floor(x);
   a=pow(10,lga);



   t=floor(a);
   printf("%d\n",t) ;

}

return 0;}


                                                 Big Number

                                                          Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
                                                                           Total Submission(s): 35058    Accepted Submission(s): 16688


Problem Description
In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encryption, etc. In this problem you are given a number, you have to determine the number of digits in the factorial of the number.
 

Input
Input consists of several lines of integer numbers. The first line contains an integer n, which is the number of cases to be tested, followed by n lines, one integer 1 ≤ n ≤ 107 on each line.
 

Output
The output contains the number of digits in the factorial of the integers appearing in the input.
 

Sample Input
  
  
2 10 20
 

Sample Output
  
  
7 19
 


这道题目,即是求N!共有多少位

同上道题,不可能求出N!数是多少,不过还好我们有log这一利器

log(N!)=log(N)+log(N-1)+...+log(2)+log(1);

要知道,log(a*10^n)约等于n,且其位数为n+1,所以,我们只要求

(int)log(N!)+1即可


AC代码


#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;


int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        double sum=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            sum+=log10(i);
        cout<<(int)sum+1<<endl;


    }


    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值