题解 | #DNA序列#
DNA序列
https://www.nowcoder.com/practice/e8480ed7501640709354db1cc4ffd42a
#include <iostream> #include <string> using namespace std; float compute_GC_Ratio(string& str) { int n = str.size(); float num = 0; for (int i = 0; i < n; i++) { if (str[i] == 'G' || str[i] == 'C') { num++; } } return num / n; } int main() { string geninp; int N; cin >> geninp >> N; int n = geninp.size(); float maxgen = 0; string temp; string maxsubstr; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (j - i + 1 == N) { temp = geninp.substr(i, j - i + 1); float GC = compute_GC_Ratio(temp); if (maxgen < GC) { maxsubstr = temp; maxgen = GC; } break; } } } cout << maxsubstr; } // 64 位输出请用 printf("%lld")
模拟
#华为OD机试真题#