例题2-6 数据统计II(多组数据的统计)

输入一些整数,求出它们的最小值,最大值和平均值(保留3为小数)。输入保证着些数都是不超过1000的整数。

输入包含多组数据,每组数据第一行是一个数n,第二行是n个整数。n=0为输入结束标记,程序应当忽略这组数据。相邻两组数据之间应输出一个空行。

样例输入:

8

2 8 3 5 1 7 3 6

4

-4 6 10 0

0

样例输出:

case 1:1 8 4.375


case 2:-4  10 3.000

//数据统计(多组数据) 
#define LOCAL 
#include <stdio.h>
#define INF 1000000000
main() 
{
	#ifdef LOCAL
	freopen("data1.txt","r",stdin);	
	freopen("data.txt","w",stdout);
	#endif
	int x,s,i,n,j=1;
	while(scanf("%d",&n)&&n!=0)
	{
		//scanf("%d",&n);
		s=0;
		max=-INF,min=INF;
		for(i=1;i<=n;i++)
		{
		  scanf("%d",&x);
		  s+=x;
		  if(min>x)
		  {
			min=x;
		  }
		  if(max<x)
		  {
			max=x;
		  }
		  //i++;
		}
		if(j>1)
		printf("\ncase %d:%d %d %0.3lf\n",j,min,max,(double)s/(i-1));
		else
		printf("case %d:%d %d %0.3lf\n",j,min,max,(double)s/(i-1));
		j++;
	} 
	//printf("%d %d %lf",min,max,(double)s/i) ;
}


猜你喜欢

转载自blog.csdn.net/weixin_27848283/article/details/80979559
今日推荐