你的代码呢?
#include <iostream> using namespace std; double f( double x, double a0, double a1=0, double a2=0, double a3=0 ) { return a0+a1*x+a2*x*x+a3*x*x*x; } int main( void ) { double x; size_t n; cin >> x >> n; double a[4] = {}; for( size_t i=0; i!=n+1; ++i ) cin >> a[i]; cout << f(x,a[0],a[1],a[2],a[3]) << endl; }
#include <iostream> using namespace std; double fun( double x, double a0, double a1=0, double a2=0, double a3=0 ) { return a0+a1*x+a2*x*x+a3*x*x*x; } int main( void ) { double x; size_t n; cin >> x >> n; double a0, a1, a2, a3; if( n == 0 ) { cin >> a0; cout << fun(x,a0) << endl; } else if( n == 1 ) { cin >> a0 >> a1; cout << fun(x,a0,a1) << endl; } else if( n == 2 ) { cin >> a0 >> a1 >> a2; cout << fun(x,a0,a1,a2) << endl; } else if( n == 3 ) { cin >> a0 >> a1 >> a2 >> a3; cout << fun(x,a0,a1,a2,a3) << endl; } }