题意:有两列摩托车,一列为从南向北行驶,一列从北向南行驶,且南北向路是双行线,而东西线路是单行线,要求的是两列摩托车经过十字路口的最短时间。
思路:小型模拟
using namespace std;
const int maxn=1000+5;
string s1,s2;
int n1,n2;
int cnt1,cnt2,ans;
int main()
{
cin>>s1>>s2;
n1=s1.size();
n2=s2.size();
while(cnt1<n1 || cnt2<n2)
{
if(cnt1==n1)
{
cnt2++;
ans++;
continue;
}
if(cnt2==n2)
{
cnt1++;
ans++;
continue;
}
if(s1[cnt1]=='L')
{
if(s2[cnt2]=='L')
{
cnt1++;
cnt2++;
ans++;
continue;
}
else
{
cnt2++;
ans++;
continue;
}
}
else
{
if(s2[cnt2]=='L')
{
cnt1++;
ans++;
continue;
}
else
{
cnt1++;
cnt2++;
ans++;
continue;
}
}
}
cout<<ans<<endl;
return 0;
}
本文介绍了一个简单的模拟问题:两队摩托车分别从相反方向通过十字路口,需计算它们全部通过的最短时间。采用C++实现,通过小型模拟算法解决该问题。
769

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



