Horner规则
霍纳(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;
}
#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规则的更多相关文章
- 多项式求值问题(horner规则)——Python实现
# 多项式求值(Horner规则) # 输入:A[a0,a1,a2...an],x的值 # 输出:给定的x下多项式的值p # Horner迭代形式实现 1 # 在此修改初值 2 A = [2, 6 ...
- 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 ...
- 使用Horner法则计算多项式的值
计算Pn(x) = an * x^n + an-1 * x^(n-1) + ... + a1 * x + a0 直接计算,需要做的乘法次数 1+2+3+……+n = n(1+n)/2 = O(n2) ...
- Partitioning, Shuffle and sort
Partitioning, Shuffle and sort what happened? - Partitioning Partitioning is the process of determi ...
- SICP-2锻炼.34
[锻炼2.34] 为x给定值,找到一个多项式x的值,它也可以被形式化为累积. 下多项式的值: an*x^n + an-1*x^n-1 + .... + a1*x + a0 採用著名的Horner规则, ...
- shell编程基础(转载)
Shell编程基础 原作者 Leal:请参阅页面底部的编者列表. 授权许可: 创作共享署名协议 GNU 自由文档许可证 注意:本文仍然在持续的修订之中,且错漏之处可能较多.如果能够阅读英语的话,可以考 ...
- Shell脚本基础学习
Shell脚本基础学习 当你在类Unix机器上编程时, 或者参与大型项目如k8s等, 某些框架和软件的安装都是使用shell脚本写的. 学会基本的shell脚本使用, 让你走上人生巅峰, 才怪. 学会 ...
- Yii1.1的验证规则
在Yii1.1的数据验证是由CValidator完成,在CValidator中提供了各种基本的验证规则 <?php public static $builtInValidators=array( ...
- iOS之应用版本号的设置规则
版本号的格式:v<主版本号>.<副版本号>.<发布号> 版本号的初始值:v1.0.0 管理规则: 主版本号(Major version) 1. 产品的主体构件进 ...
随机推荐
- poj 1328 Radar Installation(贪心)
Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...
- Fiddler 抓取eclipse中的请求
Fiddler 抓取eclipse中的请求 代码中添加 System.setProperty("http.proxySet", "true"); System. ...
- IOS深入学习(9)之Objective-C
1 前言 今天我们来解除一篇有关Objective-C的介绍文章,详情如下. 原文链接:http://blog.csdn.net/developer_zhang/article/details/120 ...
- traceroute原理
traceroute原理 ICMP ICMP全称为Internet Control Message Protocol,即,网络控制报文协议. 当一个IP数据报发送失败时,最后一个路由器会向发送发传递一 ...
- JavaScript学习笔记:检测数组方法
检查数组的方法 很多时候我们需要对JavaScript中数据类型(Function.String.Number.Undefined.Boolean和Object)做判断.在JavaScript中提供了 ...
- 解决System.Data.SQLite兼容32位和64位问题
将当前说明文档的目录下的x64.x86目录和System.Data.SQLite.dll文件复制到您的应用程序根目录中(注意更新引用,引用System.Data.SQLite.dll即可,两目录中的不 ...
- Javascript进阶篇——(DOM—认识DOM、ByName、ByTagName)—笔记整理
认识DOM文档对象模型DOM(Document Object Model)定义访问和处理HTML文档的标准方法.DOM 将HTML文档呈现为带有元素.属性和文本的树结构(节点树). 将HTML代码分解 ...
- Django REST Framework学习——Android使用REST方法访问Diango
本文更应该叫做Android如何模拟浏览器访问Django服务器后台. 环境为: Android通过HttpClient访问服务器,从Django中获取json数据,解析显示在UI界面上. 问题为: ...
- 基于注释的Spring Security实战
一.准备工作 预准备的工具及软件有: 1. Eclipse IDE:我使用Eclipse JEE 3.7版,即eclipse-jee-indigo-SR2-win32-x86_64.zip 2. JD ...
- hdu 1196
Problem Description Given an positive integer A (1 <= A <= 100), output the lowest bit of A. F ...