1443. Printer Queue

 作业……数组模拟


#include <iostream>

using namespace std;

int queue[101], n;
bool printed[101];

bool priIsFirst(int head) {
	for(int i = 0; i < n; i++) {
		if(printed[i]) {
			continue;
		}
		if(queue[i] > queue[head]) {
			return false;
		}
	}
	return true;
}

int main() {
	int cases;
	cin >> cases;
	while(cases--) {
		int mine, count = 0;
		cin >> n >> mine;
		
		int head = 0;
		for(int i = 0; i < n; i++) {
			cin >> queue[i];
			printed[i] = false;
		}
		
		while(head < n) {
			if(printed[head]) {
				head++;
				head %= n;
				continue;
			}
			if(priIsFirst(head)) {
				printed[head] = true;
				count++;
				if(head == mine) {
					cout << count << endl;
					break;
				}
				head++;
				head %= n;
			}
			else {
				head++;
				head %= n;
			}
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Here is the Printer class implementation that meets your requirements: ``` import java.util.Queue; import java.util.concurrent.ArrayBlockingQueue; public class Printer { private static Printer instance; private final Queue<String> printQueue; private boolean stateIsRunning; private Printer() { printQueue = new ArrayBlockingQueue<>(5); stateIsRunning = true; } public static synchronized Printer getInstance() { if (instance == null) { instance = new Printer(); } return instance; } public synchronized void addJob(String jobName) throws FullQueueException { if (printQueue.offer(jobName)) { System.out.println("Job " + jobName + " has been added to the print queue."); } else { throw new FullQueueException("Print queue is full. Cannot add job " + jobName); } } private synchronized String getJob() throws EmptyQueueException { String jobName = printQueue.poll(); if (jobName == null) { throw new EmptyQueueException("Print queue is empty."); } return jobName; } public synchronized void halt() { stateIsRunning = false; } public void run() { while (stateIsRunning) { try { String jobName = getJob(); System.out.println("Starting job " + jobName); int pageCount = 10; // Assuming each job has 10 pages for (int i = 1; i <= pageCount; i++) { Thread.sleep(500); // Sleep for 500ms per page } System.out.println("Job " + jobName + " has been completed."); } catch (EmptyQueueException e) { System.out.println("Printer is waiting for a job."); } catch (InterruptedException e) { System.err.println("InterruptedException occurred while processing print job: " + e.getMessage()); } } } } ``` In this implementation, we have a `printQueue` attribute which is an `ArrayBlockingQueue` of size 5. The `addJob` method adds a job to the queue and throws a `FullQueueException` if the queue is already full. The `getJob` method retrieves a job from the queue and throws an `EmptyQueueException` if the queue is empty. The `run` method continuously loops until the printer is halted (`stateIsRunning` is set to false). It retrieves a job from the queue, processes it by sleeping for 500ms per page, and then prints out that the job has been completed. If there are no jobs available, it prints out that the printer is waiting for a job. Note that we have implemented the `Printer` class as a Singleton by adding a private constructor and a `getInstance` method. This ensures that there is only one instance of `Printer` class throughout the application.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值