ch=system.in.read();
data=1;
count=0;
while(ch!="#")
{
data=*ch;
count=count+1;
ch=system.in.read();
}
result=data^count;
system.out.writeln(result);
data=1;
count=0;
while(ch!="#")
{
data=*ch;
count=count+1;
ch=system.in.read();
}
result=data^count;
system.out.writeln(result);
java.lang.Math好象没给我们提供完成开N次方的方法,只给我们提供了个java.lang.Math.sqrt()能够求开平方(开二次方),看来只好自己做个开N次方的方法,我做了一种方法,是利用java.lang.Math里的两个方法配合来实现开N次方:
如果要对X开N次方,这样写:
Math.pow(Math.E,Math.log(X)/N)
此表达式返回的是X的N次方值(double型)
好了,现在来看看它是否能完成任务:为了便于使用,把它做成一个方法forbear()来调用:class Sqrt
{
public static void main(String[] args)
{
System.out.println(Sqrt.forbear(8,3));
}
static double forbear(double x,double n)
{
return Math.pow(Math.E,Math.log(x)/n);
}
}结果是:
1.9999999999999998
Press any key to continue...
由于是double运算返回的这个结果也就等于是2看来可以用,现在来完成你的要求,N个数相乘然后再把结果开N次方:class Sqrt
{
public static void main(String[] args)
{
int[] numeric={3,4,5};//我用了3个整数,你可以使用浮点数
double total=1;
//先计算N个数相乘
for(int i=0;i<numeric.length;i++)
{
total*=numeric[i];
}
//用得到的结果开N次方得到几何平均数
total=Sqrt.forbear(total,numeric.length);
System.out.println("The result is: "+total);
}
static double forbear(double x,double n)
{
return Math.pow(Math.E,Math.log(x)/n);
}
}结果是:
The result is: 3.914867641168863
Press any key to continue...注: 3*4*5=60
3.914867641168863的3次方是59.99999999999997
what's Math.pow(3*4*5,1d/3)?
???????????????
我倒觉得难在求乘积,使用上面给出的办法,如果乘积超出double范围将是灾难