#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int i, k, n;
double s = 0, j;
cin >> n;
vector <int> v(n);
for (i = 0; i < n; ++i)
{
cin >> v[i];
s += v[i];
}
s /= 2;
j = s;
sort(v.begin(), v.end());
i = 0;
while (s >= 0)
{
s -= v[i];
++i;
}
k = i;
int z = 0;
i = n - 1;
while (j >= 0)
{
s -= v[i];
++z;
--i;
}
if (n == 1)
cout << 1;
else {
if (k < z)
cout << (k + 1);
else
cout << (z + 1);
}
//system("pause");
return 0;
}

你的程序很有趣......
假设,在输入和
我们得到
j>0。之后这个循环会运行多长时间:直到那一刻
i < 0- 即 数组越界...这就是您在图片中显示的内容。