直方图
1 #include <iostream> 2 #include <string> 3 #include <algorithm> 4 using namespace std; 5 6 int main(){ 7 string a; 8 while (cin >> a){ 9 int i, j, max = 0, num[10] = { 0 }; 10 for (i = 0; i<a.length(); i++) 11 num[a[i] - '0']++; 12 for (i = 0; i<10; i++) 13 if (num[i]>max) 14 max = num[i]; //找到最大次数,即柱状图层数 15 for (i = max; i>0; i--){ 16 for (j = 0; j<10; j++){ 17 if (i>num[j]) 18 cout << ' '; else cout << '*'; 19 } 20 cout << endl; 21 } 22 cout << "0123456789" << endl; //标出横轴 23 } 24 return 0; 25 }