题解 | #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;
    }
}

全部评论

相关推荐

10-17 09:06
门头沟学院 Java
LZStarV:(这居然能又刷到你)感觉真的就是岗位少了吧,项目很多27届这个点都差不多这个水准,只能说有点不赶趟了先慢慢投吧兄弟,啥都投一下总有机会的
点赞 评论 收藏
分享
刷牛客的我很豁达:你是不是对算法有什么误解,你没手握两篇顶刊顶会,还想搞算法岗,有顶刊顶会在算法岗算才入门
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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