重定向
#define LOCAL#include<stdio.h>
#define INF 1000000000;
int main()
{
#ifdef LOCAL
freopen("test.in","r",stdin); //注意:【test】是文件名,下方一样
freopen("test.out","w",stdout); //扩展名【.out】不要写错
#endif
int x;
int n=0 ,s=0 ;
int min = INF ;
int max = -INF;
while (scanf("%d",&x)!=EOF)
{
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;
}
在算法竞赛中,选手应该严格遵守比赛的文件名规定,包括程序文件名和输入输出文件名。不要弄错大小写,不要拼错文件名,不要使用绝对路径或相对路径。
比赛时,删除#define LOCAL即可
fopen
#include<stdio.h>
#define INF 1000000000int main()
{
FILE*fin,*fout;
fin = fopen("test(fopen版).in","rb");
fout = fopen("test(fopen版).out","wb");
int x;
int n = 0 , s = 0;
int min = INF ;
int max = -INF ;
while(fscanf(fin,"%d",&x)==1)
{
s+=x;
if(x<min) min = x;
if(x>max) max = x;
n++;
}
fprintf(fout,"%d %d #.3f\n",min,max,(double)s/n);
fclose(fin);
fclose(fout);
return 0;
}
如果想把fopen版的程序改成读写标准输入输出,只需赋值“fin=stdin;fout=stdout”即可,不要调用fopen和fclose。