题解 | #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;
}
}
}
}
查看7道真题和解析