榮耀機(jī)試20220816
滿分600,三道編程,和華子一樣。
第一題(100%通過):
日期處理題,輸入是xxxx年xx月第xx周的周x,要求轉(zhuǎn)換為xxxx-xx-xx標(biāo)準(zhǔn)格式。從2000-01-01開始,要考慮閏年,要考慮非法輸入(比如2018年01月第08周的周1,這種肯定就是不對的)
// // Created by LJ on 2022/8/16. // #include <iostream> #include <vector> #include <map> #include <stdio.h> using namespace std; vector<int> monthDays{0,31,28,31,30,31,30,31,31,30,31,30,31}; vector<int> weekdayFromStart{5,6,7,1,2,3,4}; int main(){ // 處理輸入 int year, month, week, weekday; cin >> year >> month >> week >> weekday; // 數(shù)據(jù)處理 // year:month:01 到2000-01-01過了多少天 int days = 0; days += (year-2000) * 365; // 閏年要考慮進(jìn)去 for(int y = 2000; y < year; ++y){ if(y % 4 == 0 && y % 100 != 0){ days++; } else if(y % 400 == 0){ days++; } } // 算月的時間 for(int m = 1; m < month; ++m){ days += monthDays[m]; } // 當(dāng)年如果是閏年,并且月份大于2月,需要多加一天 if(month > 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)){ days++; } // 不考慮第幾個周的周幾,總的時間已經(jīng)算好了 ++days; // cout << days << endl; // 打印測試一下 // days / 7 得到余數(shù),表示到y(tǒng)ear:month:01是星期幾 int res = days % 7; // cout << "res: " << res << endl; int dayFromYearMonth01 = weekdayFromStart[res]; // cout << "周" << dayFromYearMonth01 << endl; // 準(zhǔn)備輸出 int firstWeekdaysCnt = 7-dayFromYearMonth01+1; if(week == 1){ // 第一個周,需要判斷指定的日期,是否在year:month:01之前 if(weekday < dayFromYearMonth01){ cout << 0 << endl;} else{ printf("%04d-%02d-%02d", year, month, weekday-dayFromYearMonth01+1);} } else{ int resWeekdaysCnt = (week-2)*7 + firstWeekdaysCnt + weekday; // 計算得到的總?cè)掌?,如果超過了當(dāng)月的總天數(shù),也是錯誤的非法數(shù)據(jù) if(resWeekdaysCnt > monthDays[month]){ // 還要判斷是不是閏年 if(month == 2 && (year % 4 == 0 && year % 100 != 0) || year % 400 == 0){ if(resWeekdaysCnt == 29){ printf("%04d-%02d-%02d", year, month, resWeekdaysCnt); } } else cout << 0 << endl; } else{ printf("%04d-%02d-%02d", year, month, resWeekdaysCnt); } } return 0; } /** * 分析題意: * 將【某年某月第幾個周幾】轉(zhuǎn)換為【公歷日期】 * - 存在非法數(shù)據(jù) * - 每月的第一周和最后一周一般都不完整 * - 還要考慮閏年的影響 * - 起始時間為2000-01-01,時間為周六 */
第二題:買賣物品(100%通過)
簡單的離譜。給了兩個數(shù)組,分別代表商品的***和售賣價,要求最后得到的利潤。直接遍歷。。。
// // Created by LJ on 2022/8/16. // #include <iostream> #include <vector> #include <string> using namespace std; void s2vec(const string &s1, vector<int> &m){ int num = 0; for(char ch : s1){ if(ch == ','){ m.push_back(num); num = 0; } else{ num *= 10; num += ch-'0'; } } m.push_back(num); } int main(){ // 輸入數(shù)據(jù)處理 string s1, s2; cin >> s1 >> s2; int k; cin >> k; vector<int> m, n; s2vec(s1, m); s2vec(s2, n); // 打印測試一下 // for(int i : m){ cout << i << " "; } // cout << endl; // for(int i : n){ cout << i << " "; } // cout << endl; // 解題 for(int i = 0; i < m.size(); ++i){ if(m[i] < n[i]){ k += n[i]-m[i]; } } // 輸出 cout << k << endl; return 0; }
第三題(80%通過):
題意:小明要練習(xí)超級左旋技巧,輸入數(shù)據(jù)為多組一維數(shù)組,對于第i天的小明,如果前面天數(shù)的練習(xí)中,失誤次數(shù)小于,技巧值就要加1,否則就減1。相等的話不變。要求輸出每一組數(shù)據(jù)的最大技能獲得值,以及最后的技能獲得值。(題目有點繞,然后沒時間去想優(yōu)化了。
// // Created by LJ on 2022/8/16. // #include <iostream> #include <vector> #include <string> using namespace std; int main(){ // 處理輸入數(shù)據(jù) int T; cin >> T; vector<vector<int>> trains; for(int i = 0; i < T; ++i){ int n; cin >> n; vector<int> tmp; for(int j = 0; j < n; ++j){ int a; cin >> a; tmp.push_back(a); } trains.push_back(tmp); } // 打印輸出 // for(auto &it : trains){ // for(auto i :it){ // cout << i << " "; // } // cout << endl; // } // 暴力解題 vector<vector<int>> ans; for(auto &it : trains){ int maxValue = 0; int currValue = 0; for(int i = 1; i < it.size(); ++i){ // 應(yīng)該嘗試用空間換時間,才能跑完所有案例 for(int j = 0; j < i; ++j){ if(it[i] > it[j]){ ++currValue; } else if(it[i] < it[j]){ --currValue; } } maxValue = max(maxValue, currValue); } ans.push_back({maxValue, currValue}); } // 輸出 for(auto &it : ans){ for(int i : it){ cout << i << " "; } cout << endl; } return 0; }#榮耀筆試##榮耀#