题解 | #名字的漂亮度#C#纯数组
名字的漂亮度
https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3
#include <stdio.h> #include <string.h> int main() { char letter[26] = {'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 letterNum[26] = {0}; int n; char str[10000] = {'\0'}; scanf("%d", &n); while (scanf("%s", str) != EOF) { int len = strlen(str); for (int i = 0; i < 26; i++) { //统计的各个字母出现次数存储在letterNum中 for (int j = 0; j < len; j++) { if (letter[i] == str[j]) { letterNum[i]++; } } } for (int k = 0; k < 26; k++) { //冒泡排序letterNum,后面用于计算漂亮度 for (int m = 0; m < 26 - k - 1; m++) { if (letterNum[m] < letterNum[m + 1]) { int temp = letterNum[m]; letterNum[m] = letterNum[m + 1]; letterNum[m + 1] = temp; } } } int butifulNum = 0; //计算漂亮度,具体为字母最多的乘以26,第二多的乘以25,类推 for (int s = 0; s < 26; s++) { butifulNum += letterNum[s] * (26 - s); } printf("%d\n", butifulNum); //输出 for (int x = 0; x < len; x++) { //重置输入的str str[x] = '\0'; } for (int z = 0; z < 26; z++) { //重置letterNum,否则后面的漂亮度会出错 letterNum[z] = 0; } } return 0; }