高手请进,求指导,算法
程序代码:
#include<stdio.h> int available[3]={3,3,2}; int request[5][3]={0}; int max[5][3]={ {7,5,3}, {3,2,2}, {9,0,2}, {2,2,2}, {4,3,3,}}; int allocation[5][3]={ {0,1,1}, {2,0,0}, {3,0,2}, {2,1,1}, {0,0,2}}; int need[5][3]; int m; char c; void init(); void requestf(); int safe(); int judge(); int main(int argc,char *argv[]) { while(1) { init(); requestf(); if(!judge()) return 0; if(!safe()) return 0; printf("是否继续Y/N!"); scanf("%c",&c); if(c=='Y'||c=='y') continue; else return 1; } } void init() { int i,j; /* available[3]={3,3,2}; request[5][3]={0}; max[5][3]={ {7,5,3}, {3,2,2}, {9,0,2}, {2,2,2}, {4,3,3,}}; allocation[5][3]={ {0,1,1}, {2,0,0}, {3,0,2}, {2,1,1}, {0,0,2}};*/ for(i=0;i<5;i++) for(j=0;j<3;j++) need[i][j]=max[i][j]-allocation[i][j]; } void requestf() { int k; printf("请输入你要申请资源的进程\n"); scanf("%d",&m); printf("请输入各资源请求的数量\n"); for(k=0;k<3;k++) scanf("%d",&request[m][k]); } int judge() { int i; for(i=0;i<3;i++) if(request[m][i]>need[m][i] || request[m][i]>available[i]) { printf("请求的资源超过可利用的资源!"); printf("是否继续Y/N!"); scanf("%c",&c); return 0; } for(i=0;i<3;i++) { if(request[m][i]<=need[m][i] && request[m][i]<=available[i]) { available[i]-=request[m][i]; allocation[m][i]+=request[m][i]; need[m][i]-=request[m][i]; } else { printf("请求的资源超过可利用的资源!"); printf("是否继续Y/N!"); printf("所需要的资源不够,进程%d wait....",m); return 0; } } return 1; } int safe() { int i,j,l; int work[3]; int finish[5]={0}; int order[5]; for(i=0;i<3;i++) work[i]=available[i]; for(i=0;i<5;i++) { if(1==finish[i]) continue; else { for(j=0;j<3;j++) if(need[i][j]>work[j]) break; if(j==3) { work[j]+=allocation[i][j]; finish[i]=1; order[l++]=i; i=-1; } else continue; } } if(l==5) { printf("安全序列为:\n"); for(i=0;i<5;i++) printf("%d",order[i]); } return 1; }此为银行家算法 运行过程中有时会崩溃,求大侠看看