这类题目几乎不需要数据结构,算法基础,主要通过简单的逻辑流程和判断实现。
题目描述如下:
题目大致意思:
输入四个字符串,在头两个字符串中获得星期和小时的信息,在最后两个字符串中获得分钟的信息,星期的获取方法为,检查头两个字符串,第一次出现的相同位置的大写字母作为星期的编号(具体代表含义如题干所示);获得小时的方法为:检查头两个字符串,第二次出现的相同位置的数字或大写字母代表小时(具体代表含义如题干所示);获取分钟的方法为:检查最后两个字符串,得到第一次相同字母的出现位置,即代表分钟数。
提交结果:
只得到了14分,考虑了挺久,想不出遗漏了何种情况,暂时放下。
代码如下:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1, str2, str3, str4;
string ans;
string week[] = { "MON","TUE","WED","THU","FRI","SAT","SUN" };
cin >> str1 >> str2 >> str3 >> str4;
int count = 0;
for (int i = 0; i < str1.size() && i < str2.size(); i++)
{
if (str1[i] == str2[i] && count == 0 && str1[i] >= 'A' && str1[i] <= 'G')
{
int day = str1[i] - 'A';
ans =ans+ week[day]+" ";
count++;
continue;
}
if (str1[i] == str2[i] && count == 1 && ((str1[i] >= '0' && str1[i] <= '9') || (str1[i] >= 'A' && str1[i] <= 'N')))
{
int temp = 0;
string t;
if (str1[i] >= '0' && str1[i] <= '9')
{
temp = str1[i] - '0';
t = '0' + to_string(temp);
}
else
{
temp = str1[i] - 'A' + 10;
t = to_string(temp);
}
ans = ans + t + ':';
break;
}
}
for (int i = 0; i < str2.size() && i < str3.size(); i++)
{
if (str3[i] == str4[i]&&((str3[i]>='a'&&str3[i]<='z')|| (str3[i] >= 'A' && str3[i] <= 'Z')))
{
string temp;
if (i < 10)
{
temp = '0' + to_string(i);
}
else {
temp = to_string(i);
}
ans = ans + temp;
break;
}
}
cout << ans << endl;
}
提交后,排名上升到28245。