我的代码:
#include <iostream>
#include <vector>
#include <algorithm>
int test_n(std::vector<int>::iterator b, std::vector<int>::iterator e, int &n)
{
n++;
std::vector<int>::difference_type l = e-b;
if (l<100) return std::accumulate(b, e, 0);
std::vector<int>::iterator tmp = b + l/2;
int nL = test_n(b, tmp, n);
int nR = test_n(tmp, e, n);
return nL + nR;
}
int main()
{
int n=0;
std::vector<int> v;
for (int i=1; i<1000; i++) v.push_back(i);
std::cout << test_n(v.begin(), v.end(), n) << " (n=" << n << ")\n";
return 0;
}
为什么n
不至少增加一次?