这两个程序没什么不同,为什么一个运行结果正确,一个错误呢?请指教
#include<stdio.h>
int main()
{
int m, n, i, line[104], s, j, h;
int aver1, aver2;
while( scanf( "%d%d",&m,&n ) != EOF )
{
line[0] = 2;
for( i = 1; i < n; i++ )
line[i] = line[i-1] + 2;
h = n % m;
for( j = 0; j < n / m; j++ )
{
s = 0;
for( i = j * m; i < ( j + 1) * m; i++ )
s = s + line[i];
aver1 = s / m;
if( j == 0 )
printf( "%d",aver1);
else
printf( " %d", aver1);
}
if( h != 0)
{
s = 0;
for( i = n - h; i < n ; i++ )
s = s + line[i];
aver2 = s / h;
printf( " %d",aver2 );
}
printf( "\n");
}
return 0;
}
_____________________________________________________________________________________________________________________
#include<stdio.h>
int main()
{
int m,n,i,line[104],s,j,h;
int aver1,aver2;
while( scanf( "%d%d",&n,&m ) != EOF )
{
line[0]=2;
for( i = 1; i < n; i++ )
line[i]=line[i-1]+2;
h = n % m;//这是什么
for( j = 0; j < n / m; j++ )//为什么要是n/m
{
s = 0;
for( i = j * m; i < (j+1) * m; i++ )//好复杂,没看懂
s = line[i] + s;
aver1 = s / m;
if( j == 0 )
printf( "%d",aver1 );
else printf( " %d",aver1 );
}
if( h != 0 )
{
s = 0;
for( i = n - h; i < n;i++ )
s = s + line[i];
aver2 = s / h;
printf( " %d",aver2 );
}
printf( "\n" );
}
return 0;
}