首页 > 试题广场 >

DNA合成

[编程题]DNA合成
  • 热度指数:2958 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
DNA分子是以4种脱氧核苷酸为单位连接而成的长链,这4种脱氧核苷酸分别含有A,T,C,G四种碱基。碱基互补配对原则:A和T是配对的,C和G是配对的。如果两条碱基链长度是相同的并且每个位置的碱基是配对的,那么他们就可以配对合成为DNA的双螺旋结构。现在给出两条碱基链,允许在其中一条上做替换操作:把序列上的某个位置的碱基更换为另外一种碱基。问最少需要多少次让两条碱基链配对成功

输入描述:
输入包括一行: 包括两个字符串,分别表示两条链,两个字符串长度相同且长度均小于等于50。


输出描述:
输出一个整数,即最少需要多少次让两条碱基链配对成功
示例1

输入

ACGT TGCA

输出

0
#include<iostream>
#include<string>
using namespace std;

int main(){
	string a, b;
	cin >> a >> b;
	int len = a.length();
	int count = 0;
	for (int i = 0; i < len; i++)
	if ((a[i] == 'A' && b[i] == 'T') || (a[i] == 'T' && b[i] == 'A') || (a[i] == 'C' && b[i] == 'G') || (a[i] == 'G' && b[i] == 'C'))
		count++;
	cout << len - count;
	return 0;
}

发表于 2017-03-09 18:37:43 回复(1)
one,two = raw_input().split()
lenth = len(one)
iter = 0
fori in range(lenth):
    if''.join(one[i]+two[i]) not in ['AT','TA','CG','GC']:
        iter = iter + 1
print iter

发表于 2017-03-20 16:21:46 回复(2)
/*
	思路:
	使用ASCII码相减判断两两字符是否匹配
	abs('A'-'T') = 19
	abs('C'-'G') = 4; 
*/
#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;
 
int main() {
    string a,b;
    char cha,chb;
    int count = 0;
     
    cin >> a >> b;
    for(int i=0; i<a.length(); i++) {
        cha = a[i];
        chb = b[i];
        if(abs(cha-chb)==19 || abs(cha-chb)==4) continue;
        else {
            count++;
        }
    }
    cout << count << endl;
    return 0;
}

发表于 2017-03-23 22:33:24 回复(0)

import java.util.Scanner;

public class  Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s1 = sc.next();
        String s2 = sc.next();
        if (s1 == null || s2 == null ||
                s1.length() != s2.length() ||
                s1.isEmpty() || s2.isEmpty()) {
            return;
        }
        int count = 0;
        for (int i = 0; i < s1.length(); i ++) {
            if (s1.charAt(i) == 'A' && s2.charAt(i) == 'T') continue;
            if (s1.charAt(i) == 'T' && s2.charAt(i) == 'A') continue;
            if (s1.charAt(i) == 'C' && s2.charAt(i) == 'G') continue;
            if (s1.charAt(i) == 'G' && s2.charAt(i) == 'C') continue;
            count ++;
        }
        System.out.println(count);

    }
}
先判断输入的字符串是否为空,长度是否相等。然后注意比较即可。

发表于 2017-03-12 13:03:41 回复(0)
public class Main {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String str1 = sc.next();
            String str2 = sc.next();
            int a = 'T'+'A';
            int b = 'C'+'G';
            int count=0;
            if(str1.length()==str2.length())
            for(int i=0;i<str1.length();i++){
                int c = str1.charAt(i)+str2.charAt(i);
                if(a!=c && b!=c)count++;
            }
            System.out.print(count);
        }
    }
}

发表于 2017-03-09 15:41:28 回复(2)
include <iostream>
#include <string>
#include <map>
std::map<char,char> dna;
using namespace std;

int main() {
    dna['T']='A';
    dna['A']='T';
    dna['C']='G';
    dna['G']='C';
    char s1[50];
    int i=0,j=0,count=0;
    gets(s1);
    while(s1[j-1]!=' ') j++;
    while(s1[i]!=' '){
        if(dna[s1[i]]!=s1[j]){
            count++;
        }
        i++;
        j++;
    }
    cout<<count;
    return 0;
}
发表于 2020-02-16 11:04:31 回复(0)
对位之和不为0
input_ = input().split(' ')
chain1 = list(input_[0])
chain2 = list(input_[1])
pattern = {'A':2, 'T':-2, 'G':1, 'C':-1}
chain1 = [pattern[x] for x in chain1]
chain2 = [pattern[x] for x in chain2]

result = list(map(lambda x,y: x+y, chain1, chain2))
print(len(result) - result.count(0))

发表于 2020-04-08 15:36:06 回复(0)
import java.util.*;
public class Main3{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        String DNAL = in.next();
        String DNAR = in.next();
        int count1 = 0;
        for(int i=0; i<DNAL.length(); i++){
            if(!match(DNAL.charAt(i), DNAR.charAt(i))){
                count1++;
            }
        }
//        int count2 = 0;
//        for(int i=0; i<DNAL.length();i++){
//            if(!match(DNAL.charAt(i), DNAR.charAt(DNAR.length()-1-i))){
//                count2++;
//            }
//        }
//        if(count1>count2){
//            System.out.println(count2);
//        }
//        else{
            System.out.println(count1);
//        }
    }
    public static boolean match(char A, char B){
        if(A=='A'){
            if(B=='T'){
                return true;
            }
        }
        else if(A=='T'){
            if(B=='A'){
                return true;
            }
        }
        else if(A=='G'){
            if(B=='C'){
                return true;
            }
        }
        else if(A=='C'){
            if(B=='G'){
                return true;
            }
        }
        return false;
    }
}
按道理来说,这个DNA序列可以翻转的吧,,为什么题目没有说这种情况呢?
发表于 2020-04-03 05:11:28 回复(0)
const isPair = (couple) => {
  return(/[AT{2}/.test(couple) || /[GC]{2}/.test(couple))
}

const Mix = (dna1, dna2) => {
  var times = 0;
  for(let i = 0; i < dna1.length; i++){
    if(isPair(dna1[i] + dna2[i])){
      continue;
    }
    ['A', 'G', 'T', 'C'].forEach(e=>{
      if(!isPair(e + dna2[i]))times++
    })
  }
      return times
}

console.log(Mix('ACGT', 'TGCA'))

发表于 2020-03-19 17:48:09 回复(0)
s1 = input()
s2 = input()
c = lambda a, b: sum(map(lambda x: 0 if set(x) in ({'T', 'A'}, {'G', 'C'}) else 1, zip(a, b)))
print(c(s1, s2))

编辑于 2020-02-20 21:13:38 回复(1)
one, two = input().split()

lenth = len(one)

dic={'A':'T', 'T':'A', 'C':'G', 'G':'C'}

iter = 0

for i in range(lenth):
    if dic[one[i]] != two[i]:
        iter = iter + 1
print(iter)
编辑于 2019-09-29 16:14:04 回复(0)
#include <stdlib.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
using namespace std;
int main() {
 char s1[51];
 char s2[51];
 scanf("%s %s", s1, s2);
 int count = 0;
 for (int i = 0; s1[i] != '\0'; ++i) {
  if (s1[i] == 'A' && s2[i] == 'T' ||
   s1[i] == 'T' && s2[i] == 'A' ||
   s1[i] == 'G' && s2[i] == 'C' ||
   s1[i] == 'C' && s2[i] == 'G') {
   continue;
  }
  else {
   ++count;
  }
 }
 printf("%d", count);
 
 return 0;
}

发表于 2019-05-03 10:36:46 回复(0)
list1,list2=input().split()
count=0
S=set(["AT","CG","TA","GC"])
for i in range(len(list1)):
    if str(list1[i])+str(list2[i]) not in S:
        count+=1
print(count)

发表于 2018-08-08 20:35:14 回复(0)

a, b = input().strip().split(' ')
count = 0
for i in range(len(a)):
s = a[i] + b[i]
if s not in ['AT', 'TA', 'CG', 'GC']:
count += 1
print(count)

发表于 2018-04-23 12:54:23 回复(0)
7t_头像 7t_

// Swift 4.0
let line = readLine()!  
let list = line.split(separator: " ")
let a = list[0]
let b = list[1]
var count = 0
for index in a.indices {
    let v = (String(a[index]) + String(b[b.index(index, offsetBy: a.count+1)]))
    if !["AT", "TA", "GC", "CG"].contains(v) {
        count += 1
    }
}
print(count)
发表于 2018-03-07 19:34:51 回复(0)
# 逐项配对比较,若配对不成功操作次数就加一
DNAs = input('输入两个长度相同的DNA链用空格分隔')
base, change = DNAs.split(' ')
total_length = len(base)
base = list(base)
change = list(change)
count = 0 for i in range(total_length):    if base[i]+change[i] not in ['AT', 'CG', 'TA', 'GC']:
        count += 1    
     else: continue print('RESULT',count)

编辑于 2018-02-26 02:57:09 回复(0)
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main{

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String string = scanner.next();
        String string2 = scanner.next();
        int length1 = string.length();
        int length2 = string2.length();
        int count = 0;
        if(length1 != length2 || length1 < 1 || length2 < 1){
            return;
        }
        else {
            for(int i = 0;i < length1;i++){
                if((string.charAt(i) == 'A' && string2.charAt(i) == 'T')
                        || (string.charAt(i) == 'T' && string2.charAt(i) == 'A')
                        || (string.charAt(i) == 'C' && string2.charAt(i) == 'G')
                        || (string.charAt(i) == 'G' && string2.charAt(i) == 'C')){
                    count++;
                }
            }
        }
        System.out.println(length1 - count);
    }

}

发表于 2017-12-17 17:13:28 回复(0)
import java.util.Scanner;

/**
 * @author LiuFei
 * @create 2017-12-08 15:49
 */
public class Main {

    public static void main(String[] args) {

        int count = 0; // 计数器

        // 获取数据
        Scanner scanner = new Scanner(System.in);
        String str1 = scanner.next();
        String str2 = scanner.next();

        for (int i = 0; i < str1.length(); i++) {
            if (getCorrChar(str1.charAt(i)) != str2.charAt(i)) {
                count++;
            }
        }

        System.out.println(count);
    }

    /**
     * 返回与某碱基互补的碱基
     *
     * @param x 某碱基
     * @return 互补碱基
     */
    public static char getCorrChar(char x) {
        char y = ' ';
        switch (x) {
            case 'A': {
                y = 'T';
                break;
            }
            case 'T': {
                y = 'A';
                break;
            }
            case 'C': {
                y = 'G';
                break;
            }
            case 'G': {
                y = 'C';
                break;
            }
        }
        return y;
    }
}

编辑于 2017-12-08 17:10:23 回复(1)

#include <iostream>

#include <string>

using namespace std;

int main(int argc, const char * argv[]) {

    string a,b;

    int i,count=0;

    cin>>a>>b;


    for(i=0;i<a.length();i++){

        if((a[i]=='A' && b[i]=='T')||(a[i]=='T' && b[i]=='A')||(a[i]=='C' && b[i]=='G')||(a[i]=='G' && b[i]=='C'))

            count++;

    }

    cout<<(a.length()-count)<<endl;

    return 0;

}


发表于 2017-11-06 17:56:30 回复(0)
#include <iostream>
using namespace std;

bool matchDNA(char ch1, char ch2) {
    if (ch1 == 'C') {

    }
    bool isMatch1 = ch1 == 'C' && ch2 == 'G';
    bool isMatch2 = ch1 == 'G' && ch2 == 'C';
    bool isMatch3 = ch1 == 'A' && ch2 == 'T';
    bool isMatch4 = ch1 == 'T' && ch2 == 'A';
    if (isMatch1 || isMatch2 || isMatch3 || isMatch4) {
        return true;
    } else {
        return false;
    }
}

int main(int argc, const char * argv[]) {
    char a[50], b[50];
    cin>>a>>b;
    int i = 0;
    int sum = 0;
    while (a[i]) {
        if (!matchDNA(a[i], b[i])) {
            sum++;
        }
        i++;
    }
    cout<<sum;
    return 0;
}
发表于 2017-10-18 12:50:21 回复(0)

热门推荐

通过挑战的用户

DNA合成