程序2-10 数据统计 (重定向版)

来源:互联网 发布:知微传播分析 编辑:程序博客网 时间:2024/05/30 23:55


统计数据的最值,及平均值。

#define LOCAl#include<stdio.h>#define INF 1000000000int main(){#ifdef LOCAL    freopen("data.in","r",stdin);    freopen("data.out","w",stdout);#endif    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;}


此代码在本机测试时用文件重定向,但一旦提交到比赛,就自动”删除“重定向语句。


重定向的部分被写在#ifdef和#endif中。其含义是:

只有定义了符号LOCAL,才编译两条freopen语句。


0 0