测试点失败原因:
1. 关于小时:如果小时是个位,也需要补0
2. 关于如何判定小时:这样写可以直接对内容进行限制
//重点:可以使用大于小于‘A’这样的方式对比对的字母进行内容上的限制
if (str1[i] == str2[i] && (('A' <= str1[i] && str1[i] <= 'N') ||('0' <= str1[i] && str1[i] <= '9')) && count == 1) {
hour = str1[i];
break;
}
测试通过代码:
#include "iostream"
#include "vector"
#include "cmath"
#include "sstream"
#include "algorithm"
#include "cctype"
std::string getDayAndHour(std::string str1, std::string str2) {
char day;
char hour;
std::string dayInfo;
std::string hourInfo;
// 第一对相同的大写英文字母
int length = str1.length() < str2.length() ? str1.length() : str2.length();
int count = 0;// count = 1就说明收集到了day的信息,接着就应该收集hour的信息
for (int i = 0; i < length; ++i) {
if (str1[i] == str2[i] && 'A' <= str1[i] && str1[i] <= 'G' && count == 0) {
day = str1[i];
count++;
continue;
}
if (str1[i] == str2[i] && (('A' <= str1[i] && str1[i] <= 'N') ||('0' <= str1[i] && str1[i] <= '9')) && count == 1) {
hour = str1[i];
break;
}
}
if (day == 'A') {
dayInfo = "MON";
} else if (day == 'B') {
dayInfo = "TUE";
} else if (day == 'C') {
dayInfo = "WED";
} else if (day == 'D') {
dayInfo = "THU";
} else if (day == 'E') {
dayInfo = "FRI";
} else if (day == 'F') {
dayInfo = "SAT";
} else if (day == 'G') {
dayInfo = "SUN";
}
std::vector<char> hourCode1{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
std::vector<char> hourCode2{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'G', 'K', 'L', 'M', 'N'};
auto code1iterator = std::find(hourCode1.begin(), hourCode1.end(), hour);
if (code1iterator != hourCode1.end()) {
hourInfo = std::to_string(std::distance(hourCode1.begin(), code1iterator));
}
auto code2iterator = std::find(hourCode2.begin(), hourCode2.end(), hour);
if (code2iterator != hourCode2.end()) {
hourInfo = std::to_string(10 + std::distance(hourCode2.begin(), code2iterator));
}
if (hourInfo.size() == 1) {
hourInfo = "0" + hourInfo;
}
return dayInfo + " " + hourInfo;
}
bool isEnglish(char c) {
return isupper(c) || islower(c);
}
std::string getMinute(std::string str1, std::string str2) {
std::string MinuteInfo = "";
// 出现第一对相同英文字母的位置
int length = str1.length() < str2.length() ? str1.length() : str2.length();
for (int i = 0; i < length; ++i) {
if (str1[i] == str2[i] && isEnglish(str1[i])) {
MinuteInfo = std::to_string(i);
break;
}
}
if (MinuteInfo.size() == 1) {
MinuteInfo = "0" + MinuteInfo;
}
return MinuteInfo;
}
int main() {
std::string input1;
std::string input2;
std::string input3;
std::string input4;
std::getline(std::cin, input1);
std::getline(std::cin, input2);
std::getline(std::cin, input3);
std::getline(std::cin, input4);
std::string dayAndHourInfo = getDayAndHour(input1, input2);
std::string minuteInfo = getMinute(input3, input4);
std::cout << dayAndHourInfo << ":" << minuteInfo;
return 0;
}