首页 > 编程语言 > 详细

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

时间:2014-12-28 14:04:28      阅读:286      评论:0      收藏:0      [点我收藏+]

程序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 // LOCAL
10     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

原文:http://www.cnblogs.com/LzKlyhPorter/p/4189850.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!