โปรแกรมแสดงรูปแบบของดีเอ็นเอ
โจทย์
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:
Apair withTTpair withACpair withGGpair withC
Noted that the length of string: 0 < n <= 50.
Input:
AACGGTAAGAGGCTCOutput:
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;
}
}
}
}
}คำอธิบาย
ปรับปรุงล่าสุด