银行家算法(并没有注释

#include <iostream>
 
const int MAXM = 1;
const int MAXN = 100;
 
using namespace std;
 
int Resource[MAXM], Available[MAXM];
int Claim[MAXN][MAXM], Allocation[MAXN][MAXM];
int n;
 
int Need[MAXN][MAXM];
bool Finish[MAXN];
int finished;
 
bool IsAvailable(const int thisNeed[], const int thisAvailable[]){
    for(int i = 0; i < MAXM; i++){
        if(thisNeed[i] > thisAvailable[i]) return false;
    }
    return true;
}
 
void LineAdd(int LineA[], int LineB[]){
    for(int i = 0; i < MAXM; i++){
        LineA[i] += LineB[i];
    }
}
 
int main()
{
    cout << "Please input " << MAXM << " integers of Resource:" << endl;
    for(int i = 0; i < MAXM; i++) cin >> Resource[i];
    cout << "Please input " << MAXM << " integers of Available:" << endl;
    for(int i = 0; i < MAXM; i++) cin >> Available[i];
    cout << "Please input the number of process:" << endl;
    cin >> n;
    for(int i = 1; i <= n; i++){
        cout << "Please input " << MAXM << " integers of Claim[" << i << "]:" << endl;
        for(int j = 0; j < MAXM; j++) cin >> Claim[i][j];
    }
    for(int i = 1; i <= n; i++){
        cout << "Please input " << MAXM << " integers of Allocation[" << i << "]:" << endl;
        for(int j = 0; j < MAXM; j++) cin >> Allocation[i][j];
    }
    cout << "Running..." << endl;
    for(int i = 1; i <= n; i++){
        for(int j = 0; j < MAXM; j++){
            Need[i][j] = Claim[i][j] - Allocation[i][j];
        }
        Finish[i] = false;
    }
    finished = 0;
    for(int times = 0; times < n; times++){
        for(int i = 1; i <= n; i++){
            if(!Finish[i]){
                if(IsAvailable(Need[i], Available)){
                    Finish[i] = true;
                    finished++;
                    LineAdd(Available, Claim[i]);
                }
            }
        }
    }
    if(finished == n){
        cout << "True" << endl;
    }else{
        cout << "False" << endl;
    }
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容