【cf补题记录】A. Hotelier

思考之后再看题解,是与别人灵魂之间的沟通与碰撞


A. Hotelier

题意 给出长度为n的字符串,字符串由'L'、'R'以及数字0~9组成。旅馆有10间房子,L代表客人从左边入住,R代表客人从右边入住,数字则表示第i间房子客人退房了。问经过这n次操作后,现在的旅店入住情况。

分析 n的范围比较小,完全可以暴力模拟实现。'L'就从左往右遍历看有没有空的房子,'R‘就从右往左遍历有没有空的房子。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#include<string>
#include<stack>
using namespace std;

int room[11];
string s;
int n;

int main()
{
    memset(room, 0, sizeof(room));
    cin >> n;
    cin >> s;
    for(int i = 0; i < s.size(); i++){
        if(s[i] == 'L'){
            for(int j = 0; j < 10; j++){
                if(room[j] == 0){
                    room[j] = 1;
                    break;
                }
            }
        }
        else if(s[i] == 'R'){
            for(int j = 9; j >= 0; j--){
                if(room[j] == 0){
                    room[j] = 1;
                    break;
                }
            }
        }
        else if('0' <= s[i] && s[i] <= '9'){
            int num = s[i] - '0';
            room[num] = 0;
        }
    }

    for(int i = 0; i < 10; i++){
        cout << room[i];
    } cout << endl;

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Ayanowww/p/11367377.html