题解 | #DNA序列#

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);
        String str = in.next();
        int length = in.nextInt();
        Map<String,Double> map = new LinkedHashMap<>();//LinkedHashMap保证顺序
        for(int i =0;i<=str.length()-length;i++){
            //将分割出的子串以及子串的CG比例存入map
            String str1 = str.substring(i,i+length);
            int count = 0;
            for(int j =0;j< str1.length();j++){
                if(str1.charAt(j)=='C'||str1.charAt(j)=='G'){
                    count++;
                }
            }
            map.put(str1,(double)count/length);
        }
        //找出子串中比例的最大值
        double max = Double.MIN_VALUE;
        for(double num :map.values()){
            max = Math.max(max,num);
        }
        //找出第一个比例最大的子串
        for(String str2:map.keySet()){
            if(map.get(str2)==max){
                System.out.print(str2);
                break;
            }
        }
    }
}

全部评论

相关推荐

饼子吃到撑:学院本是这样的,找工作拼运气,你技术再好人家筛选学历照样沉入海底,海投就行了
点赞 评论 收藏
分享
04-17 23:48
西北大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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