# 1116.print-zero-even-odd

## Statement

• 例如，调用 `printNumber(7)` 将会输出 `7` 到控制台。

• 线程 A：调用 `zero()` ，只输出 `0`
• 线程 B：调用 `even()` ，只输出偶数
• 线程 C：调用 `odd()` ，只输出奇数

• `ZeroEvenOdd(int n)` 用数字 `n` 初始化对象，表示需要输出的数。
• `void zero(printNumber)` 调用 `printNumber` 以输出一个 0 。
• `void even(printNumber)` 调用`printNumber` 以输出偶数。
• `void odd(printNumber)` 调用 `printNumber` 以输出奇数。

``````输入：n = 2

``````

``````输入：n = 5

``````

• `1 <= n <= 1000`

You have a function `printNumber` that can be called with an integer parameter and prints it to the console.

• For example, calling `printNumber(7)` prints `7` to the console.

You are given an instance of the class `ZeroEvenOdd` that has three functions: `zero`, `even`, and `odd`. The same instance of `ZeroEvenOdd` will be passed to three different threads:

• Thread A: calls `zero()` that should only output `0`'s.
• Thread B: calls `even()` that should only output even numbers.
• Thread C: calls `odd()` that should only output odd numbers.

Modify the given class to output the series `"010203040506…"` where the length of the series must be `2n`.

Implement the `ZeroEvenOdd` class:

• `ZeroEvenOdd(int n)` Initializes the object with the number `n` that represents the numbers that should be printed.
• `void zero(printNumber)` Calls `printNumber` to output one zero.
• `void even(printNumber)` Calls `printNumber` to output one even number.
• `void odd(printNumber)` Calls `printNumber` to output one odd number.

Example 1:

``````Input: n = 2
Output: "0102"
Explanation: There are three threads being fired asynchronously.
One of them calls zero(), the other calls even(), and the last one calls odd().
"0102" is the correct output.
``````

Example 2:

``````Input: n = 5
Output: "0102030405"
``````

Constraints:

• `1 <= n <= 1000`

## Solution

``````#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define endl "\n"
#define fi first
#define se second
#define all(x) begin(x), end(x)
#define rall rbegin(a), rend(a)
#define bitcnt(x) (__builtin_popcountll(x))
#define complete_unique(a) a.erase(unique(begin(a), end(a)), end(a))
#define mst(x, a) memset(x, a, sizeof(x))
#define MP make_pair

using ll = long long;
using ull = unsigned long long;
using db = double;
using ld = long double;
using VLL = std::vector<ll>;
using VI = std::vector<int>;
using PII = std::pair<int, int>;
using PLL = std::pair<ll, ll>;

using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template <typename T, typename S>
inline bool chmax(T &a, const S &b) {
return a < b ? a = b, 1 : 0;
}

template <typename T, typename S>
inline bool chmin(T &a, const S &b) {
return a > b ? a = b, 1 : 0;
}

#ifdef LOCAL
#include <debug.hpp>
#else
#define dbg(...)
#endif

class ZeroEvenOdd {
private:
int n;

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++) {
while (order_ % 2 != 0) {
}

printNumber(0);

order_++;
}
}

void even(function<void(int)> printNumber) {
for (int i = 2; i <= n; i += 2) {
while (order_ % 4 != 3) {
}

printNumber(i);

order_++;
}
}

void odd(function<void(int)> printNumber) {
for (int i = 1; i <= n; i += 2) {
while (order_ % 4 != 1) {
}

printNumber(i);

order_++;
}
}

private:
int order_{0};
};

#ifdef LOCAL

int main() {
return 0;
}

#endif
``````