统计每个月兔子的总数
思路:
第n个月的兔子数量由两部分组成,一部分是上个月的兔子f(n-1),另一部是满足3个月大的兔子,会生一只兔子f(n-2)。所以第n个月兔子总数: f(n) = f(n - 1) + f(n - 2)。本题是在变相考察斐波那契数列。
代码:
#include <iostream>
using namespace std;
int num_of_rabbit(int n) {
if (n == 1 || n == 2) {
return 1;
} else {
return num_of_rabbit(n - 1) + num_of_rabbit(n - 2);
}
}
int main() {
int n;
while (cin >> n) {
cout << num_of_rabbit(n);
}
return 0;
}
字符串通配符
思路:
本题可以通过递归求解。
从前向后一次匹配,遇到相同字符,都向后移动一个字符;
如果通配符遇到"?"
,则不需匹配,自动跳过一个字符;
如果通配符遇到"*"
,此时可以有三种选择:
- 1,匹配0个,通配符串Mptr向后移动一个字符,字符串ptr不动。
- 2,匹配1个,通配符串Mptr和字符串ptr都向后移动一个字符。
- 3,匹配多个,通配符串Mptr不动,字符串ptr向后移动一个字符。
递归的终止条件:
- 通配符串Mptr和字符串ptr同时到达’\0’(同时结束),返回true
- 两个字符串有一个先结束,则返回false
代码:
#include <iostream>
#include <string>
using namespace std;
//判断合法性(需要字母和数字)
bool islegal(const char* ptr) {
if (isdigit(*ptr) || isalpha(*ptr))
return true;
else
return false;
}
bool Match(const char* Mptr, const char* ptr) {
//通配符串Mptr和字符串ptr同时到达'\0',返回true
if (*Mptr == '\0' && *ptr == '\0') {
return true;
}
//两个字符串有一个先结束,则返回false
if (*Mptr == '\0' || *ptr == '\0') {
return false;
}
if (*Mptr == '?' ) {
if (!islegal(ptr))
return false;
return Match(Mptr + 1, ptr + 1);
}
if (*Mptr == '*' ) {
//减少递归层数,此处不加会运行超时
while (*Mptr == '*') {
Mptr++;
}
Mptr--;
//如果ptr出现不合法的字符,只能按照匹配0个字符来处理
if (!islegal(ptr))
return Match(Mptr + 1, ptr);
//Match(Mptr + 1, ptr)匹配0个字符
//Match(Mptr, ptr + 1)匹配多个字符
//Match(Mptr + 1, ptr + 1)匹配一个字符
return Match(Mptr + 1, ptr) || Match(Mptr, ptr + 1) || Match(Mptr + 1, ptr + 1);
}
//(*Mptr) == (*ptr) || (*Mptr) == ((*ptr) - 32) || (*Mptr) == ((*ptr) + 32)
if (tolower(*Mptr) == tolower(*ptr)) {//忽略大小写
return Match(Mptr + 1, ptr + 1);
}
return false;
}
int main() {
string strMatch = "", str = "";
cin >> strMatch >> str;
if (Match(strMatch.c_str(), str.c_str())) {
cout << "true";
} else {
cout << "false";
}
}
汽水瓶
思路一:
本题思路比较简单,每次空瓶的数量除以3,直到最后空瓶的数量等于1时,就跳出循环即可,而不是num为0才跳出循环,因为空瓶为1的时候就已经不能再换汽水了。
当空瓶为2的时候,向老板借一个空汽水瓶,然后正好还能喝一瓶并且还老板一个空瓶。
代码:
#include<iostream>
using namespace std;
int main() {
int num = 0;
while (cin >> num) {
if (num == 0) {
return 0;
}
int sum = 0;
while (num !=1) { //等于1就跳出循环即可,而不是num为0才跳出循环
//等于1的时候就已经不能再换汽水了
if (num == 2) {
sum++;
break;
}
int tmp = num;
num /= 3;
sum += num;
if (tmp % 3) {//有余数就再把余数加上
num += (tmp % 3);
}
}
cout << sum << endl;
}
return 0;
}
思路二:
取巧,题目描述中有讲到:剩2个空瓶子时,可以先找老板借一瓶汽水,喝掉这瓶满的,喝完以后用3个空瓶子换一瓶满的还给老板。也就是说2个空瓶子即可换一瓶汽水喝,而且喝完之后手里也没有空瓶子。所以求解时直接把空瓶数除以2即可。
代码
int main() {
int n, res;
while (cin >> n) {
if (n == 0)
break;
res = n / 2;
cout << res << endl;
}
return 0;
}
查找两个字符串a,b中的最长公共子串
思路:
本题需要用动态规划求解,MCS[i][j]记录短字符串 s1 前 i 个字符和长字符串 s2 前 j 个字符的最长子串的长度,初始化所有值为 0。
- 当 s1[i-1]= = s2[j-1]时,MCS[i][j] = MCS[i - 1] [j - 1] + 1,这里使用一个额外的值
postion 来记录最长子串在短字符串 s1 中出现的起始位置,
maxsize 记录当前最长子串的长度,
当MCS[i][j] >maxsize时,maxsize= MCS[i][j], 则postion= i - maxsize; - 当s1[i-1] != s2[j-1]时不需要任何操作,最后获取substr(postion, maxsize)即为所求。
代码:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string Max_Sub(string &str_small,string &str_big)
{
if(str_small.size()>str_big.size())
{
swap(str_big,str_small);
}
int size1=str_small.size();
int size2=str_big.size();
vector<vector<int>> MSC (size1+1,vector<int>(size2+1,0));//二维矩阵
int maxsize=0;
int postion=0;
for(int i=1;i<size1+1;i++)
{
for(int j=1;j<size2+1;j++)
{
if(str_small[i-1]==str_big[j-1])
{
MSC[i][j]=MSC[i-1][j-1]+1;//核心代码
if(maxsize<MSC[i][j])
{
maxsize=MSC[i][j];
postion=i-maxsize;//正好i-maxsize就是最长公共子串的开始位置
}
}
}
}
return str_small.substr(postion,maxsize);
}
int main() {
string str1;
string str2;
cin>>str1>>str2;
cout<<Max_Sub(str1,str2);
return 0;
}
end