Description
I guess that
everybody has learned the course Calculus(微积分). To tell you the
truth, I hate what course because of the endless exercise to do! My
teacher was so strict that he assigned many exercise about
polynomial integration to me. In each problem, he just gave me a
polynomial f(x), starting point xs and ending point xt, and I must
calculate the value of . I don’t want to waste my time on that, I
need a efficient program to help me calculating that! My dear
friend, can you help me?
Input
The first
line of input contains an integer T, indicating test cases.
In each test
case, the first line consist of 3 integers: n, xs, xt. N means that
f(x) consists of n blocks:
Then
following N lines of input, each line consists two integers: ai and
di, representing the coefficient and degree of the i-th part of
f(x), respectively.
1
<= n <= 10,
1
<= xs <= xt <=
100,
-3
<= di <= 3,
0
<= ai <= 20.
Output
In each test
case, the output contains one line.
The only one
line is formatted as "Case #X: Y", where X is the test case number
(starting with 1), Y is the result of , rounded to three decimal
points.
Sample Input
2
2 1 2
2 1
3 2
1 1 5
1 0
Sample Output
Case #1:
10.000
Case #2:
4.000
嘿嘿,竟然考定积分的程序设计,不过没有考虑特殊情况,wa了一次!
#include<iostream>
#include<cmath>
using namespace std;
double a[100],b[100];
int n;
double f(double x)
{
int i;
double s1=0,s2=0;
for(i=0;i<n;i++)
{
if(b[i]!=-1)
s1+=a[i]/(b[i]+1)*pow((double)x,(b[i]+1));
if(b[i]==-1)
s2+=a[i]*log((double) x);
}
return s1+s2;
}
int main()
{
int
z,i,j;
double
x,y;
cin>>z;
for(j=1;j<=z;j++)
{
cin>>n>>x>>y;
for(i=0;i<n;i++)
cin>>a[i]>>b[i];
double l=f(y)-f(x);
cout<<"Case
#"<<j<<":"<<"
";
cout.setf(ios::fixed);
cout.precision(3);
cout<<l<<endl;
}
}