尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

1116. 打印零与奇偶数

1116. 打印零与奇偶数
📅 发布时间:2026/6/19 1:09:28

1116. 打印零与奇偶数

题目描述

假设有这么一个类:

class ZeroEvenOdd {
  public ZeroEvenOdd(int n) { ... }      // 构造函数public void zero(printNumber) { ... }  // 仅打印出 0public void even(printNumber) { ... }  // 仅打印出 偶数public void odd(printNumber) { ... }   // 仅打印出 奇数
}

相同的一个 ZeroEvenOdd 类实例将会传递给三个不同的线程:

  1. 线程 A 将调用 zero(),它只输出 0 。
  2. 线程 B 将调用 even(),它只输出偶数。
  3. 线程 C 将调用 odd(),它只输出奇数。

每个线程都有一个 printNumber 方法来输出一个整数。请修改给出的代码以输出整数序列 010203040506... ,其中序列的长度必须为 2n。

 

示例 1:

输入:n = 2
输出:"0102"
说明:三条线程异步执行,其中一个调用 zero(),另一个线程调用 even(),最后一个线程调用odd()。正确的输出为 "0102"。

示例 2:

输入:n = 5
输出:"0102030405"

解法

题目要求交替打印,先打印0,再打印偶数,最后打印奇数。一共打印2*n个数字。

Java

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;class ZeroEvenOdd {private int n;private int state = 0; // 0: print zero, 1: print odd, 2: print evenprivate ReentrantLock lock = new ReentrantLock();private Condition zeroCondition = lock.newCondition();private Condition evenCondition = lock.newCondition();private Condition oddCondition = lock.newCondition();public ZeroEvenOdd(int n) {this.n = n;}// printNumber.accept(x) outputs "x", where x is an integer.public void zero(IntConsumer printNumber) throws InterruptedException {for (int i = 0; i < n; i++) {lock.lock();try {while (state != 0) {zeroCondition.await();}printNumber.accept(0);if ((i + 1) % 2 == 1) {state = 1; // next print oddoddCondition.signal();} else {state = 2; // next print evenevenCondition.signal();}} finally {lock.unlock();}}}public void even(IntConsumer printNumber) throws InterruptedException {for (int i = 2; i <= n; i += 2) {lock.lock();try {while (state != 2) {evenCondition.await();}printNumber.accept(i);state = 0; // next print zerozeroCondition.signal();} finally {lock.unlock();}}}public void odd(IntConsumer printNumber) throws InterruptedException {for (int i = 1; i <= n; i += 2) {lock.lock();try {while (state != 1) {oddCondition.await();}printNumber.accept(i);state = 0; // next print zerozeroCondition.signal();} finally {lock.unlock();}}}
}

Java

class ZeroEvenOdd {private int n;private int state = 0; // 0: print zero, 1: print odd, 2: print evenpublic ZeroEvenOdd(int n) {this.n = n;}// printNumber.accept(x) outputs "x", where x is an integer.public void zero(IntConsumer printNumber) throws InterruptedException {for (int i = 0; i < n; i++) {synchronized (this) {while (state != 0) {this.wait();}printNumber.accept(0);if ((i + 1) % 2 == 1) {state = 1; // next print odd} else {state = 2; // next print even}this.notifyAll();}}}public void even(IntConsumer printNumber) throws InterruptedException {for (int i = 2; i <= n; i += 2) {synchronized (this) {while (state != 2) {this.wait();}printNumber.accept(i);state = 0; // next print zerothis.notifyAll();}}}public void odd(IntConsumer printNumber) throws InterruptedException {for (int i = 1; i <= n; i += 2) {synchronized (this) {while (state != 1) {this.wait();}printNumber.accept(i);state = 0; // next print zerothis.notifyAll();}}}
}

...


加油啦!加油鸭,冲鸭!!!

相关新闻

  • 2025 年装配式营地,装配式营地建设,装配式民宿,装配式出口厂家最新推荐,产能、专利、环保三维数据透视
  • Windows Server 2022 OVF (2025 年 10 月更新) - VMware 虚拟机模板
  • Linux运行时常用命令

最新新闻

  • ATM网络OAM机制深度解析:从AIS/RDI信元到硬件性能监控实战
  • 深入解析NXP MCU Bootloader与blhost工具:从原理到高级应用实践
  • 医疗AI落地两大硬坎:临床信任断裂与数据合规失焦
  • Adaboost原理深度解析:理解梯度提升家族的基石
  • 股市语言密码:看懂全球资本流动的翻译之道
  • 终极指南:如何为300+车型部署开源驾驶辅助系统openpilot

日新闻

  • 5分钟掌握Python进化算法:Geatpy高性能优化工具完全指南
  • Microchip 24AA044 EEPROM选型与应用全指南:从参数解析到实战编程
  • 华为的鸿蒙到底有多牛?为什么称作遥遥领先?

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号