题解 | #DNA序列#

DNA序列

http://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);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String str = in.next();
            int len = in.nextInt();
            String result = "";
            double max = 0.0;
            for (int i=0;i<= str.length() - len;i++){
                String sub = str.substring(i,i+len);
                double count = getCGRatio(sub);
                if (count > max){
                    max = count;
                    result = sub;
                }
            }
            System.out.println(result);
        }
    }

    static double getCGRatio(String str){
        int i = 0;
        for(Character c : str.toCharArray()){
            if (c =='C' || c == 'G'){
                i++;
            }
        }

        return (i + 0.0 ) / str.length();
    }
}
全部评论

相关推荐

昨天 11:08
门头沟学院 Java
投递京东等公司9个岗位
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 15:58
投个小米提前批试试水,先投一个岗位看看形势,不行就再沉淀一下投第二个岗位,莫辜负
Java抽象带篮子:我嘞个骚刚,已经开始研发6g了吗
投递小米集团等公司7个岗位
点赞 评论 收藏
分享
06-13 10:15
门头沟学院 Java
想去夏威夷的大西瓜在...:我也是27届,但是我现在研一下了啥项目都没有呀咋办,哎,简历不知道咋写
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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