发布时间:2026/8/15 13:49:56
LeetCode //C - 1195. Fizz Buzz Multithreaded 1195. Fizz Buzz MultithreadedYou have the four functions:printFizz that prints the word “fizz” to the console,printBuzz that prints the word “buzz” to the console,printFizzBuzz that prints the word “fizzbuzz” to the console, andprintNumber that prints a given integer to the console.You are given an instance of the class FizzBuzz that has four functions: fizz, buzz, fizzbuzz and number. The same instance of FizzBuzz will be passed to four different threads:Thread A: calls fizz() that should output the word “fizz”.Thread B: calls buzz() that should output the word “buzz”.Thread C: calls fizzbuzz() that should output the word “fizzbuzz”.Thread D: calls number() that should only output the integers.Modify the given class to output the series [1, 2, “fizz”, 4, “buzz”, …] where thei t h i^{th}ithtoken (1-indexed) of the series is:“fizzbuzz” if i is divisible by 3 and 5,“fizz” if i is divisible by 3 and not 5,“buzz” if i is divisible by 5 and not 3, ori if i is not divisible by 3 or 5.Implement the FizzBuzz class:FizzBuzz(int n) Initializes the object with the number n that represents the length of the sequence that should be printed.void fizz(printFizz) Calls printFizz to output “fizz”.void buzz(printBuzz) Calls printBuzz to output “buzz”.void fizzbuzz(printFizzBuzz) Calls printFizzBuzz to output “fizzbuzz”.void number(printNumber) Calls printnumber to output the numbers.Example 1:Input:n 15Output:[1,2,“fizz”,4,“buzz”,“fizz”,7,8,“fizz”,“buzz”,11,“fizz”,13,14,“fizzbuzz”]Example 2:Input:n 5Output:[1,2,“fizz”,4,“buzz”]Constraints:1 n 50From: LeetCodeLink: 1195. Fizz Buzz MultithreadedSolution:Ideas:Maintain a shared counter cur.Use four semaphores, one for each thread.Initially semNumber 1 because sequence starts at 1.After a thread prints its value, it increments cur and signals the semaphore corresponding to the next value.When cur n, wake all waiting threads so they can exit cleanly.Code:#includestdlib.h#includepthread.h#includesemaphore.htypedefstruct{intn;intcur;sem_tsemFizz;sem_tsemBuzz;sem_tsemFizzBuzz;sem_tsemNumber;pthread_mutex_tmutex;}FizzBuzz;FizzBuzz*fizzBuzzCreate(intn){FizzBuzz*obj(FizzBuzz*)malloc(sizeof(FizzBuzz));obj-nn;obj-cur1;sem_init(obj-semFizz,0,0);sem_init(obj-semBuzz,0,0);sem_init(obj-semFizzBuzz,0,0);sem_init(obj-semNumber,0,1);pthread_mutex_init(obj-mutex,NULL);returnobj;}// Dont change the following declarationsvoidprintNumber(inta);voidprintFizz();voidprintBuzz();voidprintFizzBuzz();staticvoidnextTurn(FizzBuzz*obj){if(obj-curobj-n){sem_post(obj-semFizz);sem_post(obj-semBuzz);sem_post(obj-semFizzBuzz);sem_post(obj-semNumber);return;}if(obj-cur%150)sem_post(obj-semFizzBuzz);elseif(obj-cur%30)sem_post(obj-semFizz);elseif(obj-cur%50)sem_post(obj-semBuzz);elsesem_post(obj-semNumber);}// printFizz() outputs fizz.voidfizz(FizzBuzz*obj){while(1){sem_wait(obj-semFizz);pthread_mutex_lock(obj-mutex);if(obj-curobj-n){pthread_mutex_unlock(obj-mutex);break;}printFizz();obj-cur;nextTurn(obj);pthread_mutex_unlock(obj-mutex);}}// printBuzz() outputs buzz.voidbuzz(FizzBuzz*obj){while(1){sem_wait(obj-semBuzz);pthread_mutex_lock(obj-mutex);if(obj-curobj-n){pthread_mutex_unlock(obj-mutex);break;}printBuzz();obj-cur;nextTurn(obj);pthread_mutex_unlock(obj-mutex);}}// printFizzBuzz() outputs fizzbuzz.voidfizzbuzz(FizzBuzz*obj){while(1){sem_wait(obj-semFizzBuzz);pthread_mutex_lock(obj-mutex);if(obj-curobj-n){pthread_mutex_unlock(obj-mutex);break;}printFizzBuzz();obj-cur;nextTurn(obj);pthread_mutex_unlock(obj-mutex);}}// You may call global function void printNumber(int x)// to output x, where x is an integer.voidnumber(FizzBuzz*obj){while(1){sem_wait(obj-semNumber);pthread_mutex_lock(obj-mutex);if(obj-curobj-n){pthread_mutex_unlock(obj-mutex);break;}printNumber(obj-cur);obj-cur;nextTurn(obj);pthread_mutex_unlock(obj-mutex);}}voidfizzBuzzFree(FizzBuzz*obj){sem_destroy(obj-semFizz);sem_destroy(obj-semBuzz);sem_destroy(obj-semFizzBuzz);sem_destroy(obj-semNumber);pthread_mutex_destroy(obj-mutex);free(obj);}

相关新闻

2026/8/15 13:44:56

把播放器配置看懂:3 个关键设置让 mpv 观影体验原地升级

把播放器配置看懂:3 个关键设置让 mpv 观影体验原地升级 【免费下载链接】mpv_PlayKit 🔄 mpv player 播放器折腾记录 Windows conf | 中文注释配置 汉化文档 快速帮助入门 | mpv-lazy 懒人包 Win11 x64 config | 着色器 shader 滤镜 filter 整合方案 …

2026/8/15 14:40:00

动态合批(Dynamic Batching):每帧现做的“临时打包“

🎬 开场:一个"会动却也能合批"的难题上一讲学了静态合批,小王很开心:不动的物体勾个 Static 就优化了! 但他遇到新问题: 场景里有一堆会飘动的金币 🪙、会飞的子弹 💥、飘…

2026/8/15 9:46:30

如何快速生成中国车牌图片:Python开源工具完整指南

如何快速生成中国车牌图片:Python开源工具完整指南 【免费下载链接】chinese_license_plate_generator 中国车牌生成器 项目地址: https://gitcode.com/gh_mirrors/ch/chinese_license_plate_generator 中国车牌生成器是一个基于Python的开源项目&#xff0c…

2026/8/15 7:22:41

当 LLM 遇见大文档:主流开源项目如何处理上下文超限

从 Agentic Loop 到 Repo Map,七种策略与六类陷阱引言:128K vs 10MB 的硬冲突 2026 年的 LLM 上下文窗口已达到 128K ~ 1M token(≈ 0.5MB ~ 4MB 文本),但 LLM 想要处理的真实数据规模远远超过这个量级:真实…

2026/8/15 0:04:00

AI 电动婴儿车智能功率 辅助控制、电源管理的完整选型方案

2026年随着 AI 技术在电动孕婴童用品中的深度渗透(如智能避障、自适应速度控制、能量回收),电动婴儿车对功率器件提出更高要求:高效率、小型化、低功耗、高可靠性。微碧半导体(VBsemi)基于 Trench 及 SGT 工…

2026/8/15 0:04:00

论文AIGC检测不达标完整教程!低门槛用5款工具逐步复检!

论文提交前自己先查一遍AI率,是2026年毕业生的常规动作。学校要求论文AI率低于30%,乃至于20%才能答辩… 很多同学发现一个尴尬的事情:同一篇论文,知网查出来AI率35%,维普查可能是48%,大雅、朱雀又是另外的数…

2026/8/15 9:46:39

实测才敢推 AI论文网站 2026最新测评与推荐

2026年真正好用的AI论文网站,核心看生成的论文质量、低AI味、格式正确、学术适配四大指标。综合实测,千笔AI、ThouPen、豆包、DeepSeek、Grammarly 是当前最值得推荐的梯队,覆盖从免费到付费、从中文到英文、从文科到理工的全场景需求。一、综…

2026/8/15 4:56:16

2026必备!AI论文网站测评:最新推荐与深度对比

2026年真正好用的AI论文网站,核心看生成的论文质量、低AI味、格式正确、学术适配四大指标。综合实测,千笔AI、ThouPen、豆包、DeepSeek、Grammarly 是当前最值得推荐的梯队,覆盖从免费到付费、从中文到英文、从文科到理工的全场景需求。 一、…

2026/8/15 9:46:30

摆脱论文困扰!盘点2026年全网爆红的的AI论文写作工具

一天写完毕业论文在2026年已不再是天方夜谭。2026年最炸裂、实测能大幅提速的AI论文写作工具,覆盖选题构思、文献整理、内容生成、格式排版等核心场景,真正帮你高效搞定论文难题。 一、全流程王者:一站式搞定论文全链路(一天定稿首…