1、问题
读取范围在1和标准输入读取的size之前每个数据出现的次数
2、代码实现
#include <stdio.h>
#include <stdlib.h>
#include <string.h>//读取范围在1和标准输入读取的size之前每个数据出现的次数
int *count(int size)
{if (size <= 0)return NULL;int value, *array = NULL;array = calloc(size, sizeof(int));if (array == NULL)return NULL;printf("请输入1到%d范围的数据\n", size);while (scanf("%d", &value) != EOF){if (value <= 0 && value > size){return NULL;}array[value - 1] += 1;}return array;}int main()
{puts("请输入你需要size的大小");int size;scanf("%d", &size);int *array = NULL;array = count(size);if (array != NULL) {for (int i = 0; i < 10; i++){printf("%d has %d count\n", i + 1, array[i]); }free(array);}return 0;
}
<