这题还是挺简单的,就是需要注意细节。
解题思路:
- 为了方便打印统计图,我们必须先提前统计字符串中每个大写字母的个数。
- 我们用变量 表示出现次数最多的字符的出现次数。从 遍历到 。再在循环里面写一个循环,遍历大写字母。如果该字母出现了 次就打印一个
*。这就是打印统计表的方法,就是一层一层的打印。 - 最后每输出一层记得换行就可以了。
#include <bits/stdc++.h>
using namespace std;
int sum[1005], maxi = -1e9;
int main(){
string tmp, s = "";
for(int i = 1; i <= 4; i++){
getline(cin, tmp);
s += tmp;
}
for(int i = 0; i < s.size(); i++){
if(s[i] >= 'A' && s[i] <= 'Z') sum[s[i]]++;
if(sum[s[i]] > maxi) maxi = sum[s[i]];
}
for(int i = maxi; i >= 1; i--){
for(int j = 'A'; j <= 'Z'; j++){
if(sum[j] >= i) cout << "* ";
else cout << " ";
}cout << endl;
}
cout << "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z";
return 0;
}后记:跟着 Charged_Charge 写题解你的估值就不会掉。