内存读写死锁

内存条

http://www.nowcoder.com/questionTerminal/3441be0bbf0a4dfd8527198a08a71fb6

分R==W、R<W、R>W三种情况

import java.util.*;
public class Main {
    public static void main(String[] args) throws Exception{
        Scanner sc = new Scanner(System.in);
        int L = sc.nextInt(), R = sc.nextInt(), W = sc.nextInt();
        if(R > L || W > L){    //读写超过内存总量的,直接死锁
            System.out.println("DEADLOCK");
            return;
        }
        if(R == W) System.out.println("OK");    //读写等大的,交替即可OK
        if(R < W){
            int sum = 0;
            while(L - sum >= W) sum += W - R;    //写大于读的,交替增量直到不能再写
            if(sum < R) System.out.println("DEADLOCK");    //增量总量不够读一次,死锁
            else System.out.println("OK");
        }
        if(R > W){
            int sum = 0;
            while(sum <= L) sum += W;    //读大于写的,先尽量写满
            sum -= W;
            if(sum < R) System.out.println("DEADLOCK");    //不够读一次,死锁
            else System.out.println("OK");
        }
    }
}
全部评论

相关推荐

05-19 19:57
蚌埠学院 Python
2237:Gpa70不算高,建议只写排名,个人技能不在多而在精,缩到8条以内。项目留一个含金量高的,减少间距弄到一页,硕士简历也就一页,本科不要写很多
实习,投递多份简历没人回...
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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