霍纳(Horner)规则是采用最少的乘法运算策略,求多项式 A(x) = a[n]x^n + a[n-1]x^(n-1) + ... + a[1]x^1 + a[0]x^0 在x处的值。

该规则为 A(x) = (...((a[n]x + a[n-1])x + ... + a[1])x + a[0])。利用霍纳规则,编写C语言程序对多项式进行求值。

解:

分别用迭代和递归两种方法来实现,解题代码分别如下:

<1> 迭代:

#include <stdio.h>
int horner(int *array, int n, int x)
{
    int result = array[n];
    while(n)
    {
        result = result * x + array[--n];
    }
    return result;
}
 
int main(void)
{
    int result = 0,
        i = 0,
        x = 0,
        amount = 0;
    do
    {
        printf("Input the amount of coefficients(include zero.):");
        scanf("%d", &amount);
    } while (amount <= 0);
 
    int a[amount];
    printf("Input the cofficients:");
    for (i = 0; i < amount; ++i)
    {
        scanf("%d", &a[i]);
    }
 
    printf("Input the x:");
    scanf("%d", &x);
 
    result = horner(a, amount - 1, x);
    printf("The result is :%d", result);
    return 0;
}
 
<2> 递归:
#include <stdio.h>
int horner(int *array, int n, int x)
{
    if ( !n )
    {
        return *array;
    }
    return horner(array + 1, n - 1, x) * x + *array;
}
 
int main(void)
{
    int result = 0,
        i = 0,
        x = 0,
        amount = 0;
    do
    {
        printf("Input the amount of coefficients(include zero.):");
        scanf("%d", &amount);
    } while (amount <= 0);
 
    int a[amount];
    printf("Input the cofficients(Like:x,y,z  or  x y z):");
    for (i = 0; i < amount; ++i)
    {
        scanf("%d", &a[i]);
    }
 
    printf("Input the x:");
    scanf("%d", &x);
 
    result = horner(a, amount - 1, x);
    printf("The result is :%d", result);
    return 0;
}

 

     

Horner规则的更多相关文章

  1. 多项式求值问题(horner规则)——Python实现

    # 多项式求值(Horner规则) # 输入:A[a0,a1,a2...an],x的值 # 输出:给定的x下多项式的值p   # Horner迭代形式实现 1 # 在此修改初值 2 A = [2, 6 ...

  2. Horner规则求多项式

    /* Horner */ /*多项式:A(x)=a[n]X^n+a[n-1]x^n-1+...+a[1]X^1+a[0]X^0*/ #include <stdio.h> long int ...

  3. 使用Horner法则计算多项式的值

    计算Pn(x) = an * x^n + an-1 * x^(n-1) + ... + a1 * x + a0 直接计算,需要做的乘法次数 1+2+3+……+n = n(1+n)/2 = O(n2) ...

  4. Partitioning, Shuffle and sort

    Partitioning, Shuffle and sort  what happened? - Partitioning Partitioning is the process of determi ...

  5. SICP-2锻炼.34

    [锻炼2.34] 为x给定值,找到一个多项式x的值,它也可以被形式化为累积. 下多项式的值: an*x^n + an-1*x^n-1 + .... + a1*x + a0 採用著名的Horner规则, ...

  6. shell编程基础(转载)

    Shell编程基础 原作者 Leal:请参阅页面底部的编者列表. 授权许可: 创作共享署名协议 GNU 自由文档许可证 注意:本文仍然在持续的修订之中,且错漏之处可能较多.如果能够阅读英语的话,可以考 ...

  7. Shell脚本基础学习

    Shell脚本基础学习 当你在类Unix机器上编程时, 或者参与大型项目如k8s等, 某些框架和软件的安装都是使用shell脚本写的. 学会基本的shell脚本使用, 让你走上人生巅峰, 才怪. 学会 ...

  8. Yii1.1的验证规则

    在Yii1.1的数据验证是由CValidator完成,在CValidator中提供了各种基本的验证规则 <?php public static $builtInValidators=array( ...

  9. iOS之应用版本号的设置规则

    版本号的格式:v<主版本号>.<副版本号>.<发布号>  版本号的初始值:v1.0.0 管理规则: 主版本号(Major version) 1.  产品的主体构件进 ...

随机推荐

  1. Android初学:联系创建Activity

    public class Activity2 extends Activity{ @Override protected void onCreate(Bundle savedInstanceState ...

  2. 【转】AAC ADTS格式分析

    1.ADTS是个啥 ADTS全称是(Audio Data Transport Stream),是AAC的一种十分常见的传输格式. 记得第一次做demux的时候,把AAC音频的ES流从FLV封装格式中抽 ...

  3. NetAnalyzer笔记 之 二. 简单的协议分析

    [创建时间:2015-08-27 22:15:17] NetAnalyzer下载地址 上篇我们回顾完了NetAnalyzer一些可有可无的历史,在本篇,我决定先不对NetAnalyzer做介绍,而是先 ...

  4. 20个经典bootsrtap后台html站点模板推荐

    今天为大家推荐20款不同风格的Bootstrap后台管理模板,每一款都经典可用,能预览和下载,保证让你挑得眼花缭乱. 1,Simpli flag蓝色 Simpli Flat蓝色管理模板是一款採用Fla ...

  5. java通过jsp的Excel导出

    在项目中一般导出报表用poi,可是假设你不想用框架就用简单的jsp也能够实现报表导出.并且实现起来还特别简单. 先看一下效果截图: 点击导出后的效果截图: 详细实现: 第一:在页面的列表页面中就是普通 ...

  6. SDK命令行操作

    * 使用前需要先在path中添加Android SDK的环境变量,跟Java JDK的配置相同 我当前目录如下:F:\Program\Android SDK\tools:F:\Program\Andr ...

  7. 重写OnPaint事件对窗体重绘(显示gif动画) 实例2

    /// <summary> /// 可显示Gif 的窗体 /// </summary> public class WinGif : Form { private Image _ ...

  8. AngularJs练习Demo3

    @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...

  9. 全世界最详细的图形化VMware中linux环境下oracle安装(三)【weber出品必属精品】

    数据库软件和数据库都建好了,基本上可以说完成90%的工作,但是美中不足的就是企业管理器还没有安装好,现在我们就开始安装企业管理器吧. 安装之前我们先将补丁给补上.补丁我们也是采用禁默安装.补丁:p83 ...

  10. hdu 3460

    算法:字典树 题意:给你一些单词,有一台打印机只能进行以下三种操作 1.读入 2.删除 3.打印 让你输出最少的操作次数将这些单词全部打印出来: (字典树节点-1)*2  表示读入和删除操作: 打印操 ...