题解 | #DNA序列#

DNA序列

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

import javax.script.*;
import java.util.*;
/*

* */
public class Main{
    public static void main(String[] args){
        Scanner sc = new  Scanner(System.in);
        String str = sc.nextLine();
        int n = sc.nextInt();
        double gcRatio = 0.0;
        String s = "";
        double max = 0.0;
        for (int i = 0; i < str.length(); i++) {
            int end = n + i;   //substring的尾端
            if (end > str.length()){
                break;
            }
            //按照输入n的范围拆分字符串
            String substring = str.substring(i, end);
            //把除了C和G以外字母都去掉
            String replaceAll = substring.replaceAll("[^CG]", "");
            gcRatio = replaceAll.length() * 1.0 / str.length();
//            double max = 0.0;
            if (gcRatio > max){
                s = substring;
                max = gcRatio;
            }
        }
        System.out.println(s);
    }
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务