作者yoco (眠月)
看板C_and_CPP
標題Re: [問題] 九九乘法表不用迴圈是叫我直接從1列到81?
時間Thu Aug 2 23:51:01 2018
被嗆我難過,回家反省,確定這個直接 template-meta 的版本沒人給過
#include <cstdio>
template <int N, int M> struct p {
static void foo() {
printf("%2d x %2d = %2d\n", N, M, N * M); p<N, M + 1>::foo();
}
};
template <int N> struct p<N, 9> {
static void foo() {
printf("%2d x %2d = %2d\n", N, 9, N * 9); p<N + 1, 1>::foo();
}
};
template <> struct p<9, 9> {
static void foo() {
printf("%2d x %2d = %2d\n", 9, 9, 9 * 9);
}
};
int main() {
p<1, 1>::foo();
}
等等來試試看 main recursive
--
To iterate is human, to recurse, divine. L. Peter Deutsch 嫩嫩迴圈 大大遞迴
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 202.39.238.191
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1533225064.A.1DB.html
推 soheadsome: 現在人家都用constexpr 08/03 00:01
→ yoco: 我... 我知道錯了..... 對不起拿十年前的技術傷大家的眼... 08/03 00:07