車庫管理



描述

有一個固定車位的車庫,如果車庫還有車位,則允許進入,如果車庫已滿,有來車入庫時,必須拒絕進入

知識點 字符串,循環,鏈表,隊列,棧,查找,搜索,排序,樹,圖,數組,函數,指針,枚舉,位運算,結構體,聯合體,文件操作,遞歸
運行時間限制 無限制
內存限制 無限制
輸入

設置車庫容量:

    capacity 20

    表明設置車庫的容量爲20,相當於只能進入20輛車

 

車輛入庫:

    in

    表明有一輛車進入車庫

 

車輛出庫:

    out

    表明有一輛車離開車庫

 

輸入結束:

    end

    表明本次輸入結束

 


輸出

當前車庫剩餘容量足夠時,車輛進入輸出allow,否則輸出reject

 

允許車輛入庫:

    allow

    表明允許車輛入庫,車輛入庫後,車庫剩餘容量減一

 

拒絕車輛入庫:

    reject

    表明當前車庫已滿,即剩餘容量爲0,拒絕車輛入庫

 


樣例輸入
capacity 20
in
in
out
out
end
樣例輸出
allow
allow

#include<iostream>
#include<sstream>
using namespace std;
int m =0,k= 0;
int main()
{
	string ca = "capacity";
	string in = "in";
	string out = "out";
	string end = "end";
	string input = "";
	char a[1024];
	int i = 0;

	while (cin.getline(a, 1024))
	{
		i = 0; input = "";
		while (a[i] != '\0')
		{
			input += a[i];
			i++;
		}


		if (input == in)
		{
			if (k < m)
			{
				k++;
				cout << "allow" << endl;
			}
			else
			{
				cout << "reject" << endl;
			}
		}
		if (input == out)
		{
			if (k > 0)
				k--;
		}
		if (input == end)
		{
			m = k = 0;
		}
		else
		{
			i = 0;
			input = "";
			while (a[i] != ' ')
			{
				input += a[i];
				i++;
			}
			if (input == ca)
			{
				input = "";
				while (a[++i] != '\0')
				{
					input += a[i];
				}
				stringstream ss;
				ss << input;
				ss >> m;
				k = 0;
			}

		}


	}
	return 0;

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章