Leetcode 1116. 打印零与奇偶数

90 篇文章 0 订阅
13 篇文章 0 订阅

 控制台没问题,不知道为啥没ac  模版里面基数偶数写反了

class ZeroEvenOdd {
private:
    int n;
    mutex mu;
    condition_variable cond;
    
    bool is0,is1,is2;
public:
    ZeroEvenOdd(int n) {
        this->n = n;
        is0=true;
        is1=false;
        is2=false;
    }

    // printNumber(x) outputs "x", where x is an integer.
    void zero(function<void(int)> printNumber) {
        for(int i=0;i<n;i++)
        {
            unique_lock<mutex> lk(mu);
            cond.wait(lk,[this](){return is0;});
            printNumber(0);
            if(i%2) is2=true;
            else is1=true;
            is0=false;
            cond.notify_all();
            
        }
    }

    void odd(function<void(int)> printNumber) {
        int num;
        if(n%2) num=n/2+1;
        else num=n/2;
        for(int i=0;i<num;i++)
        {
            unique_lock<mutex> lk(mu);
            cond.wait(lk,[this](){return is1;});
            printNumber(i*2+1);
            
            is1=false,is0=true;
            cond.notify_all();
        }
    }

    void even(function<void(int)> printNumber) {
        int num=n/2;
        
        for(int i=1;i<=num;i++)
        {
            unique_lock<mutex> lk(mu);
            cond.wait(lk,[this](){return is2;});
            printNumber(i*2);
            
            is2=false,is0=true;
            cond.notify_all();
        }
    }
};
#include <iostream>
#include <mutex>
#include <condition_variable>
#include <thread>
#include <functional>
using namespace std;
class ZeroEvenOdd {
private:
	int n;
	std::mutex mutex_t;
	std::condition_variable cond;
	bool is0 = true;
	bool is1 = true;
public:
	ZeroEvenOdd(int n) {
		this->n = n;
	}

	// printNumber(x) outputs "x", where x is an integer.
	void zero(function<void(int)> printNumber) {
		for (int i = 0; i < n; i++)
		{
			std::unique_lock<std::mutex> lk(mutex_t);
			cond.wait(lk, [this]() {return is0; });
			printNumber(0);
			is0 = false;
			cond.notify_all();
		}
	}

	void even(function<void(int)> printNumber) {
		if (n == 0)return;
		for (int i = 1; i <= n; i++)
		{
			std::unique_lock<std::mutex> lk(mutex_t);
			cond.wait(lk, [this]() {return !is0&&is1; });
			if (i % 2) 
			{
				printNumber(i);
				is0 = true;
				is1 = false;
			}
			
			cond.notify_all();
		}
	}

	void odd(function<void(int)> printNumber) {
		if (n == 0)return;
		for (int i = 1; i <= n; i++)
		{
			std::unique_lock<std::mutex> lk(mutex_t);
			cond.wait(lk, [this]() {return !is0 && !is1; });
			if (i % 2 == 0)
			{
				printNumber(i);
				is0 = true;
				is1 = true;
			}
			
			cond.notify_all();
		}

	}
};
int main() 
{
	std::function<void(int) > f1= [](int x) {printf("%d", x); };
	ZeroEvenOdd obj(5);
	thread t1(std::bind(&ZeroEvenOdd::zero,&obj,f1));
	thread t2(std::bind(&ZeroEvenOdd::even, &obj,f1));
	thread t3(std::bind(&ZeroEvenOdd::odd, &obj,f1));
	t1.join();
	t2.join();
	t3.join();
	return 0;
}

假设有这么一个类:

class ZeroEvenOdd {
  public ZeroEvenOdd(int n) { ... }      // 构造函数
  public void zero(printNumber) { ... }  // 仅打印出 0
  public void even(printNumber) { ... }  // 仅打印出 偶数
  public void odd(printNumber) { ... }   // 仅打印出 奇数
}
相同的一个 ZeroEvenOdd 类实例将会传递给三个不同的线程:

线程 A 将调用 zero(),它只输出 0 。
线程 B 将调用 even(),它只输出偶数。
线程 C 将调用 odd(),它只输出奇数。
每个线程都有一个 printNumber 方法来输出一个整数。请修改给出的代码以输出整数序列 010203040506... ,其中序列的长度必须为 2n。

 

示例 1:

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

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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/print-zero-even-odd
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值