
上图是新浪微博上的一则趣闻,是瑞典斯德哥尔摩火车上的一道题,看上去是段伪代码:
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
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
解题思路:按照题目给定的算法计算两个字符串得到的结果,然后按要求输出即可。
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
string calculate(string s) //计算
{
string result="";
for(int i=1;i<(int)s.length();i++)
{
if((s[i]-'0')%2==(s[i-1]-'0')%2)
{
if(s[i]>s[i-1])
result+=s[i];
else
result+=s[i-1];
}
}
return result;
}
int main()
{
string s1,s2;
cin>>s1;
cin>>s2;
string result1 = calculate(s1);
string result2 = calculate(s2);
if(result1==result2)
cout<<result1;
else
{
cout<<result1<<endl;
cout<<result2;
}
}
文章提供了一个基于伪代码的字符串处理问题,要求根据特定算法判断两个字符串是否能产生相同的输出。这个问题涉及到字符串操作、条件判断和循环,解决方案是实现题目描述的算法并比较结果。
944

被折叠的 条评论
为什么被折叠?



