实在想不到好方法,不过比较字符串慢多了!
class ComputePi 
{
public static void main(String[] args) 
{
int n=0;
long start=System.currentTimeMillis();
for(int i=0;i<3;i++) n=getPi_Str();
        long end =System.currentTimeMillis();
System.out.println("Time:"+(end-start)/3+"\tN:"+n);
        
start=System.currentTimeMillis();
for(int i=0;i<3;i++) n=getPi_Num();
end =System.currentTimeMillis();
System.out.println("Time:"+(end-start)/3+"\tN:"+n);
} public static int getPi_Str(){
        //4*(1-1/3+1/5-1/7+1/9-1/11+........)
double limit=3.14159;
String s=String.valueOf(limit);
double result=4;
int n=1;
double flag=-1;
double dt;
while(!String.valueOf(result).startsWith(s)){
dt=flag/(2*n+1);
result+=4*dt;
flag=(flag==1)?-1:1;
n++;
}
return n;
}
public static int getPi_Num(){
        //4*(1-1/3+1/5-1/7+1/9-1/11+........)
double limit=3.14159;
double result=4;
int n=1;
double flag=-1;
double dt;
while(Math.abs(result-limit)>0.00001||result<limit){
dt=flag/(2*n+1);
result+=4*dt;
flag=(flag==1)?-1:1;
n++;
}
//System.out.println(result);
return n;
}}
结果:
Time:1015  N:136121
Time:15   N:136121
字符串真的就是慢

解决方案 »

  1.   

    to drugon(更高,更远,更强) :
    如果个个都看书的话,那来csdn就没有什么了!毕竟讨论有讨论的好处!
    大家都一起学习!
      

  2.   

    JAVA编程算法方面的东西应该用的不是很多吧,
    JAVA编程的目地就是达到最简.
      

  3.   

    我用c写了如下一段 可是很长时间不能得出结果 请大家看下有什么问题 顺便有对数据结构感兴趣的可以qq 3018422 共同研究
    #include <stdio.h>
    #include <math.h>
    void main()
    {
    double limit = 3.14159, result = 4 ,dr=0;
    int n =1;
    while(result-limit>0.00001||result<limit)
    {
    dr  = dr+1/n;
    result = 4 * dr;
    n  = - (abs( n ) + 2) ;
    }
    printf("%d",n);
    }
    #include <stdio.h>
    #include <math.h>
    void main()
    {
    double limit = 3.14159, result = 4 ,dr=0;
    int n =1;
    while(result-limit>0.00001||result<limit)
    {
    dr  = dr+1/n;
    result = 4 * dr;
    n  = - (abs( n ) + 2) ;
    }
    printf("%d",n);
    }
      

  4.   


    第一题:
    public class CTest {
      private double limit_Pi = 3.1415;
      private int m_n = 0;  public double getPi() {
        return this.limit_Pi;
      }  public void setN(int n) {
        this.m_n = n;
      }  public int returnResult(double limit) {
        double result = 1;
        double i = 3.0;
        boolean flag = false;
        while (true) {
          if (flag) {
            result = result + 1 / i;
            flag = false;
          }
          else {
            result = result - 1 / i;
            flag = true;
          }
          if (result * 4.0 >= 3.1415 && result * 4.0 <= 3.14159) {
            break;
          }
          i += 2;
          System.out.println(result * 4.0);
        }
        System.out.println(result * 4.0);
        return (int) i;
      }  public static void main(String[] args) {
        long start = System.currentTimeMillis();
        CTest test = new CTest();
        double limit = test.getPi();
        int n = test.returnResult(limit);
        test.setN(n);
        System.out.println(test.m_n);
        long end = System.currentTimeMillis();
        System.out.print("共毫时:" + (end - start));
      }  public CTest() {
      }
    }
    共毫时:12063