ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

银行家算法

2021-12-28 22:30:16  阅读:172  来源: 互联网

标签:cout int up ++ 算法 Need Ava 银行家


#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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有