Polynomial Integration<Cont…

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;
    }
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值