为了编写最简短的解决方案,我可以使用黎曼和来近似积分,我遇到了一个奇怪的问题:如果用户请求的分区计数超过 10,则程序失败。有什么想法吗?这是代码:
// The Integral
#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <stdexcept>
#include <iomanip>
using std::cin; using std::cout;
using std::endl;
int func (int x);
int main ()
{
cout << "Please enter left and right bounds: ";
int left, right;
cin >> left >> right;
cout << "Please enter a # of partitions (>0): ";
int R;
cin >> R;
int width = (right - left) / R;
int total = 0;
for (int i = 0; i < R; ++i) {
total += func(left + width*i);
}
cout << "The integral is: " << total << endl;
return 0;
}
int func (int x)
{
return x*x;
}