-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfizzbuzz-cpp.cpp
56 lines (51 loc) · 1.16 KB
/
fizzbuzz-cpp.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
50
51
52
53
54
55
56
#include <iostream>
void fizzbuzz1() {
const int fizz = 3;
const int buzz = 5;
int f = fizz - 1;
int b = buzz - 1;
for (int times = 1; times <= 100; times++) {
if(f == 0) {
std::cout << "Fizz";
f = fizz;
}
if(b == 0) {
std::cout << "Buzz";
b = buzz;
} else if (f != fizz) {
std::cout << times;
}
f--;
b--;
std::cout << " ";
}
}
int mod15(uint64_t number) {
while (number > 0xf) {
number = (number >> 4) + (number & 0xf);
}
if (number == 15) number = 0;
return number;
}
void fizzbuzz2() {
for (int times = 1; times <= 100; times++) {
int rmd = mod15(times);
if (rmd == 0) {
std::cout << "FizzBuzz";
} else if (rmd % 3 == 0) {
std::cout << "Fizz";
} else if (rmd % 5 == 0) {
std::cout << "Buzz";
} else {
std::cout << times;
}
std::cout << " ";
}
}
int main(int argc, const char * argv[]) {
fizzbuzz1();
std::cout << std::endl;
fizzbuzz2();
std::cout << std::endl;
return 0;
}