操作系统:bank

#include <bits/stdc++.h>
#define sc(x) scanf("%lld", &(x))
#define pr(x) printf("%lld\n", x)
#define pt putchar(10)
#define mem(x, y) memset(x, y, sizeof(x))
#define For(i, j, k) for (int i = (int)(j); i <= (int)(k); i++)
#define Rep(i, j, k) for (int i = (int)(j); i >= (int)(k); i--)
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const double PI = acos(-1);
const int INF = 0x3f3f3f3f;
#define M 100
#define N 200
int n, m;
int Resource[M];
int Max[N][M];
int Allocation[N][M];
int Need[N][M];
int Available[M];
int Work[M];
bool Finish[N];
int List[N]; //存放安全序列的下标序列
void initial() {
    //创建初始状态:先输入 Resource、Max 和 Allocation
    //再计算出 Need、Available。
    //填补程序
    puts("How many processes and how many resources are there");
    cin >> n >> m;
    cout << n << "\t" << m << "\n";

    puts("Resourse,Total amount of each resource(m)");
    For(i, 0, m - 1) cin >> Resource[i], Available[i] = Resource[i];
    For(i, 0, m - 1) cout << Resource[i] << "\t";
    pt;
    puts("The maximum demand of each resource for n processes");
    For(i, 0, n - 1) For(j, 0, m - 1) cin >> Max[i][j];
    For(i, 0, n - 1) {
        For(j, 0, m - 1) cout << Max[i][j] << "\t";
        pt;
    }

    puts("How many resources are currently allocated to each process");
    For(i, 0, n - 1) For(j, 0, m - 1) cin >> Allocation[i][j];
    For(i, 0, n - 1) {
        For(j, 0, m - 1) cout << Allocation[i][j] << "\t";
        pt;
    }

    // puts("How much does each process need for each resource to run now");
    For(i, 0, n - 1) For(j, 0, m - 1) Need[i][j] = Max[i][j] - Allocation[i][j];

    puts("Show the resources needed by each process currently");
    For(i, 0, n - 1) {
        For(j, 0, m - 1) { cout << Need[i][j] << "\t"; }
        cout << "\n";
    }

    For(i, 0, n - 1) For(j, 0, m - 1) Available[j] -= Allocation[i][j];
    puts("Current number of resources remaining");
    For(i, 0, m - 1) cout << Available[i] << "\t";
    pt;
}
void printState() {
    //输出当前的状态表|Process |Max |Allocation |Need |Available|
    //填补程序
    pt;
    pt;
    puts("Current state");
    cout << "|Process"
         << "|Max\t\t\t"
         << "|Allocation\t\t"
         << "|Need\t\t\t"
         << "|Available\t\t|";
    pt;
    For(i, 0, n - 1) {
        cout << "|P" << i << "\t|";

        For(j, 0, m - 1) cout << Max[i][j] << "\t";
        cout << "|";

        For(j, 0, m - 1) cout << Allocation[i][j] << "\t";
        cout << "|";

        For(j, 0, m - 1) cout << Need[i][j] << "\t";
        cout << "|";

        if (!i)
            For(j, 0, m - 1) cout << Available[j] << "\t";
        if (!i)
            cout << "|";
        else
            cout << "\t\t\t|";
        pt;
    }
    pt;
    pt;
}
// int isfinish() {
//返回同时满足两个条件;
//{1.Finish[i]=false 2.Need[i][j]≤Work[j]}
//的进程下标i(修改Finish[i]=true),否则返回-1
//填补程序
//}
bool issafe() {
    //判定当前状态是否为安全状态
    //(返回 true 或 false),把安全序列的下标放入List[N]数组。
    //填补程序
    For(i, 0, n - 1) Work[i] = Available[i], Finish[i] = 0;
    int cnt = 0;
    For(i, 0, n - 1) {
        For(j, 0, n - 1) {
            int flag = 1;
            if (Finish[j])
                continue;
            For(k, 0, m - 1) if (Work[k] < Need[j][k]) flag = 0;
            if (flag) {
                Finish[j] = 1, List[cnt++] = j;
                For(k, 0, m - 1) Work[k] += Allocation[j][k];
                // cout << "j=" << j << " " << cnt << "\n";
            }
        }
        // cout << "i=" << i << "\n";
        if (cnt == n)
            return true;
    }

    return false;
}
void printList() {
    //输出安全序列表|Process |Work |Need |Allocation |Work+Alloc |Finish |
    //填补程序
    pt;
    pt;
    For(i, 0, m - 1) Work[i] = Available[i];
    puts("Current security sequence");
    cout << "|Process|Work\t\t\t|Need\t\t\t|Allocation\t\t|Work+"
            "Alloc\t\t|Finish\t|"
         << "\n";
    For(i, 0, n - 1) {
        cout << "|P" << List[i] << "\t|";

        For(j, 0, m - 1) cout << Work[j] << "\t";
        cout << "|";

        For(j, 0, m - 1) cout << Need[i][j] << "\t";
        cout << "|";

        For(j, 0, m - 1) cout << Allocation[List[i]][j] << "\t";
        cout << "|";

        For(j, 0, m - 1) {
            Work[j] += Allocation[List[i]][j];
            cout << Work[j] << "\t";
        }
        cout << "|";
        cout << "true\t"
             << "|";
        pt;
    }
    pt;
    pt;
}
void reqresource(int pos, int request[M]) {
    //表示第 i 个进程请求 M 类资源 request[M]
    // bool flag;
    // int j;
    // Step1: 判断条件 Request[j]≤Need[i][j]
    //填补程序
    // Step2: 判断条件 Request[j]≤Available[j]
    //填补程序
    // Step3: 预先分配
    //填补程序
    // Step4: 检测是否为安全状态
    //填补程序
    int flag = 1;
    For(i, 0, m - 1) {
        if (request[i] > Need[pos][i]) {
            puts("Request error:Request greater than the demand");
            cout << request[i] << " " << Need[i] << "\n";
            return;
        }
        if (request[i] > Available[i]) {
            puts("Request error:Request greater than available");
            cout << request[i] << " " << Available[i] << "\n";
            return;
        }
    }
    For(i, 0, m - 1) {
        Need[pos][i] -= request[i];
        Available[i] -= request[i];
        Allocation[pos][i] += request[i];
    }
    if (!issafe()) {
        puts("Resource request failed! Security check: enter unsafe state! ");
        puts("Restore previous state");
        For(i, 0, m - 1) {
            Need[pos][i] += request[i];
            Available[i] += request[i];
            Allocation[pos][i] -= request[i];
        }
    } else {
        puts("Successful resource application");
        puts("Show current security sequence");
        printList();
    }
}
int main() {
    // freopen("blank.in", "r", stdin);
    int reqid = -1, j, req[M];
    initial();
    printState();
    // cout << issafe() << endl;
    if (!issafe())
        puts("Initial state is unsafe!");
    // printf("Initial state is unsafe!\n");
    else {
        pt;
        puts("Initial state is safe!");
        // printf("\nInitial state is safe!\n");
        // For(i, 0, n - 1) cout << List[i] << " ";
        // pt;
        printList();
        puts("Input the id of request process: ( When input - 1 stops");
        while (cin >> reqid) {
            if (reqid == -1)
                break;
            if (reqid < 0 || reqid >= n)
                puts("Illegal input process ID");
            puts("Input request resources:");
            For(i, 0, m - 1) cin >> req[i];
            reqresource(reqid, req);
            printState();
            puts("Input the id of request process: ( When input - 1 stops");
        }
        // scanf("%d", &reqid);
        // while (reqid >= 0 && reqid < n) { //输入进程 id 是否合法
        //     printf("Input request resources:");
        //     for (j = 0; j < M; j++)
        //         scanf("%d", &req[j]);
        //     reqresource(reqid, req);
        //     printState();
        //     printf("Input the id of request process:");
        //     scanf("%d", &reqid);
        // }
    }
    return 0;
}
#include <bits/stdc++.h>
#define sc(x) scanf("%lld", &(x))
#define pr(x) printf("%lld\n", x)
#define pt putchar(10)
#define mem(x, y) memset(x, y, sizeof(x))
#define For(i, j, k) for (int i = (int)(j); i <= (int)(k); i++)
#define Rep(i, j, k) for (int i = (int)(j); i >= (int)(k); i--)
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
const ll mod = 1e9 + 7;
const double PI = acos(-1);
const int INF = 0x3f3f3f3f;
char s[N];
struct node {
    int key;        //进程ID
    int sequence;   //进程进入队列顺序号
    string massage; //进程说明信息
};
int main() {
    int count = 0;
    int n;
    puts("How many processes");
    cin >> n;
    // node *p,*q;
    puts("The new process control table:");
    puts("keysequence message");
    queue<node> que;
    For(i, 0, n - 1) {
        node tmp;
        cin >> tmp.key >> tmp.sequence >> tmp.massage;
        que.push(tmp);
    }
    puts("The table is:");
    queue<node> p;
    For(i, 0, n - 1) {
        node tmp = que.front();
        que.pop();
        cout << tmp.key << "\t" << tmp.sequence << "\t" << tmp.massage << "\n";
        p.push(tmp);
    }
    For(i, 0, n - 1) {
        pt;
        node tmp = p.front();
        p.pop();
        cout << "The " << ++count << "th scheduled process:\nkey = " << tmp.key
             << ", sequence = " << tmp.sequence << ", message = " << tmp.massage
             << "\n";
    }
    return 0;
}
/*
4
22 1 process22
30 2 process30
13 3 process13
90 4 process90
*/
#include <bits/stdc++.h>
#define sc(x) scanf("%lld", &(x))
#define pr(x) printf("%lld\n", x)
#define pt putchar(10)
#define mem(x, y) memset(x, y, sizeof(x))
#define For(i, j, k) for (int i = (int)(j); i <= (int)(k); i++)
#define Rep(i, j, k) for (int i = (int)(j); i >= (int)(k); i--)
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
const ll mod = 1e9 + 7;
const double PI = acos(-1);
const int INF = 0x3f3f3f3f;
char s[N];
struct node {
    int key;        //进程ID
    int priority;   //进程进入队列顺序号
    string massage; //进程说明信息
    bool operator<(const node &a) const { return priority < a.priority; }
};
int main() {
    int count = 0;
    int n;
    puts("How many processes");
    cin >> n;
    // node *p,*q;
    puts("The new process control table:");
    puts("keysequence message");
    priority_queue<node> q;
    For(i, 0, n - 1) {
        node tmp;
        cin >> tmp.key >> tmp.priority >> tmp.massage;
        q.push(tmp);
    }
    puts("The table is:");
    priority_queue<node> p;
    For(i, 0, n - 1) {
        node tmp = q.top();
        q.pop();
        cout << tmp.key << "\t" << tmp.priority << "\t" << tmp.massage << "\n";
        p.push(tmp);
    }
    For(i, 0, n - 1) {
        pt;
        node tmp = p.top();
        p.pop();
        cout << "The " << ++count << "th scheduled process:\nkey = " << tmp.key
             << ", sequence = " << tmp.priority << ", message = " << tmp.massage
             << "\n";
    }
    return 0;
}
/*
5
1 20 process1
2 50 process2
3 30 process3
4 10 process4
5 40 process5
*/
#include <bits/stdc++.h>
#define sc(x) scanf("%lld", &(x))
#define pr(x) printf("%lld\n", x)
#define pt putchar(10)
#define mem(x, y) memset(x, y, sizeof(x))
#define For(i, j, k) for (int i = (int)(j); i <= (int)(k); i++)
#define Rep(i, j, k) for (int i = (int)(j); i >= (int)(k); i--)
using namespace std;
typedef long long ll;

struct node {
    int key;        //进程ID
    string massage; //进程说明信息
    int run_time;   //进程需要运行时间
};

queue<node> q;

void PrintCurrentState(int num) {
    if(num<=0) return;
    while(num--) {
        node tmp = q.front();
        q.pop();
        cout << tmp.key << "\t" << tmp.run_time << "\t" << tmp.massage << "\n";
        q.push(tmp);
    }
}


int main() {
    int count = 0;
    int n;
    int WaitTime = 0, cnt = 0;
    puts("How many processes");
    cin >> n;
    int num = 0;
    puts("The new process control table:");
    puts("keysequence message");
    For(i, 0, n - 1) {
        node tmp;
        cin >> tmp.key >> tmp.run_time >> tmp.massage ;
        q.push(tmp);
    }
    puts("The table is:");
    while(num--) {
        node tmp = q.front();
        q.pop();
        cout << tmp.key << "\t" << tmp.run_time << "\t" << tmp.massage << "\n";
        q.push(tmp);
    }
    while(q.size()) {
        pt;
        pt;
        node tmp = q.front();
        q.pop();
        cout << "The " << ++count << "th scheduled process:\nkey = " << tmp.key
             << ", run_time = " << tmp.run_time << ", message = " << tmp.massage;
        pt;
        pt;
        if(tmp.run_time == 1) WaitTime++, tmp.run_time = 0;
        else WaitTime += 2, tmp.run_time -= 2;
        if(tmp.run_time) q.push(tmp);
        else n--;
        puts("Recent table is:");
        cout<<n<<"\n";
        PrintCurrentState(n);

//        int start_time = WaitTime;
//        cnt++;
//        WaitTime += tmp.run_time;
//        cout << "The " << ++count << "th scheduled process:\nkey = " << tmp.key
//             << ", sequence = " << tmp.sequence << ", message = " << tmp.massage
//             << ", AverageWaiting = " << (double) WaitTime / cnt << ", StartTime = "
//             << start_time << ", EndTime = " << WaitTime << ", RunTime = " << tmp.run_time 
//             
//             << "\n";
    }
}
/*
4
1 2 process1
2 4 process2
3 6 process3
4 3 process4
*/
#include <bits/stdc++.h>
#define sc(x) scanf("%lld", &(x))
#define pr(x) printf("%lld\n", x)
#define pt putchar(10)
#define mem(x, y) memset(x, y, sizeof(x))
#define For(i, j, k) for (int i = (int)(j); i <= (int)(k); i++)
#define Rep(i, j, k) for (int i = (int)(j); i >= (int)(k); i--)
using namespace std;
typedef long long ll;

struct node
{
    int address;    //存储分区起始地址
    int length;     //存储分区长度
    int flag;       //存储分区标志,0 为空闲,1 为被作业占据
    string s;  //当 flag==1 时存储分区占用标志作业名,否则存储空 nil
    bool operator<(const node& x) const
    {
        return address > x.address;      //从小到大排序
    }
};
priority_queue<node> dtable1,ftable1,dtable2,ftable2;
void create1()
{
    puts("address length flag(0 or 1) end with 0 0 0");
    while(1)
    {
        int add,len,fl;
        cin>>add>>len>>fl;
        if(!add&&!len&&!fl) break;
        node tmp;
        tmp.address=add,tmp.flag=fl,tmp.length=len;
        printf("input job_name:");
        cin>>tmp.s;
        dtable1.push(tmp);
        pt;
        puts("address length flag(0 or 1) end with 0 0 0");
    }
}
void create2()
{
    puts("address length flag(0 or 1) end with 0 0 0");
    while(1)
    {
        int add,len,fl;
        cin>>add>>len>>fl;
        if(!add&&!len&&!fl) break;
        node tmp;
        tmp.address=add,tmp.flag=fl,tmp.length=len;
        tmp.s="nil";
        ftable1.push(tmp);
        pt;
        puts("address length flag(0 or 1) end with 0 0 0");
    }
    pt;
}
void show()
{
    pt;
    puts("The free table is :");
    while(ftable1.size())
    {
        node tmp=ftable1.top();
        ftable1.pop();
        cout<<tmp.address<<","<<tmp.length<<","<<tmp.flag<<","<<tmp.s<<endl;
        ftable2.push(tmp);
    }
    pt;
    puts("The distributed table is :");
    while(dtable1.size())
    {
        node tmp=dtable1.top();
        dtable1.pop();
        cout<<tmp.address<<","<<tmp.length<<","<<tmp.flag<<","<<tmp.s<<endl;
        dtable2.push(tmp);
    }
    pt;
}
bool chk(int len,string s)
{
    int jude=0;
    int flag=1;
    while(ftable2.size())
    {
        node tmp1=ftable2.top();
        ftable2.pop();
        if(flag&&tmp1.length>=len)
        {
            node tmp2;
            tmp2.length=len,tmp2.flag=1,tmp2.s=s;
            tmp2.address=tmp1.address;
            dtable1.push(tmp2);
            jude=1;
            flag=0;
            if(tmp1.length-len!=0)
            {
                tmp1.address+=len;
                tmp1.length-=len;
                ftable1.push(tmp1);
            }
        }
        else if(!flag||tmp1.length<len)
        {
            ftable1.push(tmp1);
        }
    }
    while(dtable2.size())
    {
        node tmp=dtable2.top();
        dtable2.pop();
        dtable1.push(tmp);
    }
    return jude;
}
int main()
{
    puts("The distributed table is:");
    create1();
    puts("The free table is:");
    create2();
    show();
    while(1)
    {
        puts("The length of worked job is:");
        int tmpl;
        cin>>tmpl;
        printf("The name of worked job is:");
        string tmps;
        cin>>tmps;
        if(chk(tmpl,tmps))
        {
            puts("distributing is successful");
            show();
        }
        else
        {
            puts("distributing is not successful");
            show();
        }
    }
}
/*
0 60 1
OS
60 40 1
Task1
132 50 1
Task2
626 174 1
Task6
438 92 1
Task5
205 15 1
Task4
160 40 1
Task3
0 0 0
100 32 0
150 10 0
200 5 0
530 96 0
220 218 0
0 0 0

*/
全部评论

相关推荐

我的offer呢😡:这不才9月吗,26到明年毕业前能一直找啊,能拿下提前批,转正的,offer打牌的都是有两把刷子的,为什么非要跟他们比。如果别人是9本硕+金牌+好几段大厂实习呢?如果别人是双非通天代呢?如果别人是速通哥呢?,做好自己就行了,我们做不到他们一样提前杀死比赛,但晚点到终点也没啥关系吧
双非应该如何逆袭?
点赞 评论 收藏
分享
emmm别问我为啥上一条帖子隔了两个月我才开始投简历和拿offer,因为我懒😰简单流程如下:周一凌晨改好的简历,然后到处乱投简历;周二接到了三维家的一面通知,临时抱佛脚的背了一些八股;周三上午一面下午通知第二天hr面;周四上午hr面下午拿offer,遂收手支线:在BOSS上顺手投了几个大厂,投字节的时候不小心投城客户端了,结果过了一天HR突然把我简历要走了,还问我能不能整客户端,我直接一口答应(脏面评警告😢)结果在周三下午的时候给我打电话,说前端有空缺实习岗,问我有没有兴趣,然后就跟我约了周四下午一面😰我都没咋准备啊,咩都不会啊😭结果周四下午面完,晚上打电话通知过一面了,赶紧把二面约在下周一下午,留点缓冲时间。逆大天了,我一半的问题都不会,他居然给我过了?运气未免有点好了😥现在正在恶补计网、网安、性能优化的东西(这三大板块我是几乎一点不会,一面几乎一点答不出来,加上我又没怎么背八股,这块被干烂了😵)心得体会与经验:1.&nbsp;我giao怎么这么快就结束了,我还以为要找好久😨2.&nbsp;大厂的面试问题真的和中厂小厂很大不同,比如在三维家我能自己吹水到vue的数据劫持、Proxy代理响应式之类的他们就觉得很不错了,但是在字节你但凡敢提到一下就会追问你细节了,一追问马脚就全漏出来了3.&nbsp;有信心真的很重要,我感觉我能拿中厂offer最重要的就是吹水吹出自信来了,以至于三维家面试反问面试官有哪里还需要改进的时候,他就说很不错了解的很多😦4.&nbsp;理解很重要,我从头到尾真没背过很多八股,不过有一些知识确实是敲过代码验证过,所以面试的时候能吹水吹得出来😇想了解面经啥的可以直接评论区问我,但我可能也说不全,因为我没有记录,而且今天摆了一天感觉记忆快清空了😵下面是故事时间:我暑假刚开始的时候才开始准备八股,印象很深那个时候连什么原型、事件循环、闭包这些名词都没听过,资料也不知道怎么找,就一直零零散散的准备,感觉也只有js稍微背了一下八股,其他很多时候都是靠完全理解和手写熟悉一些机制的,但这样做效率很低,反正准备了一个多星期半个月就开摆了😭结果一摆就摆到了开学,笔记是乱七八糟的,八股是忘光光的,简历是一直没改的,实习也是一直没投过的。直到上周日晚上偶然和师兄聊天,他突然问我“你怎么还不找实习”,那天晚上才幡然醒悟,是时候做点事情了😡然后就按照上面描述的来走了。其实我感觉我从头到尾都没背特别多八股,也没怎么找刷题资料啥的,早期就是翻尚硅谷或者黑马的入门视频从头学起,中期用面试鸭看了一点点题,主要是在学js机制和敲js代码,后期才发现了w3c的面经网站,然后在那里看着学(那个时候已经懒得敲了,因为有些问题与代码感觉不像是给找实习的看的,忒细了点😂)接下来继续准备字节二面吧,虽然几乎没啥可能可以通过,但是万一有奇迹呢?😍😍😍也祝大家能够早日拿到心仪的offer
我的offer呢😡:我已经预见10天后你会发,节孝子启动了
投递三维家等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务