名字的漂亮度

名字的漂亮度

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

import java.util.*;

public class Main { 

    private final int N = 26;

    public Main() {
    }

    public int count(String name) {
        int[] arr = new int[N];
        for (int i = 0; i < name.length(); i++) {
            char ch = name.charAt(i);
            arr[ch - 'a']++;
        }
        Arrays.sort(arr);
        int result = 0;
        for (int weight = N, i = N - 1; i >= 0; i--) {
            if (arr[i] != 0) {
                result += weight * arr[i];
                weight--;
            }
        }
        return result;
    }

    public static void main(String[] args) 
    {
        Main solution = new Main();
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            int n = Integer.parseInt(in.nextLine());
            for (int i = 0; i < n; i++) {
                String name = in.nextLine().toLowerCase();
                int result = solution.count(name);
                System.out.println(result);
            }
        }
    } 
}   
全部评论
这里我觉得标记名字字母的数组排序不对,因为每个不同字母的权重算漂亮值不一样. 不知道对不对
点赞 回复 分享
发布于 2021-02-03 19:38
import java.util.*; public class Main { private final int N = 26; public Main() { } public int count(String name) { int[] arr = new int[N]; for (int i = 0; i < name.length(); i++) { char ch = name.charAt(i); arr[ch - 'a']++; } Arrays.sort(arr); int result = 0; for (int weight = N, i = N - 1; i >= 0; i--) { if (arr[i] != 0) { result += weight * arr[i]; weight--; } } return result; } public static void main(String[] args) { Main solution = new Main(); Scanner in = new Scanner(System.in); String str = in.nextLine(); int n = Integer.parseInt(str); while (in.hasNextLine()) { for (int i = 0; i < n; i++) { String name = in.nextLine().toLowerCase(); int result = solution.count(name); System.out.println(result); } } } } 这是修改后的代码,已经运行通过
点赞 回复 分享
发布于 2021-09-02 10:12

相关推荐

废铁汽车人:秋招真是牛鬼蛇神齐聚一堂
点赞 评论 收藏
分享
11-08 17:36
诺瓦科技_HR
点赞 评论 收藏
分享
7 2 评论
分享
牛客网
牛客企业服务