201903-4 메시지 전달 인터페이스 문자열 처리

문자열 처리 :
1.stringstream ss;
헤더 파일 : #include sstream
기능 및 설명 : 문자열로 초기화 한 후 변수로 출력하면 cin과 동일하며 공백은 자동으로 구분자로 사용
2.deque , Double-ended queue를 사용하면 전송 및 수신이 가능할 때 대기열의 헤드가 삭제됩니다.
코드 쇼 :

#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