感觉就是字符串的字符统计一样

DNA序列

https://www.nowcoder.com/practice/e8480ed7501640709354db1cc4ffd42a


import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String string = in.next();
        int n = in.nextInt();
        // 找出GC比例最高的子串,如果有多个则输出第一个的子串
        char[] charArray = string.toCharArray();
        double maxProportion = 0;
        String str = "";
        for (int i = 0; i < charArray.length - n; i++) {
            String substring = string.substring(i, i + n);
            double proportion = getProportion(substring, n);
            // 只有在占比大于时,才记录字串
            if (proportion > maxProportion) {
                str = substring;
                maxProportion = proportion;
            }
        }

        // 注意:字串长度和字符串长度一致的情况
        if (n == string.length()) {
            System.out.println(string);
            return;
        }
        System.out.println(str);
    }

    // 获取占比
    private static double getProportion(String substring, int n) {
        char[] chars = substring.toCharArray();
        double count = 0;
        for (int j = 0; j < chars.length; j++) {
            if (chars[j] == 'G' || chars[j] == 'C') {
                count++;
            }
        }
        double proportion = count / n;
        return proportion;
    }


}

全部评论

相关推荐

05-01 22:41
中南大学 Java
点赞 评论 收藏
分享
牛客965593684号:假的,字节hr都是不会找你内推的,直接就是同学我们约个面试?他们有权限直接捞你的。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务