有关数位的问题
Input
Each test case contains a single positive integer N(1<=N<=1,000,000,000).
Output
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
2 10 20
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;
}