假定多项式的形式为xn+xn-1+…+x2+x+1,请计算给定单精度浮点数x和正整数n值的情况下这个多项式的值。
2.0 4
31.00
1 de<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 int main() 7 { 8 float x; 9 int n; 10 float tot=0; 11 cin>>x>>n; 12 int a=n; 13 for(int i=n;i>=1;i--) 14 { 15 tot=tot+pow(x,a); 16 a--; 17 } 18 printf("%.2f",tot+1); 19 return 0; 20 }
原文:http://www.cnblogs.com/zwfymqz/p/6480898.html