| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 786 人关注过本帖, 1 人收藏
标题:银行家算法
只看楼主 加入收藏
yuhailong8828
该用户已被删除
收藏(1)
 问题点数:0 回复次数:2 
银行家算法
提示: 作者被禁止或删除 内容自动屏蔽
搜索更多相关主题的帖子: 银行家 算法 
2008-06-22 12:02
StarWing83
Rank: 8Rank: 8
来 自:仙女座大星云
等 级:贵宾
威 望:19
帖 子:3951
专家分:748
注 册:2007-11-16
收藏
得分:0 
/*****************************************************************
** HighlightCodeV3.0 software by yzfy(雨中飞燕) http:// **
*****************************************************************/
#include <stdio.h>
#include <stdlib.h>

#define N 3
#define M 3
#define F 1000
int issafe(int need1[N][M],int allo1[N][M],int max1[N][M],int avai1[M])
{
   
int need[N][M],allo[N][M],max[N][M],avai[M];
    int i,j,flag,m=0,p[M],z;

    for (i=0;i<N;i++)
    {
        
for (j=0;j<N;j++)
        {
            
need[i][j]=need1[i][j];
            allo[i][j]=allo1[i][j];
            max[i][j]=max1[i][j];
        }
        
avai[i]=avai1[i];
    }
   
while (m<N)
    {
        
z=m;
        for (i=0;i<N;i++)
        {
            
flag=1;
            for (j=0;j<M;j++)
            {
               
if (need[i][j]>avai[j])
                {
                    
flag=0;break;
                }
            }
            
if (flag==1)
            {

               
for (j=0;j<M;j++)
                {
                    
max[i][j]=F;

                    need[i][j]=F;
                }
               
for (j=0;j<N;j++) {avai[j]+=allo[i][j];}
               
p[m]=i;  printf("p:%d-->",p[m]);
                m++;
            }

        }
        
if (z==m) return 0;
    }
   
return 1;
}

void printinit(int max[N][M],int allo[N][M],int need[N][M],int avai[M])
{
   
int i,j;
    printf("\nThe max\n");
    for (i=0;i<N;i++)
    {
        
printf("p%d: ",i);
        for (j=0;j<N;j++)
        {
            
printf("%4d",max[i][j]);

        }
        
printf("\n");
    }
   
printf("\nThe allocation\n");
    for (i=0;i<N;i++)
    {
        
printf("p%d: ",i);
        for (j=0;j<N;j++)
        {
            
printf("%4d",allo[i][j]);

        }
        
printf("\n");
    }
   
printf("\nThe need\n");
    for (i=0;i<N;i++)
    {
        
printf("p%d: ",i);
        for (j=0;j<N;j++)
        {
            
printf("%4d",need[i][j]);

        }
        
printf("\n");
    }
   
printf("\nThe available\n    ");
    for (i=0;i<N;i++)
    {
        
printf("%4d",avai[i]);
    }

}
void init(int max[N][M],int allo[N][M],int need[N][M],int avai[M])
{
   
int i,j;
    printf("\nplease input the max!");
    for (i=0;i<N;i++)
    {
        
printf("\ninput the max of p:%d\n",i);
        for (j=0;j<M;j++)
            scanf("%d",&max[i][j]);
    }
   
printf("\nplease input the allocation!");
    for (i=0;i<N;i++)
    {
        
printf("\ninput the max of p:%d\n",i);
        for (j=0;j<M;j++)
            scanf("%d",&allo[i][j]);
    }
   
printf("\nplease input the available!\n");
    for (i=0;i<M;i++)
        scanf("%d",&avai[i]);
    for (i=0;i<N;i++)
        for (j=0;j<M;j++)
            need[i][j]=max[i][j]-allo[i][j];
}
int main()
{
   
int i,j,t,d[N];
    int y,e=1,repay=0;
    char r;
    int max[N][M]={{7,5,3},{3,2,2},{9,0,2}};
    int allo[N][M]={{0,1,0},{2,0,0},{3,0,2}};
    int need[N][M];
    int avai[M]={5,5,2};
    for (i=0;i<N;i++)
        for (j=0;j<M;j++)
            need[i][j]=max[i][j]-allo[i][j];
    printinit(max,allo,need,avai);
    printf("\nif you want to reset it please input R,or put other key to continue\n");
    r=getchar();
    if (r=='R'||r=='r')
        init(max,allo,need,avai);
    for (;;)
    {
        
printinit(max,allo,need,avai);
        printf("\nplese any key to check the now station\n");
        getchar();
        if (issafe(need,allo,max,avai))
        {
            
printf("\nnow the station is safe!\n");
            printf("\npealse input which pocess(0,1,2......N-1)\n");
            scanf("%d",&t);
            printf("pealse input the pocess want to apply resource\n");
            while (e==1)
            {
               
for (i=0;i<M;i++)
                    scanf("%d",&d[i]);
                for (i=0;i<M;i++)
                    if (d[i]>need[t][i])
                        {e=0;}
               
if (e==0)
                {
                    
printf("\nerror!\nthe apply is exceed the need!please reset!\n");
                    printf("the need:") ;
                    for (j=0;j<M;j++)
                        printf("%4d",need[t][j]) ;
                    printf("\nbe sure unexceed the need\npealse input the pocess want to apply resource\n");
                    e=1;
                }
               
else
                    
e=0;
            }
            
e=1;
            printf("is checking now.....");
            for (i=0;i<M;i++)
            {
               
need[t][i]-=d[i];
                allo[t][i]+=d[i];
                avai[i]-=d[i];
            }
            
if (issafe(need,allo,max,avai))
            {
               
for (i=0;i<M;i++)

                    if (need[t][i]!=0)
                        {repay=1;break;}

               
if (repay==0)
                    for (i=0;i<M;i++)
                    {
                        
avai[i]+=max[t][i];
                        max[t][i]=0;
                        allo[t][i]=0;
                    }


               
printf("\nsuccess!\n");
            }

            
else
            
{
               
printf("\nfail\n");
                for (i=0;i<M;i++)
                {
                    
need[t][i]+=d[i];
                    allo[t][i]-=d[i];
                    avai[i]+=d[i];
                }
            }
        }
        
else
            
printf("\nnow the station is unsafe!\n");


        printf("\nIf you want to continue,please input 1\n");
        printf("If you want to exit,please input 0\ncontinue or not?[1/0]");
        scanf("%d",&y);
        if (y==0)
            break;
    }
   
return 0;
}


编译出N个错误,已经解决了。

专心编程………
飞燕算法初级群:3996098
我的Blog
2008-06-22 12:14
yuhailong8828
该用户已被删除
收藏
得分:0 
回复 2# StarWing83 的帖子
提示: 作者被禁止或删除 内容自动屏蔽
2008-06-24 10:44
快速回复:银行家算法
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.036169 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved