我是靠谱客的博主 激动水池,这篇文章主要介绍C# 常用算法 迭代一 迭代,现在分享给大家,希望可以做个参考。

一 迭代

逻辑上:多次使用同一算法;
形式上:a=f(a);
示例:求平方根;
倍边法求Pi;
其他,如:数字平方和、Mandelbrot集,Jullia集;

求平方根

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 求平方根 { public class Sqrt { public static void Main(string[] args) { Console.WriteLine(sqrt(98)); Console.WriteLine(Math.Sqrt(2.0)); Console.ReadKey(); } static double sqrt(double a) { double x = 1.0; do { Console.WriteLine(x + "," + a / x); x = (x + a / x) / 2; } while (Math.Abs(x * x - a) / a > 1e-6); return x; } } }

倍边法求π

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 倍边法求π { class BeiBianFaQiuπ { static void Main() { double a = 1; for(int n=0;n<=10;n++) { a = Math.Sqrt(2 - Math.Sqrt(4 - a * a)); double pi = a * 3 * Math.Pow(2, n); Console.WriteLine(pi); } Console.WriteLine(Math.PI); Console.ReadKey(); } } }

最后

以上就是激动水池最近收集整理的关于C# 常用算法 迭代一 迭代的全部内容,更多相关C#内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(44)

评论列表共有 0 条评论

立即
投稿
返回
顶部