题解 | #DNA序列#
DNA序列
https://www.nowcoder.com/practice/e8480ed7501640709354db1cc4ffd42a
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner fzhinput = new Scanner(System.in); String zfc = fzhinput.nextLine(); int cd = fzhinput.nextInt(); int num = 0; float max = 0; float zf[] = new float[zfc.length() - cd+1]; StringBuilder stack = new StringBuilder(); for (int i = 0; i <= zfc.length() - cd; i++) { num = 0; for (int j = i; j < i + cd; j++) { if (zfc.charAt(j) == 'C' || zfc.charAt(j) == 'G') { num++; } } zf[i] = (float)num / (float)cd; max = Math.max(max, zf[i]); } for (int i = 0; i <= zfc.length() - cd; i++) { if ( zf[i] == max) { for (int j = i; j < i + cd; j++) { stack.append(zfc.charAt(j)); } break; } } System.out.println(stack.toString()); } }