平方根,立方根,阶乘,追赶法(C#)
来源:互联网 发布:unity3d中文版软件 编辑:程序博客网 时间:2024/06/10 18:57
平方根,立方根,阶乘,追赶法
class Program { static void Main(string[] args) { //sq_root(); //cu_root(); //nfactorial(); Thomas(); } //平方根 static void sq_root() { double x1, x2, a; Console.WriteLine("请输入参数(a):"); a = Convert.ToDouble(Console.ReadLine()); if (a < 0) { Console.WriteLine("Error"); } else { int i = 0; x2 = 1.0; do { x1 = x2; x2 = 0.5 * (x1 + a / x1); i++; Console.WriteLine("The {2} times,x1={0},x2={1}", x1, x2, i); } while (Math.Abs(x1 - x2) >= 1e-5); Console.WriteLine("{0} Square Root is {1},Repeated {2} times.", a, x2, i); } Console.ReadLine(); } //立方根 static void cu_root() { double x1, x2, a; Console.WriteLine("请输入参数(a)"); a = Convert.ToDouble(Console.ReadLine()); x2 = 1.0; int i = 0; do { x1 = x2; x2=(2*x1*x1*x1+a)/(3*x1*x1); i++; Console.WriteLine("The {2} times,x1={0},x2={1}", x1, x2, i); }while(Math.Abs(x1-x2)>=1e-5); Console.WriteLine("{0} Cube Root is {1},Repeated {2} times.", a, x2,i); Console.ReadLine(); } //n阶乘(支持超大数) static void nfactorial() { int n; BigInteger r; Console.WriteLine("请输入参数(n)"); n = Convert.ToInt32(Console.ReadLine()); r = factorial(n); Console.WriteLine("{0} Factorial is {1}", n, r); Console.ReadLine(); } static BigInteger factorial(int x) { if (x < 1) return 1; return x * factorial(x - 1); } //追赶法 static void Thomas() { //输入 List<double[]> ld = new List<double[]>(); Console.WriteLine("请输入系数矩阵参数如:"); Console.WriteLine("1,1,0,0"); Console.WriteLine("1,1,1,0"); Console.WriteLine("0,1,1,1"); Console.WriteLine("0,0,1,1"); Console.WriteLine("请输入系数矩阵:"); string s = ""; while (s != "End") { double[] rd; s = Console.ReadLine(); if (s != "End") { string[] s1 = s.Split(','); rd = new double[s1.Length]; for (int i = 0; i < s1.Length; i++) { try { rd[i] = Convert.ToDouble(s1[i]); } catch (Exception ex) { Console.WriteLine(ex.Message); } } if (rd.Length > 0) ld.Add(rd); } } Console.WriteLine("======================"); Console.WriteLine("请输入结果矩阵参数如:"); Console.WriteLine("1,1,0,0"); Console.WriteLine("请输入结果矩阵:"); double[] d =new double[ld.Count]; string s2 = ""; s2 = Console.ReadLine(); string[] sd = s2.Split(','); for (int i = 0; i < sd.Length; i++) { try { d[i] = Convert.ToDouble(sd[i]); } catch (Exception ex) { Console.WriteLine(ex.Message); } } //输出 //获取长度 int n=0; for (int i = 0; i < ld.Count; i++) { if (ld[i].Length > n) n = ld[i].Length; for (int j = 0; j < ld[i].Length; j++) { Console.Write(ld[i][j] + " "); } Console.WriteLine(); } Console.ReadLine(); Console.WriteLine("计算开始:"); double[] a = new double[n]; double[] b = new double[n]; double[] c = new double[n]; for (int i = 0; i < ld.Count;i++ ) { for (int j = 0; j < ld[i].Length;j++ ) { if (i == j) { b[i] = ld[i][j]; if (i + 1 < n) a[i + 1] = ld[i+1][j]; if (j + 1 < n) c[i] = ld[i][j + 1]; } //else if (i > j) a[i] = ld[i][j]; //else c[i] = ld[i][j]; } } Console.WriteLine("==========A==========="); foreach(double dx in a) { Console.Write(dx + " "); } Console.WriteLine(); Console.WriteLine("==========B==========="); foreach (double dx in b) { Console.Write(dx + " "); } Console.WriteLine(); Console.WriteLine("==========C==========="); foreach (double dx in c) { Console.Write(dx + " "); } Console.ReadLine(); double[] u = new double[n]; double[] l = new double[n]; for (int i = 0; i < n; i++) { u[i] = u_recursion(i, a, b, c); } Console.WriteLine(); Console.WriteLine("==========U==========="); foreach (double dx in u) { Console.Write(dx + " "); } Console.ReadLine(); Console.WriteLine(); Console.WriteLine("==========L==========="); for (int i = 0; i < n;i++ ) { if (i > 0) l[i] = a[i] / u[i - 1]; Console.Write(l[i] + " "); } Console.WriteLine(); Console.WriteLine("==========Y==========="); double[] y = new double[n]; for (int i = 0; i < n;i++ ) { if (i == 0) y[0] = d[0]; else y[i] = d[i] - l[i] * y[i - 1]; Console.Write(y[i] + " "); } Console.WriteLine(); Console.WriteLine("==========X==========="); double[] x = new double[n]; for (int i = 0; i <n;i++ ) { x[i] = x_recursion(i, n, y, u, c); Console.Write(x[i] + " "); } Console.WriteLine(); Console.ReadLine(); } static double u_recursion(int i,double[] a,double[] b, double[] c) { double ui=b[0]; if (i > 0) ui = b[i] - (a[i] * c[i - 1]) / u_recursion(i - 1, a, b, c); return ui; } static double x_recursion(int i,int n,double[] y,double[] u,double[] c) { double x; if (i < n-1) x = (y[i] - c[i] * x_recursion(i + 1, n, y, u, c)) / u[i]; else x = y[i] / u[i]; return x; } }
阅读全文
0 0
- 平方根,立方根,阶乘,追赶法(C#)
- 牛顿迭代法求平方根、立方根
- 牛顿迭代公式计算平方根立方根
- Guass列主元、平方根法、追赶法求解方程组的C++实现
- 追赶法
- 追赶法
- 追赶法
- 华为机试—求数的平方根立方根
- 牛顿迭代法求数的平方根和立方根
- 牛顿迭代法求一个数的立方根、平方根
- POJ1650 Integer Approximation(“追赶法”搜索)
- 一道C语言编程比赛题(时钟追赶问题)
- 牛顿法求解立方根
- 用平方根法解线性方程(c++)
- 平方根(sqrt.pas/c/cpp)(数论)
- 求解线性方程组--追赶法
- 追赶法解线性方程组
- 追赶法解方程
- 线上升级Redis纪实
- oracle sql调优的经历(隐士转换导致索引失效)
- Maven3的jar包上传
- SpringBoot优化内嵌的Tomcat
- 分步积分法
- 平方根,立方根,阶乘,追赶法(C#)
- 【记录】微信录音开发(企业微信JSSDK-下载到自己的服务器-ffmpeg转码h5播放)
- Gradle for Android(四)——依赖冲突解决
- invoke方法反射调用类指定中方法
- 基于php常用函数总结(数组,字符串,时间,文件操作)
- Magic Touch 中的手势识别(一)
- 遍历HashMap值
- BZOJ 1307 [ZJOI2008]生日聚会Party
- <mvc:default-servlet-handler />不能同时访问静态资源和动态资源的问题