上图是新浪微博上的一则趣闻,是瑞典斯德哥尔摩火车上的一道题,看上去是段伪代码:
s = ''
a = '1112031584'
for (i = 1; i < length(a); i++) {
if (a[i] % 2 == a[i-1] % 2) {
s += max(a[i], a[i-1])
}
}
goto_url('www.multisoft.se/' + s)
其中字符串的 +
操作是连接两个字符串的意思。所以这道题其实是让大家访问网站 www.multisoft.se/112358
(注意:比赛中千万不要访问这个网址!!!)。
当然,能通过上述算法得到 112358
的原始字符串 a
是不唯一的。本题就请你判断,两个给定的原始字符串,能否通过上述算法得到相同的输出?
输入格式:
输入为两行仅由数字组成的非空字符串,长度均不超过 104,以回车结束。
输出格式:
对两个字符串分别采用上述斯德哥尔摩火车上的算法进行处理。如果两个结果是一样的,则在一行中输出那个结果;否则分别输出各自对应的处理结果,每个占一行。题目保证输出结果不为空。
输入样例 1:
1112031584
011102315849
输出样例 1:
112358
输入样例 2:
111203158412334
12341112031584
输出样例 2:
1123583
112358
#include<iostream>
class tring //构建一个字符串的类,用于得到原始字符串
{
public:
std::string s;
tring(std::string); //函数用于得到字符串,注意s的类型要是string
~tring(){} //析构该函数
};
tring::tring(std::string String)
{
std::string result=""; //注意result的初始化和类型是string
for (int i = 1; i < (int)String.size(); i++)
{
if (String[i] % 2 == String[i - 1] % 2)
{
result+= std::max(String[i], String[i - 1]);
}
}
s = result;
}
void compare(tring s1,tring s2) //比较两原函数
{
if (s1.s ==s2.s)
std::cout << s1.s;
else
std::cout << s1.s << std::endl << s2.s;
}
int main()
{
std::string s1, s2;
std::cin >> s1 >> s2;
tring N1(s1);
tring N2(s2);
compare(s1, s2);
}