不使用 C++ 中的循环、递归或转到打印字符 n 遍
在本节中,我们将了解如何在不使用 C++ 中的循环和递归的情况下打印一个字符 n 遍。我们可以通过使用字符串类构造函数来解决此问题。有一个构造函数,我们正在使用其中将多次打印的字符以及打印的次数。
示例代码
#include <iostream>
using namespace std;
void print_char_n_times(char my_char, int count) {
cout << string(count, my_char) << endl;
}
int main() {
//print character B 10 times
print_char_n_times('B', 10);
//print character x 30 times
print_char_n_times('x', 30);
}输出
BBBBBBBBBB xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP