牛客网-2017年校招全国统一模拟笔试(第五场)编程题集合

第一题:

牛牛喜欢彩色的东西,尤其是彩色的瓷砖。牛牛的房间内铺有L块正方形瓷砖。每块砖的颜色有四种可能:红、绿、蓝、黄。给定一个字符串S, 如果S的第i个字符是'R', 'G', 'B'或'Y',那么第i块瓷砖的颜色就分别是红、绿、蓝或者黄。
牛牛决定换掉一些瓷砖的颜色,使得相邻两块瓷砖的颜色均不相同。请帮牛牛计算他最少需要换掉的瓷砖数量。 

输入描述:
输入包括一行,一个字符串S,字符串长度length(1 ≤ length ≤ 10),字符串中每个字符串都是'R', 'G', 'B'或者'Y'。
输出描述:
输出一个整数,表示牛牛最少需要换掉的瓷砖数量
输入例子1:
RRRRRR
输出例子1:
3
#include<iostream>
#include<string.h>
using namespace std;

int main(){
    string str;
    while(cin>>str){
        int ret = 0;
        int n = str.size();
        int last = str[0];
        for(int i = 1 ; i < n ; ++i){
            if(str[i] == last){
                if(str[i] != 'R' && (i+1 < n && str[i+1] != 'R'))
                    str[i] = 'R';
                else if(str[i] != 'G' && (i+1 < n && str[i+1] != 'G'))
                    str[i] = 'G';
                else if(str[i] != 'B' && (i+1 < n && str[i+1] != 'B'))
                    str[i] = 'B';
                else
                    str[i] = 'Y';
                ++ret;
            }
            last = str[i];
        }
        cout<<ret;
    }
}

 第二题:

牛牛从生物科研工作者那里获得一段字符串数据s,牛牛需要帮助科研工作者从中找出最长的DNA序列。DNA序列指的是序列中只包括'A','T','C','G'。牛牛觉得这个问题太简单了,就把问题交给你来解决。
例如: s = "ABCBOATER"中包含最长的DNA片段是"AT",所以最长的长度是2。 

输入描述:
输入包括一个字符串s,字符串长度length(1 ≤ length ≤ 50),字符串中只包括大写字母('A'~'Z')。
输出描述:
输出一个整数,表示最长的DNA片段
输入例子1:
ABCBOATER
输出例子1:
2
#include<iostream>
#include<string.h>
using namespace std;

int main(){
    string str;
    while(cin>>str){
        int l = 0 , ret = 0;
        for(int i = 0 ; i < str.size() ; ++i){
            l = i;
            while(str[i] == 'A' || str[i] == 'T' || str[i] == 'C' || str[i] == 'G')
                ++i;
            if(i - l > ret)
                ret = i - l;
        }
        cout << ret;
    }
}

  






原文地址:https://www.cnblogs.com/jhmu0613/p/7251176.html