输入数量不确定的[0,9]范围内的整数,统计每一种数字出现的次数,输入-1表示结束

    技术2023-04-11  75

    #include<stdio.h> int main() { const int number = 10; int x; int count[number]; //定义数组 int i; //数组初始化 for(i=0; i<number; i++){ count[i] = 0; } scanf("%d",&x); while(x != -1){ if(x >=0 && x<= 9){ count[x] ++; //使用数组 } scanf("%d", &x); } // 遍历数组 for(i = 0; i<number; i++){ printf("%d:%d\n", i, count[i]); } return 0; }

     

    Processed: 0.012, SQL: 9