rambless

DNA序列

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

import java.util.*;

// 注意类名必须为 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.nextLine();
            int n = in.nextInt();
            String temp;
            int max = 0;
            Dna dna;
            List<Dna> list = new ArrayList<>(str.length()-n+1);
            //temp中找序列数最高
            for(int i=0; i<=str.length()-n; i++) {
                temp = str.substring(i, i+n);
                dna = judge(temp);
                if(dna.len>max) {
                    max = dna.len;
                }
                list.add(dna);
            }
            for(Dna d : list) {
                if(d.len==max) {
                    System.out.println(d.str);
                    break;
                }
            }
        }
    }

    private static Dna judge(String str) {
        int len = 0;
        if(str.contains("G") && str.contains("C")) {
            char[] arr = str.toCharArray();
            for(int i=0; i<arr.length; i++) {
                if(arr[i]=='C' || arr[i]=='G') {
                    len++;
                }
            }
        }   
        return new Dna(str, len);
    }
    static class Dna {
        String str;
        int len;
        public Dna(String str, int len){
            this.str = str;
            this.len = len;
        }
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-09 13:05
TMD找工作本来就烦,这东西什么素质啊😡
Beeee0927:hr是超雄了,不过也是有道理的
点赞 评论 收藏
分享
MinJerous:虽然我一直说 计算机不怎么卡学历 但是至少得一本
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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