Description: the difficulty of this topic is not very high, because the topic has described the ideas almost, but there are some details to pay attention to
Title:
Solution 1: using char array
#include<iostream> #include<string> using namespace std; int main() { string s1, s2, s3, s4; cin >> s1 >> s2 >> s3 >> s4; int count = 0; char a = 0, b = 0; int c = 0; int flag = 0; for (int i = 0; i<s1.length()&&i<s2.length(); i++) { if (!flag && s1[i] == s2[i] && s1[i] >= 'A' && s1[i] <= 'G') { a = s1[i]; flag = 1; continue; } if (flag && s1[i] == s2[i] && ((s1[i] >= '0' && s1[i] <= '9') || (s1[i] >= 'A' && s1[i] <= 'N'))) { b = s1[i]; break; } } for (int i = 0; i < s3.length() && i < s4.length(); i++) { if (s3[i] == s4[i] && ((s3[i] >= 'a' && s3[i] <= 'z') || (s3[i] >= 'A' && s3[i] <= 'Z'))) { c = i; break; } } int x = a - 'A' + 1; int y = 0; // 9 a b c d e if (b > '9') y = 9 + (b - 'A') + 1; else y = b - '0'; char arr[8][4] = { " ","MON","TUE","WED","THU","FRI","SAT","SUN" }; printf("%s %02d:", arr[x], y); //cout << arr[x] << " " << y << ":"; printf("%02d", c); //if (c < 10) // cout << 0 << c; //else // cout << c; return 0; }
Solution 2: use string class object
#include<iostream> #include<string> using namespace std; int main() { string s1, s2, s3, s4; cin >> s1 >> s2 >> s3 >> s4; int count = 0; char a = 0, b = 0; int c = 0; int flag = 0; for (int i = 0; i<s1.length()&&i<s2.length(); i++) { if (!flag && s1[i] == s2[i] && s1[i] >= 'A' && s1[i] <= 'G') { a = s1[i]; flag = 1; continue; } if (flag && s1[i] == s2[i] && ((s1[i] >= '0' && s1[i] <= '9') || (s1[i] >= 'A' && s1[i] <= 'N'))) { b = s1[i]; break; } } for (int i = 0; i < s3.length() && i < s4.length(); i++) { if (s3[i] == s4[i] && ((s3[i] >= 'a' && s3[i] <= 'z') || (s3[i] >= 'A' && s3[i] <= 'Z'))) { c = i; break; } } int x = a - 'A' + 1; int y = 0; // 9 a b c d e if (b > '9') y = 9 + (b - 'A') + 1; else y = b - '0'; char arr[8][4] = { " ","MON","TUE","WED","THU","FRI","SAT","SUN" }; printf("%s %02d:", arr[x], y); //cout << arr[x] << " " << y << ":"; printf("%02d", c); //if (c < 10) // cout << 0 << c; //else // cout << c; return 0; }
analysis:
I
Char array and string are not very different. The algorithms are the same, but the data storage methods are different. From this, we can see the similarities between char array and string object: String object can be treated as char array
II
Points for attention:
1. The storage range of the first character , is A to G , because these are seven letters corresponding to seven weeks
2. The second character is stored as 0-9 and A-N
3. The purpose of introducing flag variable is not to perform the second operation when the first character is not stored, and not to perform the first operation when the second character is stored
4. After the end of the second storage , break out , otherwise there may be qualified characters stored behind , affecting the first qualified character
III
Printing of data
1. It's very convenient to introduce a two-dimensional character array to print weeks. Of course, you can also use the switch statement. In addition, 'A'-'A'=0 'is not 1. Here, I add an extra space to the array and a corresponding 1 to the letter. You can also add none
2. printf is much more convenient than cout when printing, and note that 0 should be used to fill in less than 2 digits when outputting hours and minutes. In fact, it is not reflected in this hour question, but we can test and draw a conclusion by ourselves (the notes in the article are how to fill in 0 when outputting cout, for reference)
(I want to write a detailed description of common functions. Let's see if there is a chance to send it later)