题目大意: K ^ N = P, 给N 和 P, 求K。数据规模:1<=n<= 200,1<=p<10101and there exists an integerk, 1<=k<=109。
思路:初看是大数运算,看了Disuss后(真不该……)发现竟然可以一句话AC……
12345678 | #include<stdio.h> #include<math.h> int main() {
double n, m;
while (scanf( "%lf%lf" , &n,&m) != EOF)
printf( "%.0lfn" ,pow(m, 1 / n)); } |
12345678 | #include<stdio.h> #include<math.h> int main() {
double n, m;
while (scanf( "%lf%lf" , &n,&m) != EOF)
printf( "%.0lfn" ,pow(m, 1 / n)); } |
#include<stdio.h>
#include<math.h>
int main()
{
double n,m;
while(scanf("%lf %lf", &n, &m) !=EOF)
printf("%.0lfn" ,pow(m, 1 / n));
}
类型长度(bit)有效数字绝对值范围
float326~710^(-37) ~ 10^38
double6415~1610^(-307) ~10^308
long double 12818~1910^(-4931) ~ 10 ^ 4932
double最大可存储10 ^ 308范围内的数,而p的范围才10 ^101,这很好理解。但double的精度才15-16位啊,存储之后精度损失这么严重,竟然有正确结果,这该怎么解释呢?
当然,pow(p ,1 / n)可以求K。受教了。
另外,可以用二分法AC此题,继续纠结精度问题……
?123456789101112131415161718192021222324252627282930313233 | #include<stdio.h> #include<math.h> #define eps 0.0000000001 void init(),work(); double n, m,k; int main() {
init();
return 0; } void init() {
while (scanf( "%lf%lf" , &n,&m) != EOF)
work(); } void work() {
long long left, right, mid;
left= 0;
right= 1000000002;
while (left+ eps < right){
mid= (left + right) / 2;
if (pow(mid,n) - m > 0)
right= mid;
else
if (pow(mid,n) - m < 0)
left= mid;
else {
printf( "%.0ldn" ,mid) ;
break ;
}
} } |
#include<math.h>
#define eps 0.0000000001
void init(), work();
double n, m, k;
int main()
{
init();
return0;
}
void init()
{
while(scanf("%lf %lf", &n, &m) !=EOF)
work();
}
void work()
{
long longleft, right, mid;
left =0;
right =1000000002;
while(left +eps < right){
mid = (left + right) / 2;
if(pow(mid, n) - m > 0)
right = mid;
else
if(pow(mid, n) - m < 0)
left = mid;
else{
printf("%.0ldn", mid) ;
break;
}
}
}