Skip to Content
CoursesCSC122โปรแกรมแสดงรูปแบบของดีเอ็นเอ

โปรแกรมแสดงรูปแบบของดีเอ็นเอ

โจทย์

Write a program to print Double DNA strains from a given single strain as example output. The other strain is generated by providing a pair of DNA using the following rules:

  • A pair with T
  • T pair with A
  • C pair with G
  • G pair with C

Noted that the length of string: 0 < n <= 50.

Input:

AACGGTAAGAGGCTC

Output:

A - - T A - - T C - - G G - - C G - - C T - - A A - - T A - - T G - - C A - - T G - - C G - - C C - - G T - - A C - - G

โค้ด

import java.util.Map; import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String dna = scanner.nextLine().toUpperCase(); int space = 0; boolean forward = true; Map<Character, String> nucleotidePairs = Map.of( 'A', "T", 'G', "C", 'C', "G", 'T', "A" ); for (char nucleotide : dna.toCharArray()) { String paired = nucleotidePairs.getOrDefault(nucleotide, ""); String spaces = " ".repeat(Math.max(0, space)); System.out.printf("%s%c - - %s\n", spaces, nucleotide, paired); if (forward) { space++; if (space > 2) { forward = false; } } else { space--; if (space < 0) { forward = true; space = 0; } } } } }

คำอธิบาย

ปรับปรุงล่าสุด