我正在执行编程任务,当我打印函数“ explode”找到的值时,它会输出一些甚至没有存储在向量中的奇怪值,例如“ 80”和“ 112”。有人能帮我找到为什么不在我的向量中时为何输出值80和112吗?
输入
6
8
5
6
13
3
4
我已经测试过看向量的内容,它们不包含程序输出的这些奇怪值。
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
typedef long long ll;
using namespace std;
ll N, ans=0, cnt=1;
vector<ll> a;
void explode(ll ind, ll rad, ll dir)
{
if (dir==1 && ind!=N-1)
{
ll i = ind+1, explode_count=0;
while (a[i]<=a[ind]+rad) {cnt++; cout << a[i] << "\n"; i++; explode_count++;}
if (explode_count>=1) explode(i-1, rad+1, 1);
}
else if (dir==-1 && ind!=0)
{
ll i = ind-1, explode_count=0;
while (a[i]>=a[ind]-rad) {cnt++; cout << a[i] << "\n"; i--; explode_count++;}
if (explode_count>=1) explode(i+1, rad+1, -1);
}
}
int main()
{
ofstream fout("angry.out");
ifstream fin("angry.in");
fin >> N;
for (ll i = 0; i < N; i++)
{
ll c;
fin >> c;
a.push_back(c);
}
sort(a.begin(), a.end());
for (ll i = 0; i < N; i++)
{
explode(i,1,1);
explode(i,1,-1);
ans=max(ans, cnt);
cnt=1;
}
fout << ans << "\n";
return 0;
}