c语言问题——年龄与疾病

某医院想统计一下某项疾病的获得与否与年龄是否有关,需要对以前的诊断记录进行整理,按照0-18、19-35、36-60、61以上(含61)四个年龄段统计的患病人数占总患病人数的比例。
输入
共2行,第一行为过往病人的数目n(0 < n <= 100),第二行为每个病人患病时的年龄。
输出
按照0-18、19-35、36-60、61以上(含61)四个年龄段输出该段患病人数占总患病人数的比例,以百分比的形式输出,精确到小数点后两位。每个年龄段占一行,共四行。
样例输入
10
1 11 21 31 41 51 61 71 81 91
样例输出
20.00%
20.00%
20.00%
40.00%

#include<stdio.h>
main()
{
int n,x,i;
float a,b,c,d;
FILE *fin,*fout;
fin=fopen("nlyjb.in","r");
fout=fopen("nlyjb.out","w");
fscanf(fin,"%d",&n);
for(i=0;i<n;i++)
{fscanf(fin,"%d",&x);
if(x>=0 && x<=18) a++;
if(x>=19 && x<=35) b++;
if(x>=36 && x<=60) c++;
else d++;}
fprintf(fout,"%.2f%% \n%.2f%% \n%.2f%% \n%.2f%% \n",100/a,100/b,100/c,100/d);
fclose(fin);
fclose(fout);
}
请问这道题怎么错的

#include <stdio.h> 



int main()
{
FILE *fin,*fout;
int  num;
int n;
int  a=0, b=0, c=0, d=0;
float a1, b1, c1, d1;
fin = fopen("in.in", "r");
fout = fopen("out.out", "w");
if (fin == NULL)
{
printf("ERROR");
return 0;
}
if (fout == NULL)
{
printf("ERROR");
return 0;
}

fscanf(fin, "%d", &num);

while (fscanf(fin, "%d", &n) !=EOF)
{
printf("%d ", n);
if (n >= 0 && n <= 18) a++;
if (n>= 19 && n<= 35) b++;
if (n >= 36 && n <= 60) c++;
if (n >= 61) d++;
}


a1 = (float)a / (float)num*100;
b1 = (float)b / (float)num * 100;
c1 = (float)c / (float)num * 100;
d1 = (float)d / (float)num * 100;

fprintf(fout, "%.2f%% \n%.2f%% \n%.2f%% \n%.2f%% \n",a1,b1,c1,d1 );
fclose(fin);
fclose(fout);
}

你这应该是fscanf的问题

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答