My Problem
I can’t AC this problem, im using a brute force methode, but it just doesnt work for some reason. Someone please help me!
My Code
#include <iostream>
using namespace std;
int p[105];
int main()
{
int n;
cin>>n;
int ans = 0;
for (int i = 0; i < n; i++)
cin>>p[i];
for (int i = 0; i < n; i++)
{
for (int j = i+1; j < n; j++)
{
int avg = 0;
for (int k = i; k <= j; k++)
avg += p[k];
avg /= j - i;
for (int k = i; k <= j; k++)
if (p[k] == avg)
{
++ans;
break;
}
}
}
cout << ans;
return 0;
}
I checked the solution, and it was basically the exact same code as me, the only difference was instead of iterating from i+1 to n, it iterated from i to n. They also used doubles for avg variable.
Here is my code after looking at the solution (the AC version):
#include <iostream>
using namespace std;
int p[105];
int main()
{
int n;
cin>>n;
int ans = 0;
for (int i = 0; i < n; i++)
cin>>p[i];
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
double avg = 0;
for (int k = i; k <= j; k++)
avg += p[k];
avg /= j - i + 1;
for (int k = i; k <= j; k++)
if (p[k] == avg)
{
++ans;
break;
}
}
}
cout << ans;
return 0;
}