C語言求平均值

//#define LOCAL
#include <stdio.h>
#define INF 1000000000

int main()
{
	#ifdef LOCAL
	freopen("data.in","r",stdin);
	freopen("data.out","w",stdout);
	#endif
	
	//FILE *fin, *fout;
	//fin = fopen("data.in","rb");
	//fout = fopen("data.out","wb");
	
	int x, n = 0,min = INF, max = -INF, s = 0;
	while(scanf("%d", &x) == 1)
	{
		s += x;
		if(x < min) min = x;
		if(x > max) max = x;
		
		printf("x = %d, min = %d, max = %d\n",x, min, max);
		n++;
	}
	
	printf("%d %d %.3f\n",min, max, (double)s/n);
	return 0;
}


1. scanf() 函數有返回值,返回成功輸入的變量的數量

2. 在Windows下輸入完畢後先按回車鍵,再按ctrl + z , 再按回車鍵,結束scanf輸入

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章