65,210
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <limits>
#include <cmath>
using namespace std;
int GetHighest(const double& n)
{
//求n^n的最高位
//注意:n取 1,2,3,...,1000000000
double intpart;
double fractpart = modf ( n * log10(n), &intpart);
//显然分数部分在[0,1)之间,所以temp在[1,10)之间,那么整数部分即为结果
double temp = pow( (double)10, fractpart);
modf ( temp, &intpart);
return intpart;
}
void main()
{
//cout<<numeric_limits<double>::max()<<endl;
cout<<GetHighest(1)<<endl;
cout<<GetHighest(2)<<endl;
cout<<GetHighest(3)<<endl;
cout<<GetHighest(4)<<endl;
cout<<GetHighest(5)<<endl;
cout<<GetHighest(6)<<endl;
cout<<GetHighest(4678)<<endl;
cout<<GetHighest(1000)<<endl;
cout<<GetHighest(1000000000)<<endl;
cout<<GetHighest(1999)<<endl;
cout<<GetHighest(999999999)<<endl;
}
#include
#include
using namespace std;
//计算一个double型数的非整数部分
double mantissa(double dval) {
if ((int) dval != (int) (dval + 1e-8)) {
return 0;
} else {
return dval - (int) dval;
}
}
//计算首位整数
int leadDigital(double mantissaValue) {
if (mantissaValue >= 0 && mantissaValue + 1e-8 < 1) {
double temp = pow((double) 10, mantissaValue);
return (int) (temp + 1e-8);
} else {
cout << "the mantissaValue is not bigger than 0 or not less than 1" << endl;
exit(0);
return 0;
}
}
int main() {
int N;
cout << "please input the test N value:" << endl;
cout << "or input the exit value: -1" << endl;
while (cin >> N) {
if (N <= 0) {
cout << "the value of N must be bigger than zero" << endl;
}
double nlogNValue = N * log10((double) N);
double mantissaValue = mantissa(nlogNValue);
int leadValue = leadDigital(mantissaValue);
cout << "The lead digital of " << N << "^" << N << " is: " << leadValue << endl;
cout << "please input the test N value again" << endl;
}
return 0;
}