1.老板发奖金
真的特别坑,我一直以为是变态跳台阶,写了很多次很多次,就是没A。看牛客贴的代码才知道这样写,为什么我也不明白……
int CalulateMethodCount(int num_money) {
if (num_money == 1) return 1;
if (num_money == 2) return 2;
if (num_money == 3) return 4;
vector<int> dp(num_money + 1, 0);
dp[0] = 0; dp[1] = 1; dp[2] = 2; dp[3] = 4;
for (int i = 4; i <= num_money; ++i) {
dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3];
}
return dp[num_money];
}
2.实现字符串的undo和redo
用到俩栈,vector也可以。思路比较简单,可惜我基本功不扎实,不会字符串分割。
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
vector<string> t;
vector<string> res;
while(getline(cin, s)){
//分割字符串
int i=0;
while(i<s.size()){
string ss = "";
while(i<s.size() && s[i]!=' '){
ss.push_back(s[i]);
i++;
}
i++;
t.push_back(ss);
}
vector<string> st;
for(int i=0; i<t.size(); i++){
if(t[i]=="undo"){
if(!res.empty()){
st.push_back(res.back());
res.pop_back();
}
}
else if(t[i]=="redo"){
if(!st.empty()){
res.push_back(st.back());
st.pop_back();
}
}else{
st.clear();
res.push_back(t[i]);
}
}
for(auto c:res){
cout<<c<<" ";
}
system("pause");
}
}
补:用空格分割字符串
1.用stringstream
int main(){
vector<string> res;
string word;
getline(cin,word);
string result;
//注意哦,stringstream
stringstream input(word);
while(input>>result){
res.push_back(result);
}
for(int i=0;i<res.size();i++){
cout<<res[i]<<endl;
}
system("pause");
return 0;
}
恩,还是继续肝吧