Re: [問題]C++ 九九乘法表 如何用if來寫呢??
手癢
meta-prpgramming版本
老實說我不知道那個inline到底最佳化會不會成功
如果會的話應該都會完全被展開來
相當於全部寫成cout形式
如果沒有其實就是遞迴的形式
這是我亂寫的 有錯別打我= =
--------------------------------------
#include <iostream>
using namespace std;
inline void printLine(int i, int j) {
cout << i << " * " << j << " = " << i * j << endl;
}
template <int I, int J>
struct Line {
inline static void print() {
printLine(I, J);
Line<I, J+1>::print();
}
};
template <int I>
struct Line<I, 9> {
inline static void print() {
printLine(I, 9);
}
};
template <int N>
struct Group {
inline static void print() {
Line<N, 1>::print();
cout << endl;
Group<N + 1>::print();
}
};
template <>
struct Group<9> {
inline static void print() {
Line<9, 1>::print();
}
};
int main() {
Group<1>::print();
return 0;
}
=============================
我改寫了一下= =
推廣成可以變成任意N*N的乘法表
新的版本
#include <iostream>
using namespace std;
inline void printLine(int i, int j) {
cout << i << " * " << j << " = " << i * j << endl;
}
template <int I, int J>
struct Line {
inline static void print() {
Line<I, J-1>::print();
printLine(I, J);
}
};
template <int I>
struct Line<I, 1> {
inline static void print() {
printLine(I, 1);
}
};
template <int NUM, int N>
struct Group {
inline static void print() {
Group<NUM, N - 1>::print();
cout << endl;
Line<N, NUM>::print();
}
};
template <int NUM>
struct Group<NUM, 1> {
inline static void print() {
Line<1, NUM>::print();
}
};
template <int NUM>
struct Meta99 {
inline static void print() {
Group<NUM,NUM>::print();
}
};
int main() {
Meta99<20>::print();
return 0;
}
--
Now.in 網路廣播平台 http://now.in
哇咧咧 創意投票系統 http://walele.com
易記學 程式設計教學 http://ez2learn.com/
VICTOR's 個人Blog http://blog.ez2learn.com/
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.165.223.152
推
02/02 15:33, , 1F
02/02 15:33, 1F
※ 編輯: StubbornLin 來自: 218.165.223.152 (02/02 15:43)
→
02/02 15:44, , 2F
02/02 15:44, 2F
→
02/02 15:46, , 3F
02/02 15:46, 3F
→
02/02 15:46, , 4F
02/02 15:46, 4F
→
02/02 15:47, , 5F
02/02 15:47, 5F
推
02/02 15:50, , 6F
02/02 15:50, 6F
→
02/02 15:53, , 7F
02/02 15:53, 7F
→
02/02 15:54, , 8F
02/02 15:54, 8F
推
02/02 16:13, , 9F
02/02 16:13, 9F
推
02/02 16:30, , 10F
02/02 16:30, 10F
推
02/02 16:39, , 11F
02/02 16:39, 11F
推
02/02 16:50, , 12F
02/02 16:50, 12F
推
02/02 17:11, , 13F
02/02 17:11, 13F
→
02/02 17:12, , 14F
02/02 17:12, 14F
推
02/02 18:04, , 15F
02/02 18:04, 15F
推
02/02 18:26, , 16F
02/02 18:26, 16F
→
02/02 18:35, , 17F
02/02 18:35, 17F
→
02/02 18:38, , 18F
02/02 18:38, 18F
推
02/03 00:45, , 19F
02/03 00:45, 19F
→
02/03 00:56, , 20F
02/03 00:56, 20F
→
02/03 00:57, , 21F
02/03 00:57, 21F
→
02/03 00:57, , 22F
02/03 00:57, 22F
→
02/03 00:58, , 23F
02/03 00:58, 23F
→
02/03 03:59, , 24F
02/03 03:59, 24F
→
02/03 03:59, , 25F
02/03 03:59, 25F
→
02/05 01:00, , 26F
02/05 01:00, 26F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 9 之 13 篇):