打印具有相等空格的星形数以创建V
Printing number of stars with equal spaces to create a V
我在linux系统上用c++进行编码。我试图向用户询问他们想要的x颗星数,以及当它打印出来时,如果他们说是ex 3的话。我希望它看起来像这样:
*** ***
*** ***
*** ***
******
因此,它应该在两侧打印3颗星,但第二行应该以1个空格开头,第三行应该以2个空格开头。
打印带有(用户输入)*的第一行,然后是2个(用户输入的)空格,然后是(用户输入*用一个空格打印第二行,后跟(用户输入)*,后跟2N−2个空格,然后后跟(用户输出)*继续打印N+1行
我附上了我当前代码和打印出来的图片。我想要一些关于如何打印所有恒星以及如何进行间距的指导。
谢谢
代码:
#include <iostream>
using namespace std;
int main()
{
int n = 0;
char star = '*';
cout << "Please enter a number." << endl;
cin >> n;
for(int i = 0; i <= n; i++)
{
cout << "n";
for (int j = 0; j <= n - i; j++)
{
cout << " ";
}
for(int k = 1; k <= i; k++)
{
cout << "*";
}
cout << endl;
}
return 0;
}
输出:
Please enter a number.
5
.
..
...
....
.....
这对我有效:
#include <iostream>
#include <string>
using namespace std;
int main()
{
char star = '*';
char space = ' ';
int numStars; // Number of stars on each side
cout << "Please enter a number: ";
cin >> numStars;
string stars = string(numStars, star); // Example, if numStars were 3, it would become '***'
int spacesBetween = numStars*2; // Space between the stars
for(int i = 0; i < numStars+1; i++)
{
cout << string(i, ' ') << stars << string(spacesBetween, ' ') << stars << endl;
spacesBetween -= 2; // subtract 2 spaces each time
}
return 0;
}
没有string()函数:
#include <iostream>
#include <string>
using namespace std;
int main()
{
char star = '*';
char space = ' ';
int numStars; // Number of stars on each side
cout << "Please enter a number: ";
cin >> numStars;
// I commented this out to show how you can do it without string()
//string stars = string(numStars, star);
string stars = "";
for(int i = 0; i < numStars; i++)
{
stars += star; // stars += '*'
}
int spacesBetween = numStars*2; // Number of spaces between the stars
string spaces = ""; // Spaces between the stars
for(int i = 0; i < numStars+1; i++)
{
spaces = "";
for(int i = 0; i < spacesBetween; i++)
{
spaces += ' ';
}
cout << string(i, ' ') << stars << spaces << stars << endl;
spacesBetween -= 2; // subtract 2 spaces each time
}
return 0;
}
作为参考,这里是我的示例代码。希望能有所帮助。
#include <iostream>
using namespace std;
const char star = '*';
void printSpaces(int line) {
for(int space = 0; space < line; space++) {
cout << " ";
}
}
void printStars(int n) {
for(int i = 0; i < n; i++) {
cout << star;
}
}
void printDoubleSpaces(int n, int line) {
for(int i = 2*n - 3 - line; i > 0; i--) {
cout << " ";
}
}
int main() {
int n = 0;
cout << "Please enter a number." << endl;
cin >> n;
for(int line = 0; line < 2*n-2; line++) {
printSpaces(line);
printStars(n);
printDoubleSpaces(n, line);
printStars(n);
cout << endl;
}
return 0;
}
输出:
Please enter a number.
3
*** ***
*** ***
*** ***
******
Please enter a number.
4
**** ****
**** ****
**** ****
**** ****
**** ****
********
Please enter a number.
5
***** *****
***** *****
***** *****
***** *****
***** *****
***** *****
***** *****
**********
使用方法strings
而不是@Manny102030string(numStars, star)
方法。
string strings(const int num, const char ch) {
char result[num+1] = {' '};
for(int i = 0; i < num; i++) {
result[i] = ch;
}
return result; // covert c-string to string
}
或者在C++中不使用string
类型
char * strings(const int num, const char ch) {
char * result = (char *)malloc(num+1); // memory allocate for repeating character
for(int i = 0; i < num; i++) {
result[i] = ch;
}
result[num] = ' ';
return result;
}
char * stars = strings(numStars, star); // In `main` method, insteading of `string stars = string(numStars, star);`
int spacesBetween = numStars*2; // Space between the stars
for(int i = 0; i < numStars+1; i++)
{
char * spacesLeft = strings(i, ' ');
char * spacesMid = strings(spacesBetween, ' ');
cout << spacesLeft << stars << spacesMid << stars << endl;
spacesBetween -= 2; // subtract 2 spaces each time
free(spacesLeft); // free allocated memory for left spaces
free(spacesMid); // free allocated memory for middle spaces
}
free(stars); // free allocated memory for stars
新更新:
#include <iostream>
using namespace std;
void repeatPrint(const int num, const char ch) {
for(int i = 0; i < num; i++) {
cout << ch;
}
}
int main() {
char star = '*';
char space = ' ';
int numStars; // Number of stars on each side
cout << "Please enter a number: ";
cin >> numStars;
int spacesBetween = numStars*2; // Space between the stars
for(int i = 0; i < numStars+1; i++)
{
repeatPrint(i, space); // Print left spaces
repeatPrint(numStars, star); // Print stars
repeatPrint(spacesBetween, space); // Print middle spaces
repeatPrint(numStars, star); // Print stars
cout << endl; // Newline
spacesBetween -= 2; // subtract 2 spaces each time
}
return 0;
}
我想要一些关于如何让它打印全明星以及如何做间距。
我认为彼得·潘的回答很有可读性。
但我正在努力了解该做什么以及如何做。我想学习,我不想只是复制。
你已经将这篇文章标记为C++,下面我提供了一种可能的C++方法。
我使用了递归(只是为了阻止您复制,欢迎提问)。除此之外,我的版本(我认为它更像C++)与其他答案非常相似。
要做什么:
a) 捕获您的预期输出(对于3),并计算每个字符子集。也许把输出V写在一个网格上。
对至少一个其他N重复此操作。这将帮助您确定各个部分的长度(请参见下面的步骤c),并且提供的代码(包括我的代码和其他代码)显示了一些可能的实现,网格大小如何影响计算。
b) 将"星形V"问题的写入划分为逐行操作。
我没有标记这些行递归构建每一行,一行接一行。。。也许这个想法的唯一标签是0..N。也许是"for循环"?
c) 决定如何生产每一条生产线。
我为每条线路的4个部分中的每一个都设计了标签,
-
leftMargin-带缩进的空格(使线条居中)
-
星形-每行需要N个数字(两次)
-
空间-用于清理立柱之间的区域(如足球?)
c)对于每个线段,我创建方法来生成所需的字符串。(我把它们叫做左边距、星星、空格……)。
我使用std::stringstream方法(来自#includesstream)和iomanip方法来生成字符串(而不是使用我自己的for循环)
d) 递归方法V1r使用识别终止的参数,并用于行创建。V1r在裁剪线条之前重复出现。
e) 递归方法inverted_V1r的工作方式大致相同。然而,它在整理线条后会重复出现。
阅读上面的每个注释,并与代码进行比较,然后尝试一下。
我建议您编写一个简单的for循环,首先只处理V1。祝你好运
#include <chrono>
#include <iomanip>
#include <iostream>
#include <sstream>
class T452_t
{
private:
int N;
public:
T452_t(int argc, char* argv[]) // ctor
{
switch (argc)
{
case 2: { N = std::stoi(argv[1]); } break;
default: {
std::cerr << " require one number, with value > 2" << std::endl;
exit(-1);
} break;
}
}
// using stringstream and iomanip
std::string leftMargin(int lvl) {
std::stringstream ss;
ss << std::setfill(' ') << std::setw(2+(N-lvl)) << " ";
return (ss.str()); // ----------^^^^^^^^---computed indent
}
std::string stars() {
std::stringstream ss;
ss << std::setfill('*') << std::setw(N) << "*";
return (ss.str()); // ----------^--- quantity
}
std::string spaces(int lvl) {
std::stringstream ss;
if (0 != lvl) // no spaces at lvl 0
ss << std::setfill(' ') << std::setw(lvl*2) << " ";
return (ss.str()); // ----------^^^^^--- quantity
}
// using recursion
void V1r(int lvl)
{
if(lvl > N) return; // recursion termination clause
V1r(lvl+1);
std::cout << leftMargin(lvl)
<< stars() << spaces(lvl) << stars()
<< std::endl;
}
// recursion
void inverted_V1r(int lvl) // upside down V1r
{
if(lvl > N) return; // recursion termination clause
std::cout << leftMargin(lvl)
<< stars() << spaces(lvl) << stars()
<< std::endl;
inverted_V1r(lvl+1); // tail recursion possible
}
int exec()
{
if (N < 2) { std::cerr << " value < 2 is too small" << std::endl; exit(-2); }
std::cout << std::endl;
V1r(0);
std::cout << "n"<< std::endl;
inverted_V1r(0);
std::cout << std::endl;
return(0);
}
}; // class T452_t
int main (int argc, char* argv[])
{
std::chrono::high_resolution_clock::time_point m_start_us =
std::chrono::high_resolution_clock::now();
int retVal = -1;
{
T452_t t452 (argc, argv);
retVal = t452.exec();
}
std::chrono::microseconds chrono_duration_us =
std::chrono::duration_cast <std::chrono::microseconds>
(std::chrono::high_resolution_clock::now() - m_start_us);
std::cout << chrono_duration_us.count() << " us" << std::endl;
return(retVal);
}
示例输出(3):
real 0m1.150s
user 0m1.004s
sys 0m0.104s
*** ***
*** ***
*** ***
******
******
*** ***
*** ***
*** ***
204 us
示例输出:(7)
******* *******
******* *******
******* *******
******* *******
******* *******
******* *******
******* *******
**************
**************
******* *******
******* *******
******* *******
******* *******
******* *******
******* *******
******* *******
286 us
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 使用std::multimap迭代器创建std::list
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 2D数组来自文本输入,中间有空格
- 使用CMake创建QML插件
- 如何在c++中为模板函数实例创建快捷方式
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 试图在visual studio上用C++创建一个桌面应用程序
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 如何在C++20中创建模板别名的推导指南
- 从文件中读取多行.txt字符串删除空格并创建新文件进行输出
- 如何使用循环从另一个字符串创建一个没有空格的新字符串
- 打印具有相等空格的星形数以创建V
- 我正在尝试在下划线之间创建空格,这代表一个秘密短语
- 在屏幕上拼接的字符之间创建空格 - SDL
- 基于另一个字符串创建一个由空格 (_) 和空格组成的字符串
- Emacs c++模式在预处理器语句后创建尾随空格+反斜杠
- 创建一个允许输入中空格的读取函数(C++)