题解 | #DNA序列#

DNA序列

https://www.nowcoder.com/practice/e8480ed7501640709354db1cc4ffd42a

#include <iostream>
#include <string>
#include <algorithm>
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")

全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 15:08
点赞 评论 收藏
分享
陆续:不可思议 竟然没那就话 那就我来吧 :你是我在牛客见到的最美的女孩
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务