题目:NYOJ773
2 16 3 27 7 4357186184021382204544 0 0
4 3 1234
自己写的二分求根超时,居然用pow可以过,我也是醉了,可能算法有bug,求纠正。
//er fen fa
#include
#include
#include
#include
//#include
using namespace std;
const int maxn = 1100;
void mul(char a[], char b[], char c[])//c = a * b
{
int i, j, k, lena = strlen(a),lenb = strlen(b);
int aa[maxn] = {0}, bb[maxn] = {0}, cc[maxn] = {0};
int cnt, tt, temp;
for( i = 0 ; i < lena; i++)
aa[i] = a[lena -1 - i] - '0';
for( i = 0 ; i < lenb; i++)
bb[i] = b[lenb -1 - i] - '0';
for(i = 0; i lenb)
return 1;
else if(lena < lenb)
return -1;
else
return strcmp(a,b);
}
void i2a(int x, char a[]) // int to array
{
int i = 0, t ,len, temp;
while(x != 0)
{
a[i++] = x % 10 + '0';
x = x / 10;
}
a[i] = '\0';
len = strlen(a);
for(i = 0 ; i < len / 2; i++)
{
temp = a[i];
a[i] = a[len - i - 1];
a[len - i -1] = temp;
}
}
int main()
{
//freopen("test.txt","r",stdin);
//freopen("test1.txt","w",stdout);
int n;
double x0 = 1, x1 = pow(10,9), x2;
char p[maxn] = {'\0'}, ans[maxn] = {'\0'};
char tt[maxn] = {'\0'};
int flag;
while(scanf("%d%s",&n,p),(n != 0 && p[0] != 0))
{
if(n == 1)
{
printf("%s\n",p);
continue;
}
if(strcmp(p,"1")==0)
{
printf("%s\n",p);
continue;
}
x0 = 1, x1 = pow(10,9);
memset(ans,'\0',sizeof(ans));
memset(tt,'\0',sizeof(tt));
i2a(n,tt);
while(1)
{
x2 = long (( x0 + x1 ) / 2);
i2a(x2,tt);
mi(tt,n,ans);
// cout<<"ans = "< 0)
x1 = x2 + 1;
else if(flag == 0)
{
printf("%s\n",tt);
break;
}
}
}
return 0;
}
#include
#include
int main()
{
double n,p,x;
while(scanf("%lf%lf",&n,&p)!=EOF)
{
if(n==0&&p==0) return 0;
printf("%.lf\n",pow(p,1.0/n));
}
return 0;
}
网上代码Java版
import java.util.Scanner;
public class Main{
public static void main(String[] args){
int n;
double p;
Scanner cin = new Scanner(System.in);
while(true){
n = cin.nextInt();
p = cin.nextDouble();
if(n == 0 && p == 0) break;
System.out.printf("%.0f\n", Math.pow(p, 1.0/n));
}
}
}