p2141
对着别人的题解敲了好多次都是错的
别人的题解
#include <bits/stdc++.h>
using namespace std;
int n, a[105], b[20005], c[20005], cnt=0 ;
int main()
{
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a[i];
c[a[i]] = 1;
}
for (int i = 0; i < n-1; i++)
{
for (int j = i + 1; j < n; j++)
{
b[a[i] + a[j]]++;
}
}
for (int i = 1; i < 20005; i++)
{
if (c[i] && b[i] > 0)
cnt++;
}
cout << cnt<<endl;
return 0;
}
我敲的wrong代码
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, a[105], b[20005], c[20005], cnt = 0;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a[i];
c[a[i]] = 1;
}
for (int i = 0; i < n-1; i++)
{
for (int j = i + 1; j < n; j++)
{
b[a[i] + a[j]]++;
}
}
for (int i = 1; i < 20005; i++)
{
if (c[i] && b[i] > 0)
cnt++;
}
cout << cnt;
return 0;
}
为什么把变量声明在主函数里面会错啊