⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 二分搜索.txt

📁 二分搜索算法
💻 TXT
字号:
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class ErFenTest
{
	public static void main(String args[])
	{
		new ErFen().init();
	}
}
class ErFen extends JFrame
{
	public void init()
	{
		Container con=getContentPane();
		con.add(new MyPanel());
		setBounds(350,250,350,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
	}
}
class MyPanel extends JPanel implements Runnable
{
	double a,b,e;
	double x;
	double fa,fb,fx;
	MyPanel()
	{
		setSize(500,500);
		new Thread(this).start();
	}
	public void run()
	{
		a=0.6;
		b=5;
		e=0.0001;
		while (b-a>e)
		{
			x=(a+b)/2.0;
			repaint();
			fa=2*Math.sin(a)-a;
			fb=2*Math.sin(b)-b;
			fx=2*Math.sin(x)-x;
			if (fa*fx<0)
			{
				b=x;
			}
			else 
				a=x;
			try
			{
				Thread.sleep(1000);
			}
			catch (Exception f)
			{
			}
		}
	}
	public void paintComponent(Graphics g)
	{
		//画坐标轴
        g.setColor(Color.red);     
        g.drawLine(0, 150,350,150);   //x轴
        g.drawLine(175,0,175,300);  //y轴


		g.setColor(Color.green);     
        for(int i = -100;i<=100;i++)
        {
			g.drawLine(i+175,(int)((-(2*Math.sin(i*3.14/100)-(i*3.14/100)))*50+150),i+175,(int)((-(2*Math.sin(i*3.14/100)-(i*3.14/100)))*50+150));
        }


		g.setColor(Color.black);
		g.drawLine((int)(x*100/3.14+175),150,(int)(x*100/3.14+175),(int)((-(2*Math.sin(x)-x))*50+150));
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -