题目
代码
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
double a,b,interval,sum;
int n,i;
double left;
while(sc.hasNext())
{
a=sc.nextDouble();
b=sc.nextDouble();
n=sc.nextInt();
interval=(b-a)/n;
sum=0;
for(i=0,left=a;i<n;i++)
{
double tmp=(left*left+(left+interval)*(left+interval))*interval/2;
/*
* 定积分约等于求面积
* 左边高:left*left
* 右边高:(left+interval)*(left+interval)
* 长度:interval
* 梯形求面积:记得除以2
*/
sum+=tmp;
left+=interval;
}
System.out.printf("%.3f\n",sum);
}
sc.close();
}
}