为解决某类问题而设计的操作序列(非可执行的指令序列)

特点:有穷性、确定性、可行性、输入输出

1、遍试算法:

逻辑上:针对所有的可能的情况进行判断
形式上FOR中用IF

示例:

  • 韩信点兵

  • 水仙花数:1^3+5^3+3^=153

运行结果:153、370、371、407

但人们目前最多知道11位的,随着位数增大,耗时增大,然后就算不出来了。

  • 完全数:28=1+2+4+7+14

  • 相亲数:M的约数之和等于N的约数之和。算法和完全数有些类似

运行结果:220,284;1184,1210;2620,2924……

  • 验证歌德巴猜想:任何一个偶数都能表示成两个质数之和。

其他:百鸡问题、鸡兔同笼问题、百分比、佩尔方程

2、迭代:一步一步逐渐精确。

逻辑上:多次使用同一算法。

形式上:a=f(a)

示例:  

  • 求平方根

  • 倍边法求Pi

其他:数字平方和、Mandelbrot集、Julia集

3、递归:分治思想,递归出口+递归主体

逻辑上:一个问题华为同样的问题

形式上:自己调用自己

示例:

  • 求阶乘

  • 斐波那契数列

  • Celay树
	using System;
	using System.Drawing;
	using System.Collections;
	using System.ComponentModel;
	using System.Windows.Forms;
	using System.Data;
	 
	public class Form1 : Form
	{
	    public Form1()
	    {
		this.AutoScaleBaseSize = new Size(6, 14);
		this.ClientSize = new Size(600, 400);
		this.Paint += new PaintEventHandler(this.Form1_Paint);
	    }
	    static void Main() 
	    {
		Application.Run(new Form1());
	    }
	    private void Form1_Paint(object sender, PaintEventArgs e)
	    {
		graphics = e.Graphics ;
		drawTree( 10, 200, 310, 100, -PI/2 );
	    }
	 
	    private Graphics graphics;
	    const double PI = Math.PI;
	    double th1 = 40 * Math.PI / 180;
	    double th2 = 30 * Math.PI / 180;
	    double per1 = 0.6;
	    double per2 = 0.7;
	 
	    void drawTree(int n, 
		    double x0, double y0, double leng, double th)
	    {
		if( n==0 ) return;
	 
		double x1 = x0 + leng * Math.Cos(th);
		double y1 = y0 + leng * Math.Sin(th);
		 
		drawLine(x0, y0, x1, y1, n/2);
		 
		drawTree( n - 1, x1, y1, per1 * leng, th + th1 );
		drawTree( n - 1, x1, y1, per2 * leng, th - th2 );
	    }
	    void drawLine( double x0, double y0, double x1, double y1, int width ){
		graphics.DrawLine( 
		    new Pen(Color.Blue, width ),
		    (int)x0, (int)y0, (int)x1, (int)y1 );
	    }
	}

	Celay树2:支随机持
	using System;
	using System.Drawing;
	using System.Collections;
	using System.ComponentModel;
	using System.Windows.Forms;
	using System.Data;
	 
	public class Form1 : Form
	{
	    public Form1()
	    {
		this.AutoScaleBaseSize = new Size(6, 14);
		this.ClientSize = new Size(400, 400);
		this.Paint += new PaintEventHandler(this.Form1_Paint);
		this.Click += new EventHandler( this.Redraw );
	    }
	    static void Main() 
	    {
		Application.Run(new Form1());
	    }
	    private void Form1_Paint(object sender, PaintEventArgs e)
	    {
		graphics = e.Graphics ;
		drawTree( 10, 200, 380, 100, -PI/2 );
	    }
	    private void Redraw(object sender, EventArgs e)
	    {
		this.Invalidate();
	    }
	 
	    private Graphics graphics;
	    const double PI = Math.PI;
	    double th1 = 35 * Math.PI / 180;
	    double th2 = 25 * Math.PI / 180;
	    double per1 = 0.6;
	    double per2 = 0.7;
	 
	    Random rnd = new Random();
	    double rand()
	    {
		return rnd.NextDouble();
	    }
	 
	    void drawTree(int n, 
		    double x0, double y0, double leng, double th)
	    {
		if( n==0 ) return;
	 
		double x1 = x0 + leng * Math.Cos(th);
		double y1 = y0 + leng * Math.Sin(th);
		 
		drawLine(x0, y0, x1, y1);
		 
		drawTree( n - 1, x1, y1, per1 * leng * (0.5+rand()), th + th1* (0.5+rand()) );
		drawTree( n - 1, x1, y1, per2 * leng * (0.4+rand()), th - th2* (0.5+rand()) );
		if(rand()>0.6)
		    drawTree( n - 1, x1, y1, per2 * leng * (0.4+rand()), th - th2* (0.5+rand()) );
	    }
	    void drawLine( double x0, double y0, double x1, double y1 ){
		graphics.DrawLine( 
		    Pens.Blue,
		    (int)x0, (int)y0, (int)x1, (int)y1 );
	    }
	}

其他:Koch分形集

小结:

遍试:FOR中用IF

迭代:WHILE中a=F(a);

递归:F(n)中用F(n-1);

本文在学习唐大仕老师在网易云课堂中C#公开课后编辑而成,再此表示感谢!


相关文章: