霍纳法则

作者:追风剑情 发布于:2016-7-22 18:46 分类:Algorithms

一个次数界为n的多项式系数表达式:

22222.jpg

上式用霍纳法则表示:

333333.jpg

代码实现:利用霍纳法则求多项式的值

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace HeNaRule
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. int x = 5;
  13. int[] an = new int[] { 2, 3, 4, 5, 6 };
  14. //计算:2*5^4+3*5^3+4*5^2+5*5^1+6*5^0
  15. int result = CalMultinomial(x, an);
  16.  
  17. Console.WriteLine("计算结果: "+result);
  18. Console.Read();
  19. }
  20.  
  21. /// <summary>
  22. /// 求多项式的值
  23. /// 霍纳法则算法
  24. /// </summary>
  25. /// <param name="x"></param>
  26. /// <param name="an"></param>
  27. /// <returns></returns>
  28. public static int CalMultinomial(int x, int[] an)
  29. {
  30. int n = an.Length;
  31. int result = 0;
  32. for (int i = 0; i < n; i++)
  33. {
  34. result = result * x + an[i];
  35. }
  36. return result;
  37. }
  38. }
  39. }

运行测试

 1111111.jpg

标签: Algorithms

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号