打印 DNA 序列的程序
给定 n 的值,即波瓣数。打印脱氧核糖核酸 (DNA) 的双螺旋结构。
Input: n = 8
Output:
AT
T--A
A----T
T------A
T------A
G----C
T--A
GC
CG
C--G
A----T
A------T
T------A
A----T
A--T
GC
AT
C--G
T----A
C------G
C------G
T----A
G--C
AT
AT
T--A
A----T
T------A
T------A
G----C
T--A
GC
解释 :
DNA 主要由 4 种碳氢化合物组成,即胞嘧啶 [C]、鸟嘌呤 [G]、腺嘌呤 [A]、胸腺嘧啶 [T]。
DNA碱基相互配对,A与T配对,C与G配对,形成称为碱基对的单位。
下面是打印双螺旋 DNA 序列的实现:
CPP
// CPP Program to print the
// 'n' lobes of DNA pattern
#include
using namespace std;
// Function to print upper half
// of the DNA or the upper lobe
void printUpperHalf(string str)
{
char first, second;
int pos = 0;
// Each half of the DNA is made of
// combination of two compounds
for (int i = 1; i <= 4; i++) {
// Taking the two carbon
// compounds from the string
first = str[pos];
second = str[pos + 1];
pos += 2;
for (int j = 4 - i; j >= 1; j--)
cout << " ";
cout << first;
for (int j = 1; j < i; j++)
cout << "--";
cout << second << endl;
}
}
// Function to print lower half
// of the DNA or the lower lobe
void printLowerHalf(string str)
{
char first, second;
int pos = 0;
for (int i = 1; i <= 4; i++) {
first = str[pos];
second = str[pos + 1];
pos += 2;
for (int j = 1; j < i; j++)
cout << " ";
cout << first;
for (int j = 4 - i; j >= 1; j--)
cout << "--";
cout << second << endl;
}
}
// Function to print 'n' parts of DNA
void printDNA(string str[], int n)
{
for (int i = 0; i < n; i++) {
int x = i % 6;
// Calling for upperhalf
if (x % 2 == 0)
printUpperHalf(str[x]);
else
// Calling for lowerhalf
printLowerHalf(str[x]);
}
}
// Driver function
int main()
{
int n = 8;
// combinations stored in the array
string DNA[] = { "ATTAATTA", "TAGCTAGC", "CGCGATAT",
"TAATATGC", "ATCGTACG", "CGTAGCAT" };
printDNA(DNA, n);
return 0;
}
Java
// Java Program to print the
// 'n' lobes of DNA pattern
import java.io.*;
class GFG {
// Function to print upper half
// of the DNA or the upper lobe
static void printUpperHalf(String str)
{
char first, second;
int pos = 0;
// Each half of the DNA is made of
// combination of two compounds
for (int i = 1; i <= 4; i++) {
// Taking the two carbon
// compounds from the string
first = str.charAt(pos);
second = str.charAt(pos+1);
pos += 2;
for (int j = 4 - i; j >= 1; j--)
System.out.print(" ");
System.out.print(first);
for (int j = 1; j < i; j++)
System.out.print("--");
System.out.println(second);
}
}
// Function to print lower half
// of the DNA or the lower lobe
static void printLowerHalf(String str)
{
char first, second;
int pos = 0;
for (int i = 1; i <= 4; i++) {
first = str.charAt(pos);
second = str.charAt(pos+1);
pos += 2;
for (int j = 1; j < i; j++)
System.out.print(" ");
System.out.print(first);
for (int j = 4 - i; j >= 1; j--)
System.out.print("--");
System.out.println(second);
}
}
// Function to print 'n' parts of DNA
static void printDNA(String str[], int n)
{
for (int i = 0; i < n; i++) {
int x = i % 6;
// Calling for upperhalf
if (x % 2 == 0)
printUpperHalf(str[x]);
else
// Calling for lowerhalf
printLowerHalf(str[x]);
}
}
public static void main (String[] args) {
int n = 8;
// combinations stored in the array
String DNA[] = { "ATTAATTA", "TAGCTAGC", "CGCGATAT",
"TAATATGC", "ATCGTACG", "CGTAGCAT" };
printDNA(DNA, n);
}
}
// This code is contributed by Gitanjali
Python3
# Python Program to print the
# 'n' lobes of DNA pattern
import math
# Function to print upper half
# of the DNA or the upper lobe
def printUpperHalf(str):
first=0
second=0
pos = 0
# Each half of the DNA is made of
# combination of two compounds
for i in range(1,5):
# Taking the two carbon
# compounds from the string
first = str[pos]
second = str[pos+1]
pos += 2
for j in range ( 4 - i, 0,-1):
print(" ",end="")
print(first,end="")
for j in range (1, i):
print("--",end="")
print(second)
# Function to print lower half
# of the DNA or the lower lobe
def printLowerHalf(str):
first=0
second=0
pos = 0
for i in range(1,5):
first = str[pos]
second = str[pos+1]
pos += 2
for j in range(1,i):
print(" ",end="")
print(first,end="")
for j in range (4 - i, 0,-1):
print("--",end="")
print(second)
# Function to print 'n' parts of DNA
def printDNA( str, n):
for i in range(0,n):
x = i % 6
# Calling for upperhalf
if (x % 2 == 0):
printUpperHalf(str[x])
else:
# Calling for lowerhalf
printLowerHalf(str[x])
# driver code
n = 8
# combinations stored in the array
DNA = [ "ATTAATTA", "TAGCTAGC", "CGCGATAT",
"TAATATGC", "ATCGTACG", "CGTAGCAT" ]
printDNA(DNA, n)
# This code is contributed by Gitanjali.
C#
// C# Program to print the 'n' lobes of
// DNA pattern
using System;
class GFG {
// Function to print upper half
// of the DNA or the upper lobe
static void printUpperHalf(string str)
{
char first, second;
int pos = 0;
// Each half of the DNA is made of
// combination of two compounds
for (int i = 1; i <= 4; i++) {
// Taking the two carbon
// compounds from the string
first = str[pos];
second = str[pos+1];
pos += 2;
for (int j = 4 - i; j >= 1; j--)
Console.Write(" ");
Console.Write(first);
for (int j = 1; j < i; j++)
Console.Write("--");
Console.WriteLine(second);
}
}
// Function to print lower half
// of the DNA or the lower lobe
static void printLowerHalf(string str)
{
char first, second;
int pos = 0;
for (int i = 1; i <= 4; i++) {
first = str[pos];
second = str[pos+1];
pos += 2;
for (int j = 1; j < i; j++)
Console.Write(" ");
Console.Write(first);
for (int j = 4 - i; j >= 1; j--)
Console.Write("--");
Console.WriteLine(second);
}
}
// Function to print 'n' parts of DNA
static void printDNA(string []str, int n)
{
for (int i = 0; i < n; i++) {
int x = i % 6;
// Calling for upperhalf
if (x % 2 == 0)
printUpperHalf(str[x]);
else
// Calling for lowerhalf
printLowerHalf(str[x]);
}
}
public static void Main () {
int n = 8;
// combinations stored in the array
string []DNA = { "ATTAATTA", "TAGCTAGC",
"CGCGATAT", "TAATATGC",
"ATCGTACG", "CGTAGCAT" };
printDNA(DNA, n);
}
}
// This code is contributed by vt_m.
PHP
= 1; $j--)
echo " ";
echo $first;
for ($j = 1; $j < $i; $j++)
echo "--";
echo $second."\n";
}
}
// Function to print lower half
// of the DNA or the lower lobe
function printLowerHalf($str)
{
$pos = 0;
for ($i = 1; $i <= 4; $i++)
{
$first = $str[$pos];
$second = $str[$pos + 1];
$pos += 2;
for ($j = 1; $j < $i; $j++)
echo " ";
echo $first;
for ($j = 4 - $i; $j >= 1; $j--)
echo"--";
echo $second."\n";
}
}
// Function to print 'n' parts of DNA
function printDNA($str, $n)
{
for ($i = 0; $i < $n; $i++)
{
$x = $i % 6;
// Calling for upperhalf
if ($x % 2 == 0)
printUpperHalf($str[$x]);
else
// Calling for lowerhalf
printLowerHalf($str[$x]);
}
}
// Driver code
$n = 8;
$DNA = array( "ATTAATTA", "TAGCTAGC",
"CGCGATAT", "TAATATGC",
"ATCGTACG", "CGTAGCAT" );
printDNA($DNA, $n);
// This code is contributed by mits.
?>
Javascript
输出 :
AT
T--A
A----T
T------A
T------A
G----C
T--A
GC
CG
C--G
A----T
A------T
T------A
A----T
A--T
GC
AT
C--G
T----A
C------G
C------G
T----A
G--C
AT
AT
T--A
A----T
T------A
T------A
G----C
T--A
GC