C++&&双指针

MP3光标位置

https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15

#include <iostream>

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    int n;
    string str1;
    while (cin >> n>>str1) 
    {
        if(n<=4)
        {
        	int s=0;
            for(char i : str1)
            {
                if(i=='U')
                {
                    s=(s-1+n)%n;
                }
                else
                {
                    s=(s+1)%n;
                }
            }
            for(int i=0;i<n;++i)
            {
                cout<<i+1<<" ";
            }
            cout<<endl;
            cout<<s+1;
        }
        else 
        {
            int p=0,q=3;
            int s=0;
            for(char c:str1)
            {
                if(c=='U')
                {
                    if(p==s&&s==0)
                    {
                        q=s=n-1;
                        p=q-3;
                    }
                    else if(p==s&&s!=0)
                    {
                        --p;--q;;--s;
                    }
                    else 
					{
                        --s;
                    }
                }
                else
                {
                    if(q==s&&s==n-1)
                    {
                        s=p=0;
                        q=p+3;
                    }
                    else if(q==s&&s!=n-1)
                    {
                        ++s;++p;++q;
                    }
                    else {
                        ++s;
                    }
                }
            }
            for(int i=p;i<=q;++i)
            {
                cout<<i+1<<" ";
            }
            cout<<endl;
            cout<<s+1;
        }
    }
}

// 64 位输出请用 printf("%lld")

全部评论

相关推荐

05-26 10:24
门头沟学院 Java
qq乃乃好喝到咩噗茶:其实是对的,线上面试容易被人当野怪刷了
找工作时遇到的神仙HR
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务