霍纳(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. Codeforce 216 div2

    D 只要搞清楚一个性质:确定了当前最大和次大的位置,局面就唯一确定了; 根据这个性质设计dp,统计到达该局面的方法数即可. E 询问的要求是: 求有多少个区间至少覆盖了询问的点集中的一个; 转化成逆命 ...

  2. 代理delegate、NSNotification、KVO在开发中的抉择

    在开发ios应用的时候,我们会经常遇到一个常见的问题:在不过分耦合的前提下,controllers间怎么进行通信.在IOS应用不断的出现三种模式来实现这种通信: 1.委托delegation: 2.通 ...

  3. awk实践积累

    #cat iii |awk '{max=$2;min=$2;for (i=2;i<=NF;i++) if ($i>max) max=$i fi} {for (i=2;i<=NF;i+ ...

  4. Linux内核设计基础(十)之内核开发与总结

    (1)Linux层次结构: (2)Linux内核组成: 主要由进程调度(SCHED).内存管理(MM).虚拟文件系统(VFS).网络接口(NET)和进程间通信(IPC)等5个子系统组成. (3)与Un ...

  5. VB6.0连接MySQL数据库

    VB6.0连接MySQL数据库

  6. vim的用法

    1. vi 与 vim 有什么区别呢,它们之间有什么关系?Vim是从Vi发展出来的一个文本编辑器,可以看作是vi的升级版.Vim的主要功能与原始的Vi完全兼容,vi不会显示颜色,而vim会根据文件内容 ...

  7. sql获取第n条数据

    select * from (select top n * from students) aa where not exists(select * from (select top n-1 * fro ...

  8. Java中关于OOM的场景及解决方法

    原文地址:http://developer.51cto.com/art/201112/305696.htm 1.OOM for Heap=>例如:java.lang.OutOfMemoryErr ...

  9. iOS_SN_深浅拷贝( 百度的)_转载

    文章原地址:http://www.cnblogs.com/5ishare/p/4362459.html 深浅拷贝前提是:是实现NSCopying或者NSMutableCopying协议. 浅拷贝只是复 ...

  10. (一)CodeMirror - 基本应用

    基本引用: <link rel="stylesheet" href="../lib/codemirror.css"> <script src= ...