如何在不使用循环、递归或goto的情况下打印N次“Hello”(其中N是用户输入)。
null
输入:N,表示要打印语句的次数。 输出:语句N次
首先我们创建一个类。之后,我们需要通过在cout/print语句中编写要打印的语句来初始化类的构造函数。这里使用的基本思想是“创建类的对象的次数,该类的构造函数被调用的次数。”
// CPP program to print a sentence N times // without loop and recursion. // Author : Rohan Prasad #include <iostream> using namespace std; class Print { public : Print() { cout << "Hello" << endl; } }; int main() { int N = 5; Print a[N]; return 0; } |
输出:
Hello Hello Hello Hello Hello
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END