题解 | #单词识别#
单词识别
https://www.nowcoder.com/practice/16f59b169d904f8898d70d81d4a140a0
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char ch[1000][100];
int n; //单词个数
for (int i = 0; i < 1000; i++) {
cin >> ch[i];
for (int j = 0; j < strlen(ch[i]); j++) { //大写字母转小写
ch[i][j] = tolower(ch[i][j]);
}
if (ch[i][strlen(ch[i]) - 1] == '.') { //字符串以“.”结尾
ch[i][strlen(ch[i]) - 1] = '\0'; //将末尾的“.”截断
n = i + 1;
break;
}
}
//冒泡排序法将字符串排序
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (strcmp(ch[i], ch[j]) < 0) {
char temp[100];
strcpy(temp, ch[i]);
strcpy(ch[i], ch[j]);
strcpy(ch[j], temp);
}
}
}
for (int i = 0, count = 1; i < n; i++) {
if (!strcmp(ch[i], ch[i + 1])) {
count++;
} else {
cout << ch[i] << ":" << count << endl;
count = 1;
}
}
}
查看21道真题和解析