数据结构的题
试编写算法,求一元多项式Pn(x)=a0+a1X+……+anXn的值
#include<stdio.h> #define M 5 int main(void) { int a[M + 1]; int i, n; printf("请顺序输入a0到a%d的这%d个系数:\n", n = M,n+1); for (i = 0; i < M+1; i++) scanf_s("%d", &a[i]); long sum = 0; int x; int y = 1; printf("请输入x的值:\n"); scanf_s("%d", &x); for (i = 0; i < M+1; i++) { sum += a[i] * y; y *= x; } printf("pn(%d)=%ld\n",x, sum); system("pause"); return 0; }
[此贴子已经被作者于2018-10-12 18:00编辑过]
#include<stdio.h> #define M 5 int main(void) { int a[M + 1]; int i, n; printf("请顺序输入a0到a%d的这%d个系数:\n", n = M,n+1); for (i = 0; i < M+1; i++) scanf_s("%d", &a[i]); long sum = 0; int x; int y = 1; printf("请输入x的值:\n"); scanf_s("%d", &x); sum = a[0]; for (i = 1; i < M+1; i++) { y *= x; sum += a[i] * y; } printf("pn(%d)=%ld\n",x, sum); system("pause"); return 0; }