题解 | #名字的漂亮度#

名字的漂亮度

http://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3

package com.hikvision.niuke;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @Classname BeautifulDegree
 * @Description 漂亮值
 * @Date 2022/2/13 18:23
 * @Created by daihuhu
 */
public class BeautifulDegree {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int num = scanner.nextInt();
            for (int i = 0; i < num; i++) {
                String name = scanner.next().toLowerCase();
                getBeautifulDegree(name);
            }
        }
    }

    private static void getBeautifulDegree(String name) {
        int[] array = new int[26];
        char[] chars = name.toCharArray();
        //统计每个字母出现的次数
        for (char ch : chars) {
            array[ch - 97] += 1;
        }
        int beautifulDegree = 0;
        //为了使漂亮值达到最大,对数组进行排序,次数出现越多的字符,使其漂亮值越大,进而整个名字的漂亮值越大
        Arrays.sort(array);
        for (int i = 0; i < array.length; i++) {
            beautifulDegree += array[i] * (i + 1);
        }
        System.out.println(beautifulDegree);
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
北斗导航Compass低仿版:学历一般 没实习 非科班,肯定很难过初筛了,先找个中小厂好好干吧,拿这段实习去投大厂实习
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务