a581簡單字典查詢與兩個期限邊界
CPE:Do Your Own Homework! (UVa 11917)
先查專長,再按準時、寬限、無法完成三段判斷。
解題思路
把科目名稱對應到所需天數。查詢科目若存在,將天數t與期限D比較:t≤D為Yesss;D<t≤D+5為Late;t>D+5才是Do your own homework!。這三個區間互斥且涵蓋所有可能。
若字典沒有該科目,無論D多大都不能請她完成,直接用最後一種回覆。查不到時可使用無限大預設值,統一落入超時分支。每筆重建專長表,編號從1起,輸出Case k:結果,注意Yesss的三個s與最後訊息的驚嘆號。
Step-by-step animation
示意:math需9天,期限D=4
步驟 1 / 3
讀取專長
表中math對應9天。
math→9
- 所需t
- 9
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <array>
#include <tuple>
#include <functional>
#include <limits>
#include <cstdint>
#include <cctype>
#include <utility>
using namespace std;
int main() {
int t;
cin >> t;
for (int tc = 1; tc <= t; ++tc) {
int n;
cin >> n;
map<string, int> days;
while (n--) {
string s;
int d;
cin >> s >> d;
days[s] = d;
}
int limit;
string subject;
cin >> limit >> subject;
auto it = days.find(subject);
int need = it == days.end() ? 1000000 : it->second;
string answer = need <= limit ? "Yesss"
: need <= limit + 5 ? "Late"
: "Do your own homework!";
cout << "Case " << tc << ": " << answer << '\n';
}
}
先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。