编程语言
首页 > 编程语言> > 银行家算法

银行家算法

作者:互联网

#include<iostream>
#include<vector>
using namespace std;
class banker
{
private:
	vector<vector<int>> All;
	vector<vector<int>> Need;
	vector<int> Ava;
	vector<int> t;
public:
	static int x;
	static int y;
 banker()
   {
	   cout << "请输入进程数和资源数" << endl;
	   cin >> x;//进程数
	   cin >> y;//资源类数目
	  if (x < 0 || y < 0)return;
	  All.resize(x);
	  for (int i = 0; i < x; ++i)
	   {
		All[i].resize(y);
	   }
	  for (int i = 0; i < x; ++i)
	  {
		  for (int j = 0; j < y; ++j)
		  {
			  cin >> All[i][j];
		  }
	  }
	  Need.resize(x);
	  for (int i = 0; i < x; ++i)
	  {
		  Need[i].resize(y);
	  }
	  cout << "请输入还需要的资源数" << endl;
	  for (int i = 0; i < x; ++i)
	  {
		  for (int j = 0; j < y; ++j)
		  {
			  cin >> Need[i][j];
		  }
	  }
	  cout << "请输入可得到的资源数目" << endl;
	  Ava.resize(y);
	  for (int i = 0; i < y; ++i)
	  {
		  cin >> Ava[i];
	  }

   }
   void output()
   {
	   cout << "资源分配如下" << endl;
	   cout << "All" << endl;
	   for (int i = 0; i < x; ++i)
	   {
		   for (int j = 0; j < y; ++j)
		   {
			   cout << All[i][j] << " ";
		   }
		   cout << endl;
	   }
	   cout << "Need" << endl;
	   for (int i = 0; i < x; ++i)
	   {
		   for (int j = 0; j < y; ++j)
		   {
			   cout << Need[i][j] << " ";
		   }
		   cout << endl;
	   }
	   cout << "Ava" << endl;
	   for (int i = 0; i < y; ++i)
	   {
	   		cout << Ava[i]<< " ";
	   }
	   cout << endl;
   }
   bool capity()
   {
	   vector<vector<int>> n(Need);
	   vector<bool>f;//是否查找完成
	   f.resize(x,false);
	   vector<int>w(Ava);//可提供的资源个数
	   int tag = 0;
	   while (1)
	   {
		   int m = -1;
		   tag = 0;
		   for (int i = 0; i < x; ++i)
		   {
			   for (int j = 0; j < y; ++j)
			   {
				   if (n[i][j] > w[j]||f[i]==true)break;
				   m = j;
			   }
			   if (m == y-1)
			   {
				   f[i] = true;
				   t.push_back(i);
				   tag = 1;
				   for (int k = 0; k < y; ++k)
				   {
					   w[k] = w[k] + All[i][k];
				   }
			   }
			   m = -1;
		   }
		   if(tag==0)
		   {
		   for (int i = 0; i < x; ++i)
		   {
			   if (f[i] != true)return false;		
		   }
			   return true;
		   }
		   
	   }	   
   }
   void op_t()
   {
	   for (int i = 0; i < x; ++i)
	   {
		   printf("p%d->", t[i]);
	   }
	   t.resize(0);
   }
   bool request(vector<int>&up,int i)
   {
	   for (int j = 0; j < y; ++j)
	   {
		   if (up[j] > Ava[j])return false;
		   Need[i][j] = Need[i][j] - up[j];
		   Ava[j] = Ava[j] - up[j];
		   All[i][j] = All[i][j] + up[j];
	   }
	 int t=capity();
	 for (int j = 0; j < y; ++j)
	 {
		 Need[i][j] = Need[i][j] + up[j];
		 Ava[j] = Ava[j] + up[j];
	 }
	 return t;
   }


};
int banker::x = 0;
int banker::y = 0;
int main()
{
    banker b1;
	b1.output();
	if (b1.capity())
	{
		b1.op_t();
		
	}else
	{
		cout << "无安全序列" << endl;
	}
	cout << "请输入申请的进程名及申请的资源数" << endl;
	vector<int> up;
	up.resize(banker::y);
	int i = 0;
	cin >> i;
	for (int i = 0; i < banker::y; ++i)
	{
		cin >> up[i];
	}
	if (b1.request(up,i))
	{
		b1.op_t();
	}
	else
	{
		cout << "无安全序列" << endl;
	}

	return 0;
}

标签:cout,int,up,++,算法,Need,Ava,银行家
来源: https://blog.csdn.net/weixin_48569863/article/details/122204250