霍纳(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. 分页标签:pager-taglib使用指南

    一简介, Pager-taglib,支持多种风格的分页显示.实际上她是一个Jsp标签库,为在JSP上显示分页信息而设计的一套标签,通过这些标签的不同的组合,会形成多种不一样的分页页面,风格各异,她自带 ...

  2. OC 图片圆角实现

    self.imageTouX.layer.masksToBounds=YES; self.imageTouX.layer.cornerRadius=/2.0f; //设置为图片宽度的一半出来为圆形 s ...

  3. C++11 多线程 基础

    C++11开始支持多线程编程,之前多线程编程都需要系统的支持,在不同的系统下创建线程需要不同的API如pthread_create(),Createthread(),beginthread()等,使用 ...

  4. JavaScript函数 bind call apply区别

    1. apply calll 在JavaScript中 call 和 apply 都是为了改变某个函数运行时上下文而存在的, 换句话说就是为了改变函数内部的this的指向. 这里我们有一个新的对象 b ...

  5. SSH 服务启动时出现如下错误:fatal: Cannot bind any address

    注意:本文相关配置及说明已在 CentOS 6.5 64 位操作系统中进行过测试.其它类型及版本操作系统配置可能有所差异,具体情况请参阅相应操作系统官方文档. 问题描述 云服务器 ECS (Elast ...

  6. jquery选择指定元素之外的所有元素

    最近的项目中有这么一个需求,点击一排图片中的任意一张后底部弹出一个对话框,要求点击任意地方隐藏对话框 这个时候用not()显然是不现实的,用closest()可以实现差不多的功能 <!DOCTY ...

  7. Android之Activity启动的源码简介

    从一个简单的startActivity开始 进入了Activity.java public void startActivity(Intent intent) { this.startActivity ...

  8. Proguard 保留native methods的问题

    发现一个奇怪的问题,如果使用下面的配置来keep的话,native的方法还是被删掉了,百思不得其解. -keepclasseswithmembers class * {     native *; } ...

  9. js中的因数分解

    方法一: <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8& ...

  10. win7添加usb3.0驱动(错误代码1392,文件或目录损坏且无法读取)

    Win7添加usb3.0驱动 之前一直按照网上的方法执行dism命令挂载时,总是失败,错误代码1392,显示原因是文件或目录损坏且无法读取.这个错误以前在装机时老是出现导致系统安装不成功,在BIOS中 ...