首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

求java算法,该如何解决

2012-01-31 
求java算法题目:1/2+1/4+1/6+1/8+.........+1/40的和,求java算法,哪位高手能帮忙下?[解决办法]int sum0f

求java算法
题目:1/2+1/4+1/6+1/8+.........+1/40的和,求java算法,

哪位高手能帮忙下?

[解决办法]
int sum=0;
for(int i=1;i<=20;i++)
sum+=1.0/(2*i);
System.out.println(sum);
[解决办法]

Java code
public class TestCode {    public static double getSun(double in) {        double sun = 0;        for (double i = 2; i <= in; i += 2) {            sun = sun + 1 / i;        }        return sun;    }    public static void main(String[] args) {        System.out.println(getSun(40));    }}
[解决办法]
探讨
Java code

public class TestCode {

public static double getSun(double in) {
double sun = 0;
for (double i = 2; i <= in; i += 2) {
sun = sun + 1 / i;
}
……

[解决办法]
探讨
非常感谢,其实我想要的是带分子和分母的结果

[解决办法]
Java code
public class Test {    public static void main(String[] args) throws Throwable {                int min = 2;        for (int i=4; i<=40; i+=2) { //求分母的最小公倍数            min = getMinTimes(min, i);        }                int sum = 0;        for (int i=2; i<=40; i+=2) { //通分分子求和, 上面这里写错了,是i+=2,不是每次递增1            sum += min/i;        }        System.out.printf("%d/%d=%.4f\n", sum, min, ((double)sum)/min); //输出结果        double n = 0;        for (int i=2; i<=40; i+=2) { //检验结果,上面这里写错了            n += ((double)1)/i;        }        System.out.printf("%.4f\n", n);    }    public static int getMinTimes(int a, int b) { //求两个数的最小公倍数        int a1 = a, b1 = b;        int mod = a%b;        while (mod != 0) {            a = b;            b = mod;            mod = a%b;        }        return (a1*b1)/b;    }}
[解决办法]
探讨
+1
母子分母只差约分了.

[解决办法]
for example :

Java code
//题目:1/2+1/4+1/6+1/8+.........+1/40的和,求java算法,public class Test {    public static void main(String[] args) {          float sum = 0 ;          //int i ;          for ( float i = 2 ; i <= 40 ; i += 2 )              sum += 1 / i ;          System.out.println ("1/2+1/4+1/6+1/8+.........+1/40 = " + sum );    }}
[解决办法]
Java code
public class B {    public static void main(String[] args) {        long[] result = new long[]{1,2};        for(int i = 1 ; i < 20; i++){            result = sum(1, i*2 + 2 , result);        }         System.out.println(result[0] + "/" + result[1]);    }    private static long[] sum(long fz,long fm, long result[]){        long[] result1 = new long[2];        result1[0] = result[0] * fm + fz * result[1];        result1[1] = result[1] * fm;        long max = 1;        for(long i = 1 ; i <= result1[0]; i++){            if(result1[0]%i == 0 && result1[1]%i ==0){                max = i;            }        }         if(max != 1){            result1[0] = result1[0]/max;            result1[1] = result1[1]/max;        }          System.out.println(result1[0] + "/" + result1[1]);        return result1;    }}
------解决方案--------------------


public class B {
public static void main(String[] args) {
long[] result = new long[]{1,2};
for(int i = 1 ; i < 20; i++){
result = sum(1, i*2 + 2 , result);

System.out.println(result[0] + "/" + result[1]);
}
private static long[] sum(long fz,long fm, long result[]){
long[] result1 = new long[2];
result1[0] = result[0] * fm + fz * result[1];
result1[1] = result[1] * fm;
long max = 1;
long it = result1[0] < result1[1] ? result1[0] : result1[1];
for(long i = it ; i >= 1 ; i--){
if(result1[0]%i == 0 && result1[1]%i ==0){
max = i;
break;
}

if(max != 1){
result1[0] = result1[0]/max;
result1[1] = result1[1]/max;
}
System.out.println(result1[0] + "/" + result1[1]);
return result1;


}
最后结果:55835135/31039008

热点排行
Bad Request.