zoukankan      html  css  js  c++  java
  • Java学习的第四十二天

    1.例4.7弦截法求方程f(x)=x^3-5x^2+16x-80=0的根

     import java.util.Scanner;
     import java.lang.*;
     public class cjava {
          public static void main(String[] args) {
              double x1,x2,f1,f2,x;
              Scanner a=new Scanner(System.in);
              do
              {
                  System.out.println("input x1,x2:");
                 x1=a.nextDouble();
                 x2=a.nextDouble();
                 f1=f(x1);
                 f2=f(x2);
             }while(f1*f2>=0);
             x=root(x1,x2);
            System.out.println("A root of equation is "+x);
         }
         static double f(double x) {
             double y;
             y=x*x-5*x+16*x-80;
           return y;
         }
         static double xpoint(double x1,double x2)
         {
             double y;
             y=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
             return y;     }
         static double root(double x1,double x2)
         {
             double x,y,y1;
             y1=f(x1);
            do
             {
                 x=xpoint(x1,x2);
                 y=f(x);
             if(y*y1>0)
             {
                y1=y;
                 x1=x;
             }
             else
                 x2=x;
         }while(Math.sqrt(y)>=0.00001);
             return x;
             }
    }

    例4.8递归的方法求n!

    import java.util.Scanner;
     public class cjava {
         public static void main(String[] args) {
    int n;long y;
    @SuppressWarnings("resource")
    Scanner a=new Scanner(System.in);
    System.out.println("please input an integer");
    n=a.nextInt();
    y=fac(n);
    System.out.println(n+"!="+y);
             }
         static long fac(int n) {
             long f;
             if(n<0) {
                 System.out.println("n<0,data error!");
                 f=-1;
             }
             else if(n==0||n==1)
                 f=1;
             else
                 f=fac(n-1)*n;
             return f;
         }
         }

     2.没问题

    3.明天继续写例题

  • 相关阅读:
    hrbust1841再就业(状态压缩dp)
    并查集 poj2236
    JavaScript 常用单词整理
    HTML/CSS 常用单词整理
    HTML/CSS 知识点
    JavaScript 知识点
    ArcGIS裁剪影像数据
    小小程序员
    ArcGIS平面阴影制作
    前端相关的seo技术
  • 原文地址:https://www.cnblogs.com/feng747/p/13519794.html
Copyright © 2011-2022 走看看