我找个一个,供大家参考
#include <stdio.h>
int main(int argc, char *argv[])
{
int claim[5][3] = {{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};
int allocation[5][3] = {{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}};
int p,s = 0;
int count=0;
int need[5][3] = {{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}};
int result[5] = {-1,-1,-1,-1,-1};
int work[3] = {3,3,2};
printf("all source:\n A B C\n 10 5 7\n");
printf("available:\n A B C\n 3 3 2\n");
printf("every max source:\n A B C\n");
for(p=0;p<5;p++){
printf("P%d: ",p);
for(s=0;s<3;s++){
printf(" %d ",claim[p][s]);
need[p][s]=claim[p][s]-allocation[p][s];
}
printf("\n");
}
printf("allocation:\n A B C\n");
for(p=0;p<5;p++){
printf("P%d: ",p);
for(s=0;s<3;s++)
printf(" %d ",allocation[p][s]);
printf("\n");
}
printf("every need:\n A B C\n");
for(p=0;p<5;p++){
printf("P%d: ",p);
for(s=0;s<3;s++)
printf(" %d ",need[p][s]);
printf("\n");
}
for(s=0;s<5;s++)
for(p=0;p<5;p++){
if(result[p]==-1&&need[p][0]<=work[0]&&need[p][1]<=work[1]&&need[p][2]<=work[2]){
work[0]=work[0]+allocation[p][0];
work[1]=work[1]+allocation[p][1];
work[2]=work[2]+allocation[p][2];
result[p]=s;count++;
printf("P%d->",p);
}
}
if(count==5)
printf("\nit is safe!\n");
else
printf("\nit is danger\n");
return 0;
}