题解 | #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);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
String str = in.nextLine();
int len = in.nextInt();
char[] c = new char[str.length()];
for (int i = 0; i < str.length(); i++) {
c[i] = str.charAt(i);
}
int index = -1;
double max = -1;
for (int i = 0; i <= str.length() - len; i++) {
int count = 0;
for (int j = 0; j < len; j++) {
if (c[i + j] == 'C' || c[i + j] == 'G') {
count ++;
}
}
double rare = (double) count / str.length();
if (i >= 0 && rare > max) {
index = i;
max = rare;
}
}
if (index != -1) {
System.out.println(str.substring(index, index + len));
}
System.out.println("");
}
}
}

查看23道真题和解析
