一个次数界为n的多项式系数表达式:
上式用霍纳法则表示:
代码实现:利用霍纳法则求多项式的值
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HeNaRule { class Program { static void Main(string[] args) { int x = 5; int[] an = new int[] { 2, 3, 4, 5, 6 }; //计算:2*5^4+3*5^3+4*5^2+5*5^1+6*5^0 int result = CalMultinomial(x, an); Console.WriteLine("计算结果: "+result); Console.Read(); } /// <summary> /// 求多项式的值 /// 霍纳法则算法 /// </summary> /// <param name="x"></param> /// <param name="an"></param> /// <returns></returns> public static int CalMultinomial(int x, int[] an) { int n = an.Length; int result = 0; for (int i = 0; i < n; i++) { result = result * x + an[i]; } return result; } } }
运行测试