輸入數量不確定的[0,9]範圍內的整數,統計每一種數字出現的次數,輸入-1表示結束

#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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章