欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

DNA序列

程序员文章站 2022-06-08 12:21:36
...

题目来源

DNA序列

题目描述

一个DNA序列由A/C/G/T四个字母的排列组合组成。G和C的比例(定义为GC-Ratio)是序列中G和C两个字母的总的出现次数除以总的字母数目(也就是序列长度)。在基因工程中,这个比例非常重要。因为高的GC-Ratio可能是基因的起始点。

给定一个很长的DNA序列,以及要求的最小子序列长度,研究人员经常会需要在其中找出GC-Ratio最高的子序列。
DNA序列

解题思路

本题相当于遍历字符串,以每个下标为开始,进行提取子串。
然后相当于找最大值一样,依次打擂台。找到最大值的GC比例

题目解答

import java.util.*;
public class Main{
    public static double giveRatio(String str){
         double ratio=0;
         double count=0;
        for(char ch:str.toCharArray()){
            if(ch=='G'||ch=='C'){
                count++;
            }
        }
        ratio=count/(double)str.length();
        return ratio;
    }
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        String s=sc.nextLine();
        int usedSize=sc.nextInt();
        String maxStr="";
        double maxRatio=0;
        for(int i=0;i<s.length()-usedSize+1;i++){
            String str=s.substring(i,i+usedSize);
            if(giveRatio(str)>maxRatio){
                maxStr=str;
                maxRatio=giveRatio(str);
            }
        }
        System.out.println(maxStr);
    }
}
相关标签: 练习题目