逻辑有点混乱,帮你理清了一下
#include
using namespace std;
int main()
{
int n, m, i, j, s, b, d, e, f, g, h, k, l;
int a[101];
while (cin >> n >> m)
{
s = 0;
e = 0;
g = 1;
k = 0;
h = m;
d = n / m;//每m个数取平均值,可以取d次//
f = n%m;//剩余f个数//
for (i = 1; i <= n; i++)
{
a[i] = 2 * i;
}
//将以2为首项的偶数递增数列存入数组a//
for (j = g; j <= h; j++)
{
s = s + a[j];
e++;
if (e == m)
{
b = s / m;
if (k > 0) cout << " ";
cout << b;
g = g + m;
h = h + m;
e = 0;
s = 0;
k++;
}
if (k == d)
{
break;
}
}
//计算每m个数的平均数//
if (f > 0)
{
s = 0;
for (i = h-m+1; i <= n; i++)
{
s = s + a[i];
}
l = s / f;
cout << " " << l << endl;
}
else
{
cout << endl;
}
//计算剩余f个数的平均数//
}
return 0;
}