首页 > 试题广场 >

内存条

[编程题]内存条
  • 热度指数:1065 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

在很多存储模型中,内存池是使用循环队列来实现的。一个内存池可以看成一个环型的内存空间。现在有一个大小为L字节的内存池,用户会对它进行读和写两种操作。每次写会从当前存储位置的末尾向后空余的位置写入W比特的数据;每次读会从当前存储位置的开头读取R比特的数据,然后将这些位置的数据释放。所以任意时刻在内存中存储有数据的位置都是连续的一段。

读和写操作是会被堵塞的,比如当剩余的内存空间不足W比特时,写操作就会被堵塞,等待读操作进行直到剩余空间不小于W比特;读操作也是类似。但是存在一种情况,系统会发生死锁,即当前剩余空间不足W比特写操作无法进行;但是当前存储的数据也不足R比特,读操作也无法进行。这时系统就陷入了死锁。现在你需要帮助判断一下某个系统是否有可能发生死锁,初始时内存池是空的。


输入描述:
第一行包含三个整数L,R,W。1≤L,R,W≤1018,R,W≤L


输出描述:
若会发生死锁,输出DEADLOCK;否则输出OK。
示例1

输入

5 3 4

输出

DEADLOCK
示例2

输入

5 2 3

输出

OK
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int L,R,W;
    cin>>L>>R>>W;
    if(R+W>L)
        cout<<"DEADLOCK"<<endl;
    else
        cout<<"OK"<<endl;
    return 0;
}

发表于 2019-06-27 15:58:59 回复(1)
#include<bits/stdc++.h>
using namespace std;
int main(){
    int l,r,w;
    cin>>l>>r>>w;
    i***t;=r+w) {cout<<"OK";return 0;}
    int rem=l;
    for(int i=0;i<l+1;i++){
        while(rem>=w) rem -= w;
        while(r<=l-rem) rem += r;
        if(rem<w&&l-rem<r) {cout<<"DEADLOCK";return 0;}
    }
    cout<<"OK";
    return 0;
}
//测试样例太少了,6 3 4 /7 3 5 是不会死锁的。只判断l和r+w大小是不对的
//l>=r+w一定不死锁,反之可能死锁,!!不是一定
//用模拟思路,允许情况下连续写连续读,内存剩余最多只能是0-l这l+1个可能(后面就是一个循环了),所以在l前+1次模拟内没有死锁情况出现就不可能出现死锁了。
发表于 2019-05-21 11:12:37 回复(2)
#include <bits/stdc++.h>

using namespace std;

int main()
{     int l,r,w;     while(cin>>l>>r>>w)      {         if(r+w>l)             cout<<"DEADLOCK"<<endl;         else             cout<<"OK"<<endl;     }     return 0;
}

发表于 2019-01-19 02:48:19 回复(1)
// 模拟的方式。像是一个有限状态机
// tip1: 写读写读随机交替 和 先写到不能写再读到不能读 最终结果是一样的
// tip2: 如果没有死锁,那么每进行一次“先写到不能写再读到不能读”的剩余空闲空间一定是一个周期,注意L不一定在周期中
// tip3: 由于空间最大为L,因此剩余空闲空间的周期不会大于L
// tip4: 如果每次操作剩余空间l小于W,并且每次残余rem小于R 那么就是死锁
import java.io.*;
public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] tmp = br.readLine().split(" ");
        br.close();
        int L = Integer.parseInt(tmp[0]), R = Integer.parseInt(tmp[1]), W = Integer.parseInt(tmp[2]);
        int l = L, rem = 0, loop = L;
        while(loop-- >0){ // 最多L次
            int maxW = l/W*W; // 最大可写
            int maxR = (rem+maxW)/R*R; // 最大可读
            rem = rem + maxW - maxR; // 残余
            l = L-rem; // 空闲
            if(l < W && rem < R) {System.out.println("DEADLOCK"); break;}
            if(rem == 0) {System.out.println("OK"); break;} // 起始状态也是0,但0不一定是在周期中
        }
        if(loop == 0) System.out.println("OK");
    }
}

编辑于 2019-03-27 21:53:19 回复(0)
为什么都写的是r+w>l就死锁,不应该是r+w>l+1吗?比如 6,3,4是不会死锁的,可以自己一个一个试一试,真的不会死锁啊。。。可能测试用例没有r+w和l+1相等的情况吧。。。
发表于 2019-04-30 17:32:24 回复(2)
#include<iostream>
using namespace std;
int main()
{
    int L,R,W;
    cin>>L>>R>>W;
    if (R>L-W)
        cout<<"DEADLOCK"<<endl;
    else
        cout<<"OK"<<endl;
    return 0;
}
如果笔试都是这么简单的就好了。。
发表于 2019-09-19 11:57:19 回复(0)
我来添加一个回答:
牛客原题,看那篇的解答就行。辗转相除法哈!
def method(l,r,w):
    total = r+w
    d = w
    while True:
        if d==0:
            d = R
            break
        r = r%d
        if r==0:
            break
        d = d%r
    return 'DEADLOCK' if l<total-d else 'OK'
if __name__=='__main__':
    l,r,w = list(map(int,input().split()))
    print(method(l,r,w))


发表于 2019-09-03 09:41:15 回复(0)
#include <stdio.h>

int l,r,w;

int main()
{
	scanf("%d%d%d",&l,&r,&w);
	if(l<r+w)
		puts("DEADLOCK");
	else
		puts("OK");
	return 0;
}

发表于 2019-08-20 09:43:07 回复(0)
while True:
    try:
        l,r,w=map(int,input().split(' '))
        #print(l,r,w)
        if l<r or l-r<w:
            print('DEADLOCK')
        else:
            print('OK')
    except:
        break
发表于 2019-07-24 09:13:47 回复(0)
所以这题是L比特不是L字节吧???我寻思5 字节 写4bits,读3bits 怎么出来的死锁??
发表于 2019-06-21 17:41:37 回复(0)
#include<iostream>
using namespace std;
int main()
{
    int L,R,W;
    cin>>L>>R>>W;
    if (L-R-W<0 )
        cout<<"DEADLOCK";
    else
        cout<<"OK";
}
发表于 2019-06-12 19:17:09 回复(0)
import java.util.*;
public class Main{
    public static void main(String args[]){
        Scanner scan = new Scanner(System.in);
        int l = scan.nextInt();
        int r = scan.nextInt();
        int w = scan.nextInt();
        int space = l;
        boolean OK = true;
        for(int i=0;i<l;++i){
            while(space>=w)space-=w;
            while(l-space>=r)space+=r;
            if(space<w&&l-space<r){
                OK = false;
                break;
            }
        }
        if(OK)
            System.out.print("OK");
        else
            System.out.print("DEADLOCK");
    }
}

发表于 2019-06-06 11:13:27 回复(0)
#include<stdio.h>
int main()
{
    int L = 0, R = 0, W = 0;
    scanf("%d%d%d",&L,&R,&W);
    if((R+W)>L)
    {
        printf("DEADLOCK\n");
    }
    else
    {
        printf("OK\n");
    }
    return 0;
}


发表于 2019-04-27 17:54:59 回复(0)
#include<iostream>
using namespace std;
int main ()
{
    int l,r,w;
    cin>>l>>r>>w;
    cout<<((r+w)>l?"DEADLOCK":"OK");

    return 0;
}

发表于 2019-04-03 15:43:11 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
       Scanner sc = new Scanner(System.in);
       int L = sc.nextInt(); 
       int W = sc.nextInt();
       int R = sc.nextInt();
       if(W+R>L){
          System.out.println("DEADLOCK"); 
       }else{
          System.out.println("OK");
       }
    }
}

发表于 2019-03-28 10:36:51 回复(0)
l,r,w = map(int,input().split())
if r+w>l:
    print('DEADLOCK')
else:
    print('OK')
阅读理解
发表于 2019-01-24 16:04:59 回复(0)