例2-4 文件读写(freopen重定向)
#include<stdio.h> #define INF 1000000000 int file_freopen() { int x,min=INF,max=-INF,S=0,count=0; freopen("E:\\Code\\C\\算法竞赛入门经典\\Debug\\input.txt","r",stdin); freopen("E:\\Code\\C\\算法竞赛入门经典\\Debug\\output.txt","w",stdout); while(scanf("%d",&x)==1) { S+=x; if(x<min) min=x; if(x>max) max=x; count++; } printf("%d %d %.3lf\n",min,max,(double)S/count); return 0; }
例2-4 文件读写(fopen)
#include<stdio.h> #define INF 1000000000 int file_fopen() { int x,min=INF,max=-INF,S=0,count=0; FILE *fin,*fout; fin = fopen("E:\\Code\\C\\算法竞赛入门经典\\Debug\\input.txt","rb"); fout = fopen("E:\\Code\\C\\算法竞赛入门经典\\Debug\\output.txt","wb"); while(fscanf(fin,"%d",&x)==1) { S+=x; if(x<min) min=x; if(x>max) max=x; count++; } fprintf(fout,"%d %d %.3lf\n",min,max,(double)S/count); fclose(stdin); fclose(stdout); return 0; }
习题2-2 水仙花数
#include<stdio.h> int daffodil() { int a,b,c,x; for(a=1;a<=9;a++) { for(b=0;b<=9;b++) { for(c=0;c<=9;c++) { x=a*100+b*10+c; if(x==a*a*a+b*b*b+c*c*c) //ABC=A*A*A+B*B*B*C*C*C printf("%d\n",x); } } } return 0; }
时间: 2024-11-05 14:47:55