网易编程题——牛牛的闹钟
/*
问题描述:A时B分上课,牛牛到教室要X分钟,所以牛牛最晚什么时候起床。
思考:不用思考,简单题最晚当然是 上课时间-从宿舍到教室花费的时间
*/
#include<algorithm> #include<cmath> #include<iostream> #include<vector> using namespace std; int main() { int num; cin >> num; int hour, minute; vector<int> time_v; for (int i = 0; i < num; i++) { cin >> hour >> minute; time_v.push_back(hour * 60 + minute); } int spend_hour; cin >> spend_hour; int school_h, shool_m; cin >> school_h >> shool_m; sort(time_v.begin(), time_v.end()); int time = school_h * 60 + shool_m - spend_hour; for (int i = 1; i < num; i++) { if (time - time_v[i - 1] >= 0 && time - time_v[i] < 0) { cout << time_v[i - 1] / 60 << " "; cout << time_v[i - 1] % 60; } } system("pause"); return 0; }
方法二:
#include<bits/stdc++.h> using namespace std; int main() { int n; cin >> n; set<int> aSet; for(int i = 0; i < n; ++i) { int h,m; cin >> h >> m; int temp = 60*h + m; aSet.insert(temp); } int x; cin >> x; int h,m; cin >> h >> m; int temp = 60*h + m - x; auto it = upper_bound(aSet.begin(),aSet.end(),temp); it--; temp = *(it); cout << temp/60 << " " << temp%60;