荣耀机试20220816
满分600,三道编程,和华子一样。
第一题(100%通过):
日期处理题,输入是xxxx年xx月第xx周的周x,要求转换为xxxx-xx-xx标准格式。从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;
// 数据处理
// year:month:01 到2000-01-01过了多少天
int days = 0;
days += (year-2000) * 365;
// 闰年要考虑进去
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];
}
// 当年如果是闰年,并且月份大于2月,需要多加一天
if(month > 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)){ days++; }
// 不考虑第几个周的周几,总的时间已经算好了
++days;
// cout << days << endl; // 打印测试一下
// days / 7 得到余数,表示到year:month:01是星期几
int res = days % 7;
// cout << "res: " << res << endl;
int dayFromYearMonth01 = weekdayFromStart[res];
// cout << "周" << dayFromYearMonth01 << endl;
// 准备输出
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;
// 计算得到的总日期,如果超过了当月的总天数,也是错误的非法数据
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;
}
/**
* 分析题意:
* 将【某年某月第几个周几】转换为【公历日期】
* - 存在非法数据
* - 每月的第一周和最后一周一般都不完整
* - 还要考虑闰年的影响
* - 起始时间为2000-01-01,时间为周六
*/ 第二题:买卖物品(100%通过)
简单的离谱。给了两个数组,分别代表商品的***和售卖价,要求最后得到的利润。直接遍历。。。
//
// 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(){
// 输入数据处理
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%通过):
题意:小明要练习超级左旋技巧,输入数据为多组一维数组,对于第i天的小明,如果前面天数的练习中,失误次数小于,技巧值就要加1,否则就减1。相等的话不变。要求输出每一组数据的最大技能获得值,以及最后的技能获得值。(题目有点绕,然后没时间去想优化了。
//
// Created by LJ on 2022/8/16.
//
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(){
// 处理输入数据
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){
// 应该尝试用空间换时间,才能跑完所有案例
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;
} #荣耀笔试##荣耀#
查看10道真题和解析