输出1-100
模板元编程
// 利用递归继承实现输出0-100
#include <iostream>
template <int N>
class X : public X<N - 1> {
public:
X() { std::cout << N << std::endl; }
};
template <> class X<0> {};
int main() {
X <100> x;
return system("pause");
}
宏展开
// 利用宏展开实现输出0-100
#include <cstdio>
#include <cstdlib>
#define STEP1 ++n <= MAX ? printf("%d\n", n) : 0;
#define STEP2 STEP1 STEP1
#define STEP4 STEP2 STEP2
#define STEP8 STEP4 STEP4
#define STEP16 STEP8 STEP8
#define STEP32 STEP16 STEP16
#define STEP64 STEP32 STEP32
#define STEP128 STEP64 STEP64
#define STEP256 STEP128 STEP128
int n = 0;
const int MAX = 100;
int main() {
STEP128;
return system("pause");
}
网友评论