**
题干要求
其中n是多项式的阶数,a[]中存储系数,x是给定点。函数须返回多项式f(x)的值。 这里主要讲解自定义函数的原理:
一、涉及的数学知识
- 用到多项式的合并与展开
简单 例子:3x^2+2x+1=x(3x+2)+1
- 本题假设我们输入 :n=3 ; x =2.0;
a[0]=1 ; a[1]=1;a[2]=2 ;a[3]=3;
则f(x)=a[0] * (x^0) + a[1] * (x^1)+ a[2] * (x ^2)+a[3] * (x^3)=
x(x(a[3])x+a[2])+a[1])+a[0] ;
用循环计算 x(x(a[3])x+a[2])+a[1])+a[0]; 然后依照先算括号里面的原则计算。
用到的循环语句是 add = add * x + a[i];
add 等于0是为了第一次计算引出a[3];
二、代码实现
#include <stdio.h>
相关文章
- #include <> typedef struct Node { int index; struct Node *next; }JosephuNode; int Josephu(int n, int m) { int i, j; JosephuNode *head, *tail; head = tail = (JosephuNode *)malloc(sizeof(JosephuNode)); for (i = 1; i < n; ++i) { tail->index = i; tail->next = (JosephuNode *)malloc(sizeof(JosephuNode)); tail = tail->next; } tail->index = i; tail->next = head; for (i = 1; tail != head; ++i) { for (j = 1; j < m; ++j) { tail = head; head = head->next; } tail->next = head->next; printf("第%4d个出局的人是:%4d号\n", i, head->index); free(head); head = tail->next; } i = head->index; free(head); return i; } int main { int n, m; scanf("%d%d", &n, &m); printf("最后胜利的是%d号!\n", Josephu(n, m)); system("pause"); return 0; }">设编号为1,2,… n的n个人围坐一圈,约定编号为k(1数组实现: #include <> #include <> int Josephu(int n, int m) { int flag, i, j = 0; int *arr = (int *)malloc(n * sizeof(int)); for (i = 0; i < n; ++i) arr[i] = 1; for (i = 1; i < n; ++i) { flag = 0; while (flag < m) { if (j == n) j = 0; if (arr[j]) ++flag; ++j; } arr[j - 1] = 0; printf("第%4d个出局的人是:%4d号\n", i, j); } free(arr); return j; } int main { int n, m; scanf("%d%d", &n, &m); printf("最后胜利的是%d号!\n", Josephu(n, m)); system("pause"); return 0; } 链表实现: #include <> #include <> typedef struct Node { int index; struct Node *next; }JosephuNode; int Josephu(int n, int m) { int i, j; JosephuNode *head, *tail; head = tail = (JosephuNode *)malloc(sizeof(JosephuNode)); for (i = 1; i < n; ++i) { tail->index = i; tail->next = (JosephuNode *)malloc(sizeof(JosephuNode)); tail = tail->next; } tail->index = i; tail->next = head; for (i = 1; tail != head; ++i) { for (j = 1; j < m; ++j) { tail = head; head = head->next; } tail->next = head->next; printf("第%4d个出局的人是:%4d号\n", i, head->index); free(head); head = tail->next; } i = head->index; free(head); return i; } int main { int n, m; scanf("%d%d", &n, &m); printf("最后胜利的是%d号!\n", Josephu(n, m)); system("pause"); return 0; }
- java实现斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。 n
public class Solution_feibonaqi
{
public int Fibonacci(int n)
{
int result[] =
{ 0, 1 };
if (n < 2)
{
return result[n];
}
int f0 = 0;
int f1 = 1;
int f2 = 0;
for (int i = 2; i <= n; i++)
{
f2 = f1 + f0;
f0 = f1;
f1 = f2;
}
return f2;
}
public static void main(String[] args)
{
Scanner sc = new Scanner;
int n = ;
Solution_feibonaqi fei = new Solution_feibonaqi;
((n));
}
}
- 很多人咨询的问题intel至强e5-2680和I5\i7、L5630X2有什么区别?
- 判断i能否被3和5整除的python表达式为_下列Python语句的运行结果是 x=True; y=False; z=True; if not x or y: print(1) elif not x ...
- iOS中的armv7,armv7s,arm64,i386,x86_64
- R语言中的I(x^2)
- i686和x86_64的区别
- Java第三章习题3-7(1到n的阶乘和
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Administrator
*/
public class Find {
public void main{
int n=9999;
int sum=0;
int k=1,i=1;
for( i=1;i<=10;i++){
k=k*i;
sum=sum+k;
if(sum>9999){
break;
}
}
(i-1);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Administrator
*/
public class Test {
public static void main(String[] args){
Find f=new Find;
;
}
}
- x264 环路滤波原理系列:i_deblocking_filter_alphac0 和 i_deblocking_filter_beta 的应用
- f(x)=∑ i=0 n (a[i]×x i )的计算方法