奇安信816 C++笔试题

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;
}

恩,还是继续肝吧

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值