#include
struct numCountStruct {
int n;
int count;
} numCount[10000];
int main() {
int count = 0; // 数字计数
int n;
FILE *fp = fopen("file.txt", "r");
while (~fscanf(fp, "%d", &n)) { // 处理到文件末尾
int i;
for (i = 0; i < count; i++) {
if (numCount[i].num == n) { // 发现已经出现过,出现数+1
numCount[i].count++
}
}
if (i == count) { // 新的数字,添加到数组末尾
numCount[count++] = {n, 1};
}
}
fclose(fp);
for (int i = 0; i < count; i++) {
printf("%d presents %d times.\n", numCount[i].n, numCount[i].count);
}
}
最终arr数组里就是统计的结果
int n;
int times[20]={0};
FILE *fp=fopen("test.txt","r");
for(int i=0;i<50;i++){
fscanf(fp,"%d",&n);
times[n-1]++; //出现次数
}
for(int i=0;i<50;i++){
if(times[i]!=0)printf("%d: %d%%",i+1,times[i]*2);
}