感觉就是字符串的字符统计一样
DNA序列
https://www.nowcoder.com/practice/e8480ed7501640709354db1cc4ffd42a
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String string = in.next(); int n = in.nextInt(); // 找出GC比例最高的子串,如果有多个则输出第一个的子串 char[] charArray = string.toCharArray(); double maxProportion = 0; String str = ""; for (int i = 0; i < charArray.length - n; i++) { String substring = string.substring(i, i + n); double proportion = getProportion(substring, n); // 只有在占比大于时,才记录字串 if (proportion > maxProportion) { str = substring; maxProportion = proportion; } } // 注意:字串长度和字符串长度一致的情况 if (n == string.length()) { System.out.println(string); return; } System.out.println(str); } // 获取占比 private static double getProportion(String substring, int n) { char[] chars = substring.toCharArray(); double count = 0; for (int j = 0; j < chars.length; j++) { if (chars[j] == 'G' || chars[j] == 'C') { count++; } } double proportion = count / n; return proportion; } }