c語言!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
按下面的公式求sum的值
程序編程題
sum=m-(m+1)+(m+2)-(m+3)+.....+(n-1)-n
例如,如果m和n的值分別為1和100,只計算結果為-50。
#include "stdio.h"
#include "math.h"
#include "conio.h"
#include "stdlib.h"
fun( int m, int n)
{
/***********begin***********/
/************end************/
}
main()
{ int m, n, t;
scanf("%d,%d",&m,&n);
if(m>n)
{ t=m; m=n; n=t; }
printf("sum=%d\n",fun(m,n));
NONO( );
}
NONO( )
{ FILE *fr, *fw;
int i, m, n;
fr = fopen("C:\\homework\\10010131\\PROGIN.DAT", "r");
fw = fopen("C:\\homework\\10010131\\PROGOUT.DAT","w") ;
for(i=1; i<=5; i++)
{ fscanf(fr,"%d,%d", &m,&n);
fprintf(fw,"sum = %d\n",fun(m,n));}
fclose(fr);
fclose(fw);
}
最佳回答:
/***********begin***********/
long sum,f;
for(sum=0,f=1;m<=n;m++,f=-f)
sum+= m * f;
return sum;
/************end************/