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. 产品的主体构件进 ...
随机推荐
- phpcms:七、list.html
1.列表页{pc:content action="lists" catid="$catid" num="25" order="id ...
- js如何判断字符串是否进行过window.btoa()转码
window.btoa()是基于Base64算法的.window.btoa()只能将ASCII字符进行转码 因此我们需要了解Base64的原理及主要特征:Base64的原理在这里就不多说了,网上很多讲 ...
- px 和 em换算
常用px,pt,em换算表 pt (point,磅):是一个物理长度单位,指的是72分之一英寸. px (pixel,像素):是一个虚拟长度单位,是计算机系统的数字化图像长度单位,如果px要换算成物理 ...
- (转载)XML Tutorial for iOS: How To Read and Write XML Documents with GDataXML
In my recent post on How To Choose the Best XML Parser for Your iPhone Project, Saliom from the comm ...
- 利用JConsole工具监控java程序内存和JVM
一.找到java应用程序对应的进程PI 性能测试应用程序访问地址:http://192.168.29.218:7070/training/ 部署的应用服务器为tomcat6.028 启动tomcat服 ...
- wp8模拟器操作键盘
当前焦点在模拟器上的某一个输入控件上时候, 按pagedown/pageup可以切换是用pc键盘还是模拟器键盘
- .net framework版本以及服务器部署问题
自己做了个官网,部署到服务器很多问题,发现targetframework为4.0,但是公司服务器的版本为:2.0.50727,但是公司一个项目用的lambda和linq就没有事,发现这个是3.5支持的 ...
- 7 RandomAccessFile读取文件内容保存--简单例子(需要验证)
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; /** * 读取动态产生的文件内容 */ publ ...
- 4 常量类--Map常量
public static final HashMap<String, String> ETL_SOURCE_INPUTTYPE_MAP = new HashMap<String, ...
- struts.custom.i18n.resources国际化详解(一)
每种框价都会有国际化的支持,struts2的国际化大致上分为页面的国际化,Action的国际化以及xml的国际化 首先在struts.properties文件中加入以下内容:struts.custom ...