题解 | #DNA序列#分享一种解题思路

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 dna = in.nextLine();
        int n = in.nextInt();
        in.close();

        double gcMax = 0;
        String rlt = "";
        for(int i = 0; i < dna.length() - n + 1; i++){
            String tmp = dna.substring(i, i + n);
            double tp = gcRatio(dna, tmp);
            if(tp > gcMax){
                gcMax = tp;
                rlt = tmp;
            }
        }

        System.out.println(rlt);
    }

    public static double gcRatio(String s, String tt){
        double gc = 0;
        for(int i = 0; i < tt.length(); i++){
            if(tt.charAt(i) == 'G' || tt.charAt(i) == 'C'){
                gc++;
            }
        }

        gc = gc / s.length();

        return gc;
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-07 14:00
点赞 评论 收藏
分享
06-26 22:20
门头沟学院 Java
码农索隆:让你把简历发给她,她说一些套话,然后让你加一个人,说这个人给你改简历,然后开始卖课
我的求职精神状态
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务