201903-4 消息传递接口  字符串处理

字符串的处理:
1.stringstream ss;
头文件:#include sstream
作用及说明:用字符串初始化后再输出到一个变量中时,此时相当于cin,自动将空格作为分界符
2.deque,使用双端队列,当可以传递并接收时队头出队删除。
代码如下:

#include <iostream>
#include <deque>
#include <cstdio>
#include <sstream>
#include <string>
using namespace std;
int t,n;
int strtoint(string s)
{
    
    
    int num = 0;
    string str = s.substr(1);
    for(int i=0;i<str.size();i++)
    {
    
    
        num = num*10+str[i]-'0';
    }
    return num;
}
int main()
{
    
    
    int i,j;
    string str;
    cin>>t>>n;
    getchar();
    deque<string> process[n];
    for(i=0;i<t;i++)
    {
    
    
        for(j=0;j<n;j++)
            process[j].clear();
        for(j=0;j<n;j++)
        {
    
    
            getline(cin,str);
            stringstream ss;
            ss<<str;
            while(ss>>str)
            {
    
    
                process[j].push_back(str);
            }
        }
        int num = 1;
        j=0;
        while(num<=n)
        {
    
    
            if(process[j].size()==0)
            {
    
    
                j = (j+1)%n;
                num++;
            }else{
    
    
                string s = process[j].front();
                int numbers = strtoint(s);
                if(process[numbers].size() == 0)
                        break;
                if(s[0] == 'R')
                {
    
    
                    string s1 = process[numbers].front();
                    if(s1[0] == 'S'&&(strtoint(s1))==j)
                    {
    
    
                        process[j].pop_front();
                        process[numbers].pop_front();
                        num=1;
                    }else
                    {
    
    
                        j = (j+1)%n;
                        num++;
                    }
                }else if(s[0] == 'S')
                {
    
    
                    string s1 = process[numbers].front();
                    if(s1[0] == 'R'&&(strtoint(s1))==j)
                    {
    
    
                        process[j].pop_front();
                        process[numbers].pop_front();
                        num=1;
                    }else
                    {
    
    
                        j = (j+1)%n;
                        num++;
                    }
                }

            }
        }
        int flag = 0;
        for(j=0;j<n;j++)
            if(process[j].size()!=0)
            {
    
    
                cout<<"1"<<endl;
                flag = 1;
                break;
            }
        if(flag == 0)
            cout<<"0"<<endl;
    }
    return 0;
}



猜你喜欢

转载自blog.csdn.net/weixin_44142774/article/details/113848551
今日推荐