📌  相关文章
📜  在C++中打印1到100,没有循环和递归

📅  最后修改于: 2021-05-25 20:18:14             🧑  作者: Mango

以下是一个C++程序,该程序可以打印1到100,而无循环且无递归。

#include 
using namespace std;
  
template
class PrintOneToN
{
public:
    static void print()
    {
        PrintOneToN::print();  // Note that this is not recursion
        cout << N << endl;
    }
};
  
template<>
class PrintOneToN<1>
{
public:
    static void print()
    {
        cout << 1 << endl;
    }
};
int main()
{
    const int N = 100;
    PrintOneToN::print();
    return 0;
}

输出:

1
2
3
..
..
98
99
100