• 大小: 4KB
    文件类型: .py
    金币: 1
    下载: 0 次
    发布日期: 2021-06-03
  • 语言: Python
  • 标签:

资源简介

用python3.1写的银行家算法,主要用了类,数据类型为列表

资源截图

代码片段和文件信息

#coding=utf-8
import sys
class Banker():
    def __init__(selfmn):
        self.m=m
        self.n=n

    def safe(self):
        work=[]
        finish=[]
        f_num=0
        for i in range(n):
            ava=[]
            for j in range(m):
                ava.append(available[j])
            work.append(ava)
        ‘‘‘print(work)‘‘‘
            
        for i in range(n):
            finish.append(0)
            
        for i in range(n):
            if finish[i]==0:
                for j in range(m):
                    if need[i][j]<=work[i][j]:
                        work[i][j] +=allocation[i][j]
                        finish[i]=1 #有问题!
                        f_num +=1   #经i、j循环后值为15
                    else:                        
                        break
        ‘‘‘print(f_num)‘‘‘
        if f_num==n*m:
            return 1
        else:
            return 0
        
    def check(self):
        flag=1
        for i in range(n):
            for j in range(m):
                #try:
                if request[i][j]<=need[i][j] and request[i][j]<=available[j]:
                    #‘int‘ object is not iterable可迭代的
                    available[j] -=request[i][j]
                    allocation[i][j] +=request[i][j]
                    need[i][j] -=request[i][j]                        

                else:
                    ‘‘‘print(‘ok‘)‘‘‘
                    flag=0
                    break
                
        if flag==0:
            print(‘分配失败!‘)
            
        safe=Banker.safe(self)
        ‘‘‘print(safe)‘‘‘
        #safe() takes exactly 1 positional argument (0 given)需加self 

评论

共有 条评论

相关资源