这种试卷根本不会做
。。。。。
老板一共需要给某个员工发奖金n元,可以选择一次发1元,也可以选择一次发2元,也可以选择一次发3元。请问老板给这位员工发放完n元奖金共有多少种不同的方法?
数据范围:1 <= n <= 10*
class Solution {
public:
/**
*
* @param num_money int整型 奖金的总数,单位为元
* @return int整型
*/
int CalulateMethodCount(int num_money) {
// write code here
vector<int>dp(num_money+1);
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];
}
};
撤销/恢复操作具有广泛的用途,比如word文档中输入一个单词,可以点撤销,然后可以再恢复。
编程实现如下功能: 从标准输入读取到一个字符串,字符串可包含0个或多个单词,单词以空格或者tab分隔; 如果遇到 “undo” 字符串,表示"撤销"操作,前一个字符串被撤销掉; 如果遇到"redo"字符串,表示恢复刚才撤销掉的字符串.
例如: 输入字符串 “hello undo redo world.”, 对字符串中的 undo 和 redo 处理后, 最终输出的结果为 “hello world.”
栈没做出来:
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int main(){
string s;
string res = "";
stack<string>str;
stack<string>delStr;
while(cin >> s){
if(s == "undo"){
string temp = str.top();
str.pop();
delStr.push(temp);
}else if(s == "redo"){
string temp = delStr.top();
str.push(temp);
delStr.pop();
}else{
str.push(s);
}
}
stack<string>ans;
while(str.empty()!=true){
string temp = str.top();
str.pop();
ans.push(temp);
}
while(ans.empty() != true){
cout << ans.top() << " ";
ans.pop();
}
return 0;
}
思路比较简单,就是细节没处理好
#include<bits/stdc++.h>
using namespace std;
int main() {
vector<string> undo_queue, words;
string s;
while (cin >> s) {
char ch = getchar();
//遇到undo时,将words的最后一个单词放进undo_queue中,然后弹出这个单词
if (s == "undo") {
if (!words.empty()) {
undo_queue.push_back(words.back());
words.pop_back();
}
}
//遇到redo时,将undo_queue中的最后一个词取出放回到words中,并弹出这个词
else if (s == "redo") {
if (!undo_queue.empty()) {
words.push_back(undo_queue.back());
undo_queue.pop_back();
}
}
//遇到普通词直接放进words中,并且要清空undo_queue,因为插入的新的词以后undo_queue里的数据已经不是最新的了
else {
words.push_back(s);
undo_queue = vector<string>();
}
if (ch == '\n')
break;
}
for (auto word : words) {
cout << word << " ";
}
return 0;
}