只是一道入门题,谈谈我作为新人的易错点qaq
循环内int i=?其实没有固定的值,问题在于能否能使循环次数不多不少;
要注意特别是循环中else用不用,用在哪个地方,是否在条件执行时对结果产生错误影响,比如这道题当n==k时就可能影响得出的Bsum数;
(如果i!=k而是i=1)```cpp
#define _CRT_SECURE_NO_WARNINGS
#include
#include <stdio.h>
using namespace std;
int main()
{
int a = 0, b = 0, c = 0, d = 0, i = 1, j = 0; double h = 0, l = 0, e = 0, f = 0;
scanf("%d%d", &a, &b);
for (i = 1; i <= a; i++) {
j++;
if (j % b == 0) {
c = c + j;
h++;
}
}
d = a * (a + 1) / 2 - c;
l = a - h;
e = double(c);
e = e / h;
f = double(d);
f = f / l;
printf("%.1lf %.1lf%.1lf %.1lf", e, f,h,l);
return 0;
}