直方图
复制代码
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 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步