目录
046:字符串替换
047:神奇数
048:DNA序列
046:字符串替换
字符串替换_牛客题霸_牛客网 (nowcoder.com)
题目:
题解:
简单模拟题~
class StringFormat {
public:
string formatString(string str, int n, vector<char> arg, int m) {
string ret;
int j=0;
for(int i=0;i<str.size();i++)
{
if((str[i]<='Z' && str[i]>='A') || (str[i]<='z' && str[i]>='a'))
{
ret+=str[i];
}
else
{
ret+=arg[j++];
i++;
}
}
while(j<arg.size())
{
ret+=arg[j++];
}
return ret;
}
};
047:神奇数
神奇数_牛客笔试题_牛客网 (nowcoder.com)
题解:
根据题意模拟就行,注意不要有前导0的情况。
#include <iostream>
#include<string>
#include<cmath>
using namespace std;
bool isPrime(int num) {
if (num < 2) return false;
for (int i = 2; i <= sqrt(num); ++i) {
if (num % i == 0) {
return false;
}
}
return true;
}
bool check(int k) {
string str = to_string(k);
int n = str.size();
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (str[i] == '0' && str[j] == '0') {
continue;
}
if (str[i] == '0') {
int num2 = (str[j] - '0') * 10 + (str[i] - '0');
if (isPrime(num2)) {
return true;
}
}
if (str[j] == '0') {
int num1 = (str[i] - '0') * 10 + (str[j] - '0');
if (isPrime(num1)) {
return true;
}
}
if (str[i] != '0' && str[j] != '0') {
int num1 = (str[i] - '0') * 10 + (str[j] - '0');
int num2 = (str[j] - '0') * 10 + (str[i] - '0');
if (isPrime(num1) || isPrime(num2)) {
return true;
}
}
//}
}
}
return false;
}
int main() {
int a, b = 0;
cin >> a >> b;
int ret = 0;
for (int k = a; k <= b; k++) {
if (check(k)) {
//cout<<k<<endl;
ret++;
}
}
cout << ret << endl;
return 0;
}
048:DNA序列
DNA序列_牛客题霸_牛客网 (nowcoder.com)
题目:
题解:
固定长度的滑动窗口:
1.进窗口
2.判断
3.出窗口
4.更新结果
#include <iostream>
#include <string>
using namespace std;
string s;
int x;
int main() {
cin >> s >> x;
int begin = -1; // 标记结果的起始位置
int maxCount = 0; // 存储之前的窗⼝⾥⾯ C + G 的个数
int count = 0; // 统计窗⼝内 C + G
int left = 0, right = 0, n = s.size();
while (right < n) {
if (s[right] == 'C' || s[right] == 'G') count++;
while (right - left + 1 > x) {
if (s[left] == 'C' || s[left] == 'G') count--;
left++;//出窗口
}
//更新结果
if (right - left + 1 == x) {
if (count > maxCount) {
begin = left;
maxCount = count;
}
}
right++;//进窗口
}
cout << s.substr(begin, x) << endl;
return 0;
}