zoukankan      html  css  js  c++  java
  • 操作系统 银行家算法

    谁知道对不对 反正看着挺像的

    试了一下过程考核的题 好像是挺像的

      1 #include<bits/stdc++.h>
      2 #define cl(a,b) memset(a,b,sizeof(a))
      3 using namespace std;
      4 
      5 const int maxn=1e3+10;
      6 
      7 int n,m;
      8 int ans[maxn],tmp[maxn];
      9 int available[maxn];
     10 int need[maxn][maxn];
     11 int allocation[maxn][maxn];
     12 bool ok,vis[maxn];
     13 
     14 void init()
     15 {
     16     ok=false;
     17     cl(vis,false);
     18     cl(ans,0),cl(tmp,0);
     19     cl(available,0);
     20     cl(need,0);
     21     cl(allocation,0);
     22 }
     23 
     24 bool cmp(int now)
     25 {
     26     int tmp[maxn];
     27     for(int i=0; i<m; i++)
     28     {
     29         tmp[i]=(available[i]+allocation[now][i]);
     30     }
     31     for(int i=0; i<m; i++)
     32     {
     33         if(tmp[i] < need[now][i]) return false;
     34     }
     35     return true;
     36 }
     37 
     38 void add(int now)
     39 {
     40     for(int i=0; i<m; i++)
     41     {
     42         available[i]+=allocation[now][i];
     43     }
     44 }
     45 
     46 void div(int now)
     47 {
     48     for(int i=0; i<m; i++)
     49     {
     50         available[i]-=allocation[now][i];
     51     }
     52 }
     53 
     54 void read(int a[][maxn])
     55 {
     56     for(int i=0; i<n; i++)
     57     {
     58         for(int j=0; j<m; j++)
     59         {
     60             scanf("%d",&a[i][j]);
     61         }
     62     }
     63 }
     64 
     65 void read(int a[])
     66 {
     67     for(int i=0; i<m; i++)
     68     {
     69         scanf("%d",&a[i]);
     70     }
     71 }
     72 
     73 void print(int a[])
     74 {
     75     for(int i=0; i<n; i++)
     76     {
     77         printf("%d%c",a[i],i==n-1?'
    ':' ');
     78     }
     79 }
     80 
     81 void print(int a[][maxn])
     82 {
     83     for(int i=0; i<n; i++)
     84     {
     85         for(int j=0; j<m; j++)
     86         {
     87             printf("%d%c",a[i][j],j==m-1?'
    ':' ');
     88         }
     89     }
     90 }
     91 
     92 void check(int now)
     93 {
     94     if(now==n)
     95     {
     96         ok=true;
     97         for(int i=0; i<n; i++)
     98         {
     99             ans[i]=tmp[i];
    100         }
    101         return ;
    102     }
    103     for(int i=0; i<n; i++)
    104     {
    105         if(!vis[i]&&cmp(i))
    106         {
    107             tmp[n-now-1]=i+1;
    108             add(i);
    109             vis[i]=true;
    110             check(now+1);
    111             vis[i]=false;
    112             div(i);
    113         }
    114     }
    115     return ;
    116 }
    117 
    118 int main()
    119 {
    120     init();
    121 //    freopen("in.txt","r",stdin);
    122     scanf("%d%d",&n,&m);
    123     read(need);
    124     read(allocation);
    125     read(available);
    126     check(0);
    127     if(ok)
    128     {
    129         printf("OK!
    ");
    130         print(ans);
    131     }
    132     else
    133     {
    134         printf("Can't!
    ");
    135     }
    136     return 0;
    137 }/*
    138 
    139 5 4
    140 
    141 0 0 1 2
    142 1 7 5 0
    143 2 3 5 6
    144 0 6 5 2
    145 0 6 5 6
    146 
    147 0 0 3 2
    148 1 0 0 0
    149 1 3 5 4
    150 0 3 3 2
    151 0 0 1 4
    152 
    153 1 6 2 2
    154 
    155 */
  • 相关阅读:
    socket-重叠模型(overlap)
    ssh 免密登陆
    安装google 框架
    为什么不同网段的ip 不能直接通信
    python中的import,reload,以及__import__
    C Runtime Library、C  Runtime
    SQLite3 C/C++ 开发接口简介
    mysql添加索引语句
    mysql 字段左右补0
    @Transactional注解的失效场景
  • 原文地址:https://www.cnblogs.com/general10/p/7010151.html
Copyright © 2011-2022 走看看