1014 福尔摩斯的约会 (C和C++版本)
这道题主要是细节问题:
前两行输入:
- 第二次查询是在第一次查询基础上进行
- 时间,未满10前面要有0,例如:星期一 05:06
- 第一,二次查询的相同,都是在同一位置上相同
- 第一次查询是MON~SUN,对应字母A~G,只能规定'A' ~'G'(测试点4)
- 第二次查询A~N就只能A~N,用isupper()还得加条件(这个是测试点2)
后两行输入
- 输出数据是下标
- 未满10记得补0
C++:
#include <iostream> #include <vector> #include <string> using namespace std; size_t func(string &s1, string &s2) { return s1.size() < s2.size() ? s1.size() : s2.size(); } int main() { string str1, str2, str3, str4; vector<string> vec = { "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN" }; int num = 1; cin >> str1 >> str2; cin >> str3 >> str4; unsigned int size = func(str1, str2); for (size_t n = 0; n < size; ++n) { if(str1[n] == str2[n] && isupper(str1[n]) && num!=0 && str1[n] < 'H'){ cout << vec[str1[n] - 'A'] << " "; num = 0; continue; } if (str1[n] == str2[n] && num == 0) { if (isdigit(str1[n])) { if (str1[n] <= '9') cout << '0'; cout << str1[n] << ":"; break; } else if(isupper(str1[n]) && str1[n] < 'O'){//测试点2,A~N cout << str1[n] - 'A' + 10 << ":"; break; } } } unsigned int isize = func(str3, str4); for (size_t n = 0; n < isize; ++n) { if(str3[n] == str4[n]) if (isalpha(str3[n])) { if (n < 10) cout << '0' << n; else cout << n; break; } } return 0; }
C:
#include <stdio.h> #include <stdlib.h> #include <string.h> //#pragma warning(disable:4996) size_t func(char *s1, char *s2) { return strlen(s1) < strlen(s2) ? strlen(s1) : strlen(s2); } int main() { char str1[60], str2[60], str3[60], str4[60]; const char *vec[10] = { "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN" }; int num = 1; unsigned int n; scanf("%s%s", &str1, &str2); scanf("%s%s", &str3, &str4); unsigned int size = func(str1, str2); for (n = 0; n < size; ++n) { if(str1[n] == str2[n] && str1[n] >= 'A' && str1[n] < 'H' && num != 0){//测试点4,A~G printf("%s", vec[str1[n] - 'A']); printf(" "); num = 0; continue; } if (str1[n] == str2[n] && num == 0) { if (str1[n]>='0' && str1[n] <= '9') { if (str1[n] <= '9') printf("0"); printf("%c:", str1[n]); break; } else if(str1[n] >= 'A' && str1[n] < 'O'){//测试点2,A~N printf("%d:",str1[n] - 'A' + 10); break; } } } unsigned int isize = func(str3, str4); for (n = 0; n < isize; ++n) { if(str3[n] == str4[n]) if ((str3[n] >= 'A' && str3[n] <= 'Z') || (str3[n] >= 'a' && str3[n] <= 'z')) { if (n < 10) printf("0%d", n); else printf("%d", n); break; } } //system("PAUSE"); return 0; }

浙公网安备 33010602011771号