奇安信笔试,第一题和第二题的原本题意
第一题说是只能用1块,两块,3块(我fo了),我后面差一点猜对了(苦笑),ac 0
#include<iostream> #include<vector> using namespace std; int CalulateMethodCount(int num_money); int main() { int num_money; cin >> num_money; cout << CalulateMethodCount(num_money) << endl; return 0; } int CalulateMethodCount(int num_money) { // write code here //dp[i]表示采用1,2,...,n-1发送奖金的发放种类数 //注意:先发1再发2 先发2再发1是两种方法 /*vector<int> dp(num_money + 1, 0); dp[0] = 1; for (int i = 1; i <= num_money; ++i) { for (int money = 1; money <= i; ++money) { dp[i] += dp[i - money]; } } return dp.back(); */ //return pow(2,num_money-1); //原来只能用1、2和n吗? //n只能最后一次用 if (num_money == 1) return 1; if (num_money == 2) return 2; vector<int> dp(num_money + 1, 0); dp[0] = 1; dp[1] = 1; dp[2] = 2; for (int i = 2; i <= num_money; ++i) { dp[i] = dp[i - 1] + dp[i - 2]; } return dp[num_money] + 1; }
第二题我猜测如果redo前面不是undo,则恢复了个“寂寞”,ac1
#include<iostream> #include<vector> #include<string> #include<stack> using namespace std; int main() { string cur; stack<string> del; vector<string> save; while (cin >> cur) { if (cur == "undo") {//撤销 if (save.empty()) continue;//无字符串可以撤销 cur = save.back(); save.pop_back(); del.push(cur); } else if (cur == "redo") {//恢复 if (del.empty()) continue;//无撤销的字符串可以恢复 cur = del.top();//此时del不应该为空 del.pop(); save.push_back(cur); } else { save.push_back(cur); while (not del.empty()) del.pop();//如果前一个不是撤销则恢复“空气” } } for (int i = 0; i < save.size(); ++i) { cout << save[i] << " "; } return 0; }