Re: [問題] 九九乘法表不用迴圈是叫我直接從1列到81?

看板C_and_CPP作者 (のヮの)時間5年前 (2018/07/16 22:48), 編輯推噓2(203)
留言5則, 5人參與, 5年前最新討論串4/29 (看更多)
※ 引述《red0whale (red whale)》之銘言: : 剛才做題目, : https://i.imgur.com/NI4TYj5.jpg
: 九九乘法表用兩個或一個迴圈來做我都會 : 但不用迴圈叫我列九九乘法表是哪招? : 難道是要我直接從1*1列到9*9嗎? : 還是其實有妙招? : 說實在我真想不到不用迴圈就能簡單列出九九乘法表的方法了 我覺得這題要求用 C 真的比較困難,C++ 的話可以輕鬆很多 大概像這樣 https://ideone.com/Yb0TqG #include <iomanip> #include <iostream> template<int...> struct IntegerSequence { }; template<int n, class = IntegerSequence<>, bool = n == 0> struct MakeIntegerSequenceImpl; template<int n, int... i> struct MakeIntegerSequenceImpl<n, IntegerSequence<i...>, true> { typedef IntegerSequence<i...> type; }; template<int n, int... i> struct MakeIntegerSequenceImpl<n, IntegerSequence<i...>, false> { typedef typename MakeIntegerSequenceImpl< n - 1, IntegerSequence<n - 1, i...>>::type type; }; template<int n> using MakeIntegerSequence = typename MakeIntegerSequenceImpl<n>::type; template<int n> struct MultiplicationTableCell { static constexpr int value = ((n / 9) + 1) * ((n % 9) + 1); }; template<class> struct MultiplicationTableImpl; template<int... n> struct MultiplicationTableImpl<IntegerSequence<n...>> { static const int value[]; }; template<int... n> const int MultiplicationTableImpl<IntegerSequence<n...>>::value[] = { MultiplicationTableCell<n>::value... }; struct MultiplicationTable : MultiplicationTableImpl<MakeIntegerSequence<81>> { static void print_twoLoops() { for (int i = 0; i < 9; ++i) { for (int j = 0; j < 9; ++j) { std::cout << std::setw(2) << value[(i * 9) + j] << ','; } std::cout << std::endl; } } static void print_oneLoop() { for (int i = 0; i < 81; ++i) { std::cout << std::setw(2) << value[i] << ','; if ((i % 9) == 8) { std::cout << std::endl; } } } static void print_noLoop_twoArgs(int i = 0, int j = 0) { if (i == 9) { return; } if (j == 9) { std::cout << std::endl; return print_noLoop_twoArgs(i + 1, 0); } std::cout << std::setw(2) << value[(i * 9) + j] << ','; print_noLoop_twoArgs(i, j + 1); } static void print_noLoop_oneArg(int i = 0) { if (i == 81) { return; } std::cout << std::setw(2) << value[i] << ','; if ((i % 9) == 8) { std::cout << std::endl; } print_noLoop_oneArg(i + 1); } }; int main() { std::cout << "Use 'two' for loops:" << std::endl; MultiplicationTable::print_twoLoops(); std::cout << std::endl; std::cout << "Use 'only one' for loop:" << std::endl; MultiplicationTable::print_oneLoop(); std::cout << std::endl; std::cout << "Use 'no loops' (two arguments):" << std::endl; MultiplicationTable::print_noLoop_twoArgs(); std::cout << std::endl; std::cout << "Use 'no loops' (one argument):" << std::endl; MultiplicationTable::print_noLoop_oneArg(); return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.43.180.55 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1531752496.A.59E.html

07/16 23:57, 5年前 , 1F
有嗎XDD
07/16 23:57, 1F

07/17 00:02, 5年前 , 2F
終於連TMP都出現了XD
07/17 00:02, 2F

07/17 13:05, 5年前 , 3F
有想過www
07/17 13:05, 3F

07/17 13:18, 5年前 , 4F
看有沒有人要用preprocessor寫,我確定可以
07/17 13:18, 4F

07/17 13:25, 5年前 , 5F
2樓有寫macro版本
07/17 13:25, 5F
文章代碼(AID): #1RJB0mMU (C_and_CPP)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 4 之 29 篇):
文章代碼(AID): #1RJB0mMU (C_and_CPP)