- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我们今天讨论了“fizz buzz”编程测试,我考虑过用 C++ 实现它,但使用元编程。理想情况下,它会在编译期间生成输出。
我当前的代码使用模板,但仍必须执行它才能生成输出。在 Ideone 上查看.
我还在用std::cout <<
为了在屏幕上打印输出。 FizzBuzz<i>::value
将给出 i
, -1
(嘶嘶声),-2
(嗡嗡声),或 -3
(嘶嘶声)。那么word<value>
为负数定义并给出 char const *
:
template <int i>
struct Print {
static void print() {
Print<i - 1>::print();
auto const value = FizzBuzz<i>::value;
if (value < 0) {
std::cout << word<value>() << std::endl;
} else {
std::cout << value << std::endl;
}
}
};
由于递归,循环没有了,有一个Print<0>
这会阻止它。
有没有办法打印出word<value>()
或 value
,哪些在编译期间在编译时已知?这可能不适用于所有编译器,因此我对适用于 GCC 和/或 Clang 的解决方案感到满意。
最佳答案
虽然通过一些模板技巧可以在编译时生成字符串,但在编译时打印它似乎并不合理。
下面是生成字符串的代码:
#include <iostream>
#include <type_traits>
#include <utility>
// Compile time string
template <char ...C> struct str_lit
{
static constexpr char value[] {C..., '\0'};
};
// Integer to str_lit
constexpr std::size_t cexpr_pow(std::size_t x, std::size_t y)
{
std::size_t ret = 1;
while (y--)
ret *= x;
return ret;
}
template <std::size_t N, std::size_t X, typename = std::make_index_sequence<X>> struct num_to_sl_impl;
template <std::size_t N, std::size_t X, std::size_t ...Seq> struct num_to_sl_impl<N, X, std::index_sequence<Seq...>>
{
static constexpr auto func()
{
if constexpr (N >= cexpr_pow(10,X))
return num_to_sl_impl<N, X+1>::func();
else
return str_lit<(N / cexpr_pow(10,X-1-Seq) % 10 + '0')...>{};
}
};
template <std::size_t N> using num_to_sl = decltype(num_to_sl_impl<N,1>::func());
// str_lit concatenation
template <typename F, typename ...P> struct sl_cat_impl {using type = typename sl_cat_impl<F, typename sl_cat_impl<P...>::type>::type;};
template <char ...C1, char ...C2> struct sl_cat_impl<str_lit<C1...>,str_lit<C2...>> {using type = str_lit<C1..., C2...>;};
template <typename F, typename ...P> using sl_cat = typename sl_cat_impl<F, P...>::type;
// The FizzBuzz
template <std::size_t N> struct fizzbuzz_impl
{
using fizz = std::conditional_t<N % 3 == 0,
str_lit<'f','i','z','z'>,
str_lit<>>;
using buzz = std::conditional_t<N % 5 == 0,
str_lit<'b','u','z','z'>,
str_lit<>>;
using type = sl_cat<typename fizzbuzz_impl<N-1>::type,
std::conditional_t<N % 3 == 0 || N % 5 == 0,
sl_cat<fizz, buzz>,
num_to_sl<N>>,
str_lit<'\n'>>;
};
template <> struct fizzbuzz_impl<0>
{
using type = str_lit<>;
};
template <std::size_t N> using fizzbuzz = typename fizzbuzz_impl<N>::type;
示例用法:
int main()
{
std::cout << fizzbuzz<15>::value;
}
输出:
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizzbuzz
关于c++ - C++ 编译时的 Fizz-Buzz,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45868182/
https://leetcode.com/problems/fizz-buzz/open in new window Total Accepted: 31093 Total Submiss
在学习了迭代器类方法和生成器之后,我测试了使用每个习语的简单 Fizz Buzz 解决方案的性能特征: >>> from timeit import timeit >>> timeit('tuple(
我正在创建一个基于 Fizz Buzz 序列的测验,用户需要猜测该系列中的最后一个元素。 示例:422、嘶嘶声、424、嗡嗡声、? 可能的答案应该是: 嘶嘶声 嗡嗡声 嘶嘶声 整数 这就是我卡住的地方
1.题目 给你一个整数 n ,找出从 1 到 n 各个整数的 Fizz Buzz 表示,并用字符串数组 answer(下标从 1 开始)返回结果,其中: answer[i] == “FizzBuzz”
为什么这会导致 1 2 Fizz 3 ?它不应该导致 1 2 Fizz 吗?!我对这个循环的输出有点困惑..?添加 else if 可以解决这个问题,我的问题是为什么? for (int i = 1;
我正在努力学习Java。有一天,我看到一个网站提供在线解决挑战的方法。这是我选择的代码项目:Fizz Buzz 这就是我的项目: import java.io.BufferedReader; impo
出于教育目的,我正在使用多线程实现经典的“嘶嘶声”问题。 “嘶嘶声”游戏是: The player designated to go first says the number "1", and ea
我的任务是用 Javascript 做 FizzBuzz 游戏。问题是我的循环在第一次迭代后停止,只返回第一个值 (4)。我可能对我的代码视而不见,但我找不到错误在哪里。如果你可以的话,请将我推向
我们今天讨论了“fizz buzz”编程测试,我考虑过用 C++ 实现它,但使用元编程。理想情况下,它会在编译期间生成输出。 我当前的代码使用模板,但仍必须执行它才能生成输出。在 Ideone 上查看
我刚刚做了 fizz buzz 测试,但我做的与下面的答案不同。我将第一个“if”语句(即 i%3===0 && i%5===0)作为最后一个“else if”语句,但出于某种原因它没有不工作。但是我
我的任务是用 Javascript 做 FizzBuzz 游戏。问题是当我运行该函数时,我得到“Answer = undefined”。我必须用逗号分隔值来打印它,但我想我可以自己弄清楚;提前致谢
剧透警告:我是一个真正的新手。负责找出嘶嘶声ruby 用于一个类,虽然我发现了多个版本的代码解决问题,我的理解是如此简陋,以至于我不能弄清楚这些例子是如何真正起作用的。 第一个问题(如果你笑出声请引用
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 4年前关闭。 Improve t
我听说这是一个常见的面试问题,请问这里有什么问题,谢谢。 for(i in 1:100){ if(i%15==0){ print('fizzbuzz') } else if
我对这段代码有些失望。我可能看不到它。我不断收到“意外 token ”或“非法”错误(考虑到我以前从未见过这样的错误,后者完全让我感到困惑。 我一直在仔细检查语法,我想这可能是我没有捕捉到的东西? f
为了清除我在 python 中的 lambda、map 和 list 的概念,我正在尝试实现 this solution仅在一行中使用 lambda 而不是将函数“rs”作为参数传递给 map 函数,
我正在尝试使用 go lang 中的 map 来实现 fizz buzz 问题。但是,此代码需要改进其工作方式。由于遍历 map 的 for 循环,它不断打印不需要的和冗余的结果。我尝试了很多解决方案
我是一名优秀的程序员,十分优秀!