题解 | #DNA序列#
DNA序列
https://www.nowcoder.com/practice/e8480ed7501640709354db1cc4ffd42a
#include <iostream> #include <string> using namespace std; int main() { string strs; cin >> strs; //DNA序列 int n; cin >> n; //要截取的长度 string findStrs; //找到的字符串 double maxRatio = 0.0; //CG占比 int num = 0; //CG出现的数量 for (int i = 0; i + n <= strs.size(); i++) { string newStrs = strs.substr(i, n); //当前截取的字符串 num = 0; for (int j = 0; j < newStrs.size(); j++) { if (newStrs[j] == 'C' || newStrs[j] == 'G') { num += 1; } } double ratio = double(num) / newStrs.size(); if (ratio > maxRatio) { maxRatio = ratio; findStrs = newStrs; } } cout << findStrs << endl; } // 64 位输出请用 printf("%lld")