题解 | #DNA序列#分享一种解题思路
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 dna = in.nextLine(); int n = in.nextInt(); in.close(); double gcMax = 0; String rlt = ""; for(int i = 0; i < dna.length() - n + 1; i++){ String tmp = dna.substring(i, i + n); double tp = gcRatio(dna, tmp); if(tp > gcMax){ gcMax = tp; rlt = tmp; } } System.out.println(rlt); } public static double gcRatio(String s, String tt){ double gc = 0; for(int i = 0; i < tt.length(); i++){ if(tt.charAt(i) == 'G' || tt.charAt(i) == 'C'){ gc++; } } gc = gc / s.length(); return gc; } }