输入包括一行: 包括两个字符串,分别表示两条链,两个字符串长度相同且长度均小于等于50。
输出一个整数,即最少需要多少次让两条碱基链配对成功
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; }
one,two = raw_input().split()lenth = len(one)iter = 0fori in range(lenth):if''.join(one[i]+two[i]) not in ['AT','TA','CG','GC']:iter = iter + 1print iter
/* 思路: 使用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; }
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); } }
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); } } }
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))
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序列可以翻转的吧,,为什么题目没有说这种情况呢?
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'))
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)
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; }
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)
# 逐项配对比较,若配对不成功操作次数就加一 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)
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); } }
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; } }
#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;
}
#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;
}