题目只说了要严格按照格式写,但是没有说输入的范围(没有说是不是所有26个字母都会出现)
我一开始的思路是有可能有些单词不会出现,那就得考虑底部的单词别表和单词没出现时中间多余的空格
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
string a1, a2;
int a[200];
int main() {
for (int x = 0; x < 4; x++) {
getline(cin, a1);
for (int y = 0; y < a1.size(); y++) {
if (a1[y] >= 'A' && a1[y] <= 'Z') {
a[a1[y]]++;
}
}
}
int yes = 0;
int xx = 0, mmx = 0;
for (int i = 65; i <= 90; i++) {
if (a[i] != 0) {
a2 += char(i);
if (yes == 0) {
xx = i;
yes = 1;
}
if (a[i] > mmx)mmx = a[i];
}
}
for (;mmx;mmx--) {
for (int y = xx; y <= 90; y++) {
if (y != xx&&a[y]!=0)cout << ' ';
if (a[y] == mmx) {
cout << '*';
a[y]--;
}
else if(a[y]!=0)cout << ' ';
}
cout << endl;
}
for (int x = 0; x < a2.size(); x++) {
if (x != 0)cout << ' ';
cout << a2[x];
}
return 0;
}
按照这个思路去写只有40分,看了一下题解感觉就是所有单词都会出现,然后我稍微改了一下,就连因为有可能有些单词会不出现,写的缩进语句都没删,还是过了,也是服了
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
string a1, a2;
int a[200];
int main() {
for (int x = 0; x < 4; x++) {
getline(cin, a1);
for (int y = 0; y < a1.size(); y++) {
if (a1[y] >= 'A' && a1[y] <= 'Z') {
a[a1[y]]++;
}
}
}
int yes = 0;
int xx = 0, mmx = 0;
for (int i = 65; i <= 90; i++) {
if (a[i] != 0) {
a2 += char(i);
if (yes == 0) {
xx = i;
yes = 1;
}
if (a[i] > mmx)mmx = a[i];
}
}
for (;mmx;mmx--) {
for (int y = xx; y <= 90; y++) {
if (y != xx)cout << ' ';
if (a[y] == mmx) {
cout << '*';
a[y]--;
}
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" << endl;
return 0;
}