问题描述:

HD-ACM算法专攻系列(19)——Leftmost Digit

 

AC源码:

解题关键是,数据很大,不能强算,需要使用技巧,这里使用科学计算法,令N^N=a*10^n ,取对数后变为 N*log10(N)=log10(a)+n,令x = log10(a)+n  又 n = int(x)  [取整,当然根据所给数据范围,为了避免溢出,这是使用的是long long取整],则 a = 10^(x - int(x)),最后带入x= N*log10(N),对a的值取整即为最终结果。

 

#include"iostream"
#include"cmath"
using namespace std;

int main()
{
	int t, n;
	double x;
	cin>>t;
	for(int i = 0; i < t; i++)
	{
		cin>>n;
		x = n * log10(n);
		cout<<(int)pow(10, x  - (long long)x)<<endl;
	}
	
    return 0;
}

  

相关文章:

  • 2021-07-01
  • 2021-12-25
  • 2021-06-12
  • 2021-10-25
  • 2021-06-17
  • 2021-12-28
  • 2021-12-28
  • 2021-07-03
猜你喜欢
  • 2021-09-10
  • 2021-11-14
  • 2021-11-15
  • 2021-12-30
  • 2022-01-17
  • 2021-06-20
  • 2022-01-27
相关资源
相似解决方案