求助!看着和第一个样例输出结果一样呀,咋一个也通不过

P1598 垂直柱状图

@[QYlucky](/user/689435) 解题思路:用一个数组统计每个字母出现的总次数,并找出字母出现的最大次数。用一个 for 循环从最大次数到 1 进行遍历,依次判断每个字母出现的次数是否达到了当前层数,然后输出
by wunaidedanjuan @ 2023-08-29 17:46:57


@[QYlucky](/user/689435) 微调代码(调整地方已注释): ```cpp #include<bits/stdc++.h> using namespace std; int main(){ string s; char word[]={'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'}; int num[30]={0}; char ans[30][100]={' '}; for(int i=0;i<4;i++){ getline(cin,s); int len=s.length(); for(int j=0;j<len;j++){ for(int k=0;k<26;k++){ if(s[j]==word[k]){ num[k]+=1;//k 的值不需要额外更改 break; } } } } // for(int j=0;j<26;j++){ // ans[0][j]=word[j]; // } // for(int i=0;i<26;i++){ // for(int j=1;j<=num[i];j++){ // ans[j][i]='*'; // } // } int max=0; for(int i=0;i<26;i++){ if(num[i]>max) max=num[i]; } for(int i=max;i>=1;i--){//从 max 到 1 遍历 for(int j=0;j<26;j++){ if(num[j]>=i) cout<<"*"; else cout<<" "; if(j!=25) cout<<" "; // if(j!=25) cout<<ans[i][j]<<" "; // else cout<<ans[i][j]; } cout<<endl; } for(int i=0;i<=25;i++)//输出字母 { cout<<word[i]; if(i!=25) cout<<" "; } return 0; } ``` 附本人 AC 代码 ```cpp #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<cmath> #include<ctime> #include<queue> #include<string> #include<bitset> #include<cctype> #include<cstdlib> #include<functional> #include<istream> #include<sstream> #include<streambuf> #define ll long long using namespace std; const int N=200; int a[N]; int main() { // freopen("a.out","w",stdout); memset(a,0,sizeof(a)); string s; int maxn=0; for(int i=1;i<=4;i++) { getline(cin,s); for(int j=0;j<s.length();j++) { if(s[j]>='A'&&s[j]<='Z') { a[(int)s[j]-64]++; maxn=max(maxn,a[(int)s[j]-64]); } } } // printf("%d",maxn); for(int i=maxn;i>=1;i--) { for(int j=1;j<=26;j++) { if(a[j]>=i) printf("*"); else printf(" "); if(j!=26) printf(" "); } printf("\n"); } printf("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\n"); return 0; } ```
by wunaidedanjuan @ 2023-08-29 17:49:43


@[wunaidedanjuan](/user/951032) 本人代码最后输出多了一个换行符(才发现 Q A Q)
by wunaidedanjuan @ 2023-08-29 17:50:54


@[wunaidedanjuan](/user/951032) 万分感谢!!!
by QYlucky @ 2023-08-29 20:29:43


|