1195.fizz-buzz-multithreaded
Statement
Metadata
- Link: 交替打印字符串
- Difficulty: Medium
- Tag:
多线程
编写一个可以从 1 到 n 输出代表这个数字的字符串的程序,但是:
- 如果这个数字可以被 3 整除,输出 "fizz"。
- 如果这个数字可以被 5 整除,输出 "buzz"。
- 如果这个数字可以同时被 3 和 5 整除,输出 "fizzbuzz"。
例如,当 n = 15
,输出: 1, 2, fizz, 4, buzz, fizz, 7, 8, fizz, buzz, 11, fizz, 13, 14, fizzbuzz
。
假设有这么一个类:
class FizzBuzz {
public FizzBuzz(int n) { … } // constructor
public void fizz(printFizz) { … } // only output "fizz"
public void buzz(printBuzz) { … } // only output "buzz"
public void fizzbuzz(printFizzBuzz) { … } // only output "fizzbuzz"
public void number(printNumber) { … } // only output the numbers
}
请你实现一个有四个线程的多线程版 FizzBuzz
, 同一个 FizzBuzz
实例会被如下四个线程使用:
- 线程A将调用
fizz()
来判断是否能被 3 整除,如果可以,则输出fizz
。 - 线程B将调用
buzz()
来判断是否能被 5 整除,如果可以,则输出buzz
。 - 线程C将调用
fizzbuzz()
来判断是否同时能被 3 和 5 整除,如果可以,则输出fizzbuzz
。 - 线程D将调用
number()
来实现输出既不能被 3 整除也不能被 5 整除的数字。
提示:
- 本题已经提供了打印字符串的相关方法,如
printFizz()
等,具体方法名请参考答题模板中的注释部分。
Metadata
- Link: Fizz Buzz Multithreaded
- Difficulty: Medium
- Tag:
Concurrency
You 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 the ith
token (1-indexed) of the series is:
"fizzbuzz"
ifi
is divisible by3
and5
,"fizz"
ifi
is divisible by3
and not5
,"buzz"
ifi
is divisible by5
and not3
, ori
ifi
is not divisible by3
or5
.
Implement the FizzBuzz
class:
FizzBuzz(int n)
Initializes the object with the numbern
that represents the length of the sequence that should be printed.void fizz(printFizz)
CallsprintFizz
to output"fizz"
.void buzz(printBuzz)
CallsprintBuzz
to output"buzz"
.void fizzbuzz(printFizzBuzz)
CallsprintFizzBuzz
to output"fizzbuzz"
.void number(printNumber)
Callsprintnumber
to output the numbers.
Example 1:
Input: n = 15
Output: [1,2,"fizz",4,"buzz","fizz",7,8,"fizz","buzz",11,"fizz",13,14,"fizzbuzz"]
Example 2:
Input: n = 5
Output: [1,2,"fizz",4,"buzz"]
Constraints:
1 <= n <= 50
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
// head
class FizzBuzz {
private:
int n;
public:
FizzBuzz(int n) {
this->n = n;
}
// printFizz() outputs "fizz".
void fizz(function<void()> printFizz) {
while (order_ <= n) {
if (order_ % 3 != 0 || order_ % 15 == 0) {
std::this_thread::yield();
continue;
} else {
printFizz();
order_++;
}
}
}
// printBuzz() outputs "buzz".
void buzz(function<void()> printBuzz) {
while (order_ <= n) {
if (order_ % 5 != 0 || order_ % 15 == 0) {
std::this_thread::yield();
continue;
} else {
printBuzz();
order_++;
}
}
}
// printFizzBuzz() outputs "fizzbuzz".
void fizzbuzz(function<void()> printFizzBuzz) {
while (order_ <= n) {
if (order_ % 15 != 0) {
std::this_thread::yield();
continue;
} else {
printFizzBuzz();
order_++;
}
}
}
// printNumber(x) outputs "x", where x is an integer.
void number(function<void(int)> printNumber) {
for (int i = 1; i <= n; i++) {
if (i % 3 == 0 || i % 5 == 0) {
continue;
}
while (order_ % 3 == 0 || order_ % 5 == 0) {
std::this_thread::yield();
}
printNumber(i);
order_++;
}
}
private:
int order_{1};
};
#ifdef LOCAL
int main() {
return 0;
}
#endif
最后更新: October 11, 2023