fork download
  1. #include <iomanip>
  2. #include <iostream>
  3.  
  4. template<int...> struct IntegerSequence {
  5. };
  6.  
  7. template<int n, class = IntegerSequence<>, bool = n == 0> struct MakeIntegerSequenceImpl;
  8.  
  9. template<int n, int... i>
  10. struct MakeIntegerSequenceImpl<n, IntegerSequence<i...>, true> {
  11. typedef IntegerSequence<i...> type;
  12. };
  13.  
  14. template<int n, int... i>
  15. struct MakeIntegerSequenceImpl<n, IntegerSequence<i...>, false> {
  16. typedef typename MakeIntegerSequenceImpl<n - 1, IntegerSequence<n - 1, i...>>::type type;
  17. };
  18.  
  19. template<int n>
  20. using MakeIntegerSequence = typename MakeIntegerSequenceImpl<n>::type;
  21.  
  22. template<int n>
  23. struct MultiplicationTableCell {
  24. static constexpr int value = ((n / 9) + 1) * ((n % 9) + 1);
  25. };
  26.  
  27. template<class> struct MultiplicationTableImpl;
  28.  
  29. template<int... n>
  30. struct MultiplicationTableImpl<IntegerSequence<n...>> {
  31. static const int value[];
  32. };
  33.  
  34. template<int... n>
  35. const int MultiplicationTableImpl<IntegerSequence<n...>>::value[] = {
  36. MultiplicationTableCell<n>::value...
  37. };
  38.  
  39. struct MultiplicationTable : MultiplicationTableImpl<MakeIntegerSequence<81>> {
  40. static void print_twoLoops() {
  41. for (int i = 0; i < 9; ++i) {
  42. for (int j = 0; j < 9; ++j) {
  43. std::cout << std::setw(2) << value[(i * 9) + j] << ',';
  44. }
  45. std::cout << std::endl;
  46. }
  47. }
  48.  
  49. static void print_oneLoop() {
  50. for (int i = 0; i < 81; ++i) {
  51. std::cout << std::setw(2) << value[i] << ',';
  52. if ((i % 9) == 8) {
  53. std::cout << std::endl;
  54. }
  55. }
  56. }
  57.  
  58. static void print_noLoop_twoArgs(int i = 0, int j = 0) {
  59. if (i == 9) {
  60. return;
  61. }
  62. if (j == 9) {
  63. std::cout << std::endl;
  64. return print_noLoop_twoArgs(i + 1, 0);
  65. }
  66. std::cout << std::setw(2) << value[(i * 9) + j] << ',';
  67. print_noLoop_twoArgs(i, j + 1);
  68. }
  69.  
  70. static void print_noLoop_oneArg(int i = 0) {
  71. if (i == 81) {
  72. return;
  73. }
  74. std::cout << std::setw(2) << value[i] << ',';
  75. if ((i % 9) == 8) {
  76. std::cout << std::endl;
  77. }
  78. print_noLoop_oneArg(i + 1);
  79. }
  80. };
  81.  
  82. int main() {
  83. std::cout << "Use 'two' for loops:" << std::endl;
  84. MultiplicationTable::print_twoLoops();
  85. std::cout << std::endl;
  86. std::cout << "Use 'only one' for loop:" << std::endl;
  87. MultiplicationTable::print_oneLoop();
  88. std::cout << std::endl;
  89. std::cout << "Use 'no loops' (two arguments):" << std::endl;
  90. MultiplicationTable::print_noLoop_twoArgs();
  91. std::cout << std::endl;
  92. std::cout << "Use 'no loops' (one argument):" << std::endl;
  93. MultiplicationTable::print_noLoop_oneArg();
  94. return 0;
  95. }
  96.  
Success #stdin #stdout 0s 4444KB
stdin
Standard input is empty
stdout
Use 'two' for loops:
 1, 2, 3, 4, 5, 6, 7, 8, 9,
 2, 4, 6, 8,10,12,14,16,18,
 3, 6, 9,12,15,18,21,24,27,
 4, 8,12,16,20,24,28,32,36,
 5,10,15,20,25,30,35,40,45,
 6,12,18,24,30,36,42,48,54,
 7,14,21,28,35,42,49,56,63,
 8,16,24,32,40,48,56,64,72,
 9,18,27,36,45,54,63,72,81,

Use 'only one' for loop:
 1, 2, 3, 4, 5, 6, 7, 8, 9,
 2, 4, 6, 8,10,12,14,16,18,
 3, 6, 9,12,15,18,21,24,27,
 4, 8,12,16,20,24,28,32,36,
 5,10,15,20,25,30,35,40,45,
 6,12,18,24,30,36,42,48,54,
 7,14,21,28,35,42,49,56,63,
 8,16,24,32,40,48,56,64,72,
 9,18,27,36,45,54,63,72,81,

Use 'no loops' (two arguments):
 1, 2, 3, 4, 5, 6, 7, 8, 9,
 2, 4, 6, 8,10,12,14,16,18,
 3, 6, 9,12,15,18,21,24,27,
 4, 8,12,16,20,24,28,32,36,
 5,10,15,20,25,30,35,40,45,
 6,12,18,24,30,36,42,48,54,
 7,14,21,28,35,42,49,56,63,
 8,16,24,32,40,48,56,64,72,
 9,18,27,36,45,54,63,72,81,

Use 'no loops' (one argument):
 1, 2, 3, 4, 5, 6, 7, 8, 9,
 2, 4, 6, 8,10,12,14,16,18,
 3, 6, 9,12,15,18,21,24,27,
 4, 8,12,16,20,24,28,32,36,
 5,10,15,20,25,30,35,40,45,
 6,12,18,24,30,36,42,48,54,
 7,14,21,28,35,42,49,56,63,
 8,16,24,32,40,48,56,64,72,
 9,18,27,36,45,54,63,72,81,