変形Fizz-Buzz問題
Posted feedbacks - C++
C++ なら else どころか条件分岐もループも消せる。
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 | #include <cstdio>
template<bool mod3, bool mod5>
const char* message()
{
return "hoge";
}
template<>
const char* message<true, false>()
{
return "Fizz";
}
template<>
const char* message<false, true>()
{
return "Buzz";
}
template<>
const char* message<true, true>()
{
return "FizzBuzz";
}
template<int n>
void fizzbuzz()
{
fizzbuzz<n - 1>();
printf("%2d:%s\n", n, message<n % 3 == 0, n % 5 == 0>());
}
template<>
void fizzbuzz<0>()
{
}
int main()
{
fizzbuzz<20>();
}
|
1 2 3 4 5 6 7 8 9 10 11 | int main(int, char* [])
{
static const char* const ss[] = { "Hoge", "Fizz", "Buzz", "FizzBuzz" };
for(int i = 1; i <= 20; ++i)
{
std::cout << std::setw(2) << i << ':' << ss[(i%5==0?2:0)+(i%3==0?1:0)] << std::endl;
}
return 0;
}
|


raynstard
#3758()
Rating0/2=0.00
[ reply ]