首页 > 代码库 > 《算法竞赛入门经典》第二章 2.3

《算法竞赛入门经典》第二章 2.3

程序2-7

  略去

程序2-8

 1 #define LOCAL 2 #include<stdio.h> 3 #define INF 100000000 4 int main() 5 { 6     #ifdef LOCAL 7     freopen("data.in","r",stdin); 8     freopen("data.out","w",stdout); 9     #endif // LOCAL10     int x, n = 0, min = INF, max = -INF, s = 0;11     while(scanf("%d", &x) == 1)12     {13         s +=x;14         if(x < min) min = x;15         if(x > max) max = x;16         /*17         printf("x = %d, min = %d, max = %d\n ",x, min, max);18         */19         n++;20     }21     printf("%d %d %0.3lf",min, max, (double)s/n);22     return 0;23 }

 

习题 2-9

 1 #include<stdio.h> 2 #define INF 100000000 3 int main() 4 { 5     FILE *fin, *fout; 6     fin = fopen("data.in","rb"); 7     fout = fopen("data.out","wb"); 8     int x, n = 0, min = INF, max = -INF, s = 0; 9     while(fscanf(fin,"%d", &x) == 1)10     {11         s +=x;12         if(x < min) min = x;13         if(x > max) max = x;14         n++;15     }16     fprintf(fout,"%d %d %0.3lf",min, max, (double)s/n);17     fclose(fin);18     fclose(fout);19     return 0;20 }

习题2-8,2-9文件操作,也是最近做论文才熟悉一些的。。。

《算法竞赛入门经典》第二章 2.3