-
Notifications
You must be signed in to change notification settings - Fork 0
/
fizz_buzz.cpp
49 lines (43 loc) · 894 Bytes
/
fizz_buzz.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// 412. Fizz Buzz: https://leetcode.com/problems/fizz-buzz
// Author: [email protected]
#include <stdio.h>
#include <string>
#include <vector>
using std::string;
using std::vector;
class Solution
{
public:
vector<string> fizzBuzz(int n)
{
vector<string> nums;
for (int i = 1; i <= n; i++)
{
string str;
if ((i % 3) == 0)
{
str = "Fizz";
}
if ((i % 5) == 0)
{
str += "Buzz";
}
if (str.empty())
{
str = std::to_string(i);
}
nums.push_back(str);
}
return nums;
}
};
int main(int argc, char* argv[])
{
int n = 15;
auto nums = Solution().fizzBuzz(n);
for (const auto& str: nums)
{
printf("%s\n", str.c_str());
}
return 0;
}