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. 产品的主体构件进 ...
随机推荐
- 【POJ2242】The Circumference of the Circle(初等几何)
已知三点坐标,算圆面积. 使用初等几何知识,根据海伦公式s = sqrt(p(p - a)(p - b)(p - c)) 和 外接圆直径 d = a * b * c / (2s) 来直接计算. #in ...
- Windows系统结构
四种用户模式进程:1.系统支持进程,比如登录进程和会话管理器,并不是Windows服务,不有服务控制管理器启动2.服务进程,一些以Windows服务方式来运行的组件3.用户应用进程4.环境子系统服务器 ...
- 马士兵 Servlet_JSP(2) JSP源代码)
1.最简单的JSP HelloWorld.jsp <html> <head> <title>Hello</title> ...
- CentOS系统、Jdk、Tomcat安装实战
CentOS系统.Jdk.Tomcat安装实战 第一次接触Liunx系统,都说J2EE系统在Li ...
- winzip15.0注冊码
username:Juzhaofeng 授权码:MPZRP-Y7LWW-K1DKG-FM92E-2C5F5-ZEKFF
- webService返回自定义类型的数据处理
1.自定义一个Student 数据类型: package com.chnic.webservice; import java.io.Serializable; public class Student ...
- Android Action Bar 详解篇 .
作者原创,转载请标明出处:http://blog.csdn.net/yuxlong2010 作为Android 3.0之后引入的新的对象,ActionBar可以说是一个方便快捷的导航神器.它可以作为活 ...
- Builder模式 初体验
看来Java构造器模式,决定动手体验下.构造器模式是什么?干什么用的?推荐大家看下ITEYE的一篇文章 http://www.iteye.com/topic/71175 了解构 ...
- css3 tranform transition animation
tranform:对象图形变形 tranform的属性包括: 1.none 表示不进行变换: 2.rotate 旋转 transform:rotate(20deg) 旋转 ...
- 提高jQuery执行效率
1. 使用最新版本的jQuery jQuery的版本更新很快,你应该总是使用最新的版本.因为新版本会改进性能,还有很多新功能. 下面就来看看,不同版本的jQuery性能差异有多大.这里是三条最常见的j ...