写完代码后自认为是正确的,但录入数据后发下答案完全错误…太绝望了
错误代码:
#include <stdio.h>
int main()
{
int n, m;
while ( ~scanf("%d %d", &n, &m) )
{
int sum=0;
int i;
int o = 2;
int times = n/m;
int mark = 0;
if ( n%m==0 )
{
while ( times-- )
{
sum = 0;
for ( i=1; i<=m; i++ )
{
sum += o;
o += 2;
}
if ( mark == 0 )
{
mark = 1;
printf ( "%d", sum );
}
else
printf ( " %d", sum );
}
printf ( "\n" );
}
else
{
int yu = n%m;
while ( times-- )
{
sum = 0;
for ( i=1; i<=m; i++ )
{
sum += o;
o += 2;
}
printf ( "%d ", sum );
}
for ( i=1; i<=yu; i++ )
{
sum = 0;
sum += o;
o += 2;
}
printf ( "%d\n", sum );
}
}
return 0;
}
错误分析:
第一眼人眼检查是并没有发现错误,格式的输出处理也很到位呀,但用debug一分析就发现错误的地方很幼稚,答案求成了总和而非平均数。