打印具有相等间距的星星数以创建V 我在Linux系统上用C++编写代码。我试着向用户询问他们想要的x个星星的数量,以及打印出来的时间,如果他们说是ex 3的话。我希望它看起来像这样: *** *** *** *** *** *** ******
因此,它应该在两面打印3颗星,但第二行应该以1个空格开始,第三行应该以2个空格开始 打印第一行(用户输入)*,后跟2个(用户输入)空格,然后再后跟(用户输入)* 用一个空格打印第二行,后跟(用户输入)*,后跟2N− 2个空格,后跟(用户输入)* 继续打印N+1行打印具有相等间距的星星数以创建V 我在Linux系统上用C++编写代码。我试着向用户询问他们想要的x个星星的数量,以及打印出来的时间,如果他们说是ex 3的话。我希望它看起来像这样: *** *** *** *** *** *** ******,c++,C++,因此,它应该在两面打印3颗星,但第二行应该以1个空格开始,第三行应该以2个空格开始 打印第一行(用户输入)*,后跟2个(用户输入)空格,然后再后跟(用户输入)* 用一个空格打印第二行,后跟(用户输入)*,后跟2N− 2个空格,后跟(用户输入)* 继续打印N+1行 我附上了我当前代码和打印件的照片。我想要一些关于如何让它打印所有星星和如何做间距的指导 多谢各位 代码: 作为参考,这里是我的示例代码。希望能有帮助 #include <iostream> using namespace
我附上了我当前代码和打印件的照片。我想要一些关于如何让它打印所有星星和如何做间距的指导 多谢各位 代码:
作为参考,这里是我的示例代码。希望能有帮助
#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;
}
使用方法
strings
而不是@Manny102030string(numStars,star)
方法
string strings(const int num, const char ch) {
char result[num+1] = {'\0'};
for(int i = 0; i < num; i++) {
result[i] = ch;
}
return result; // covert c-string to string
}
最新更新:
#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;
}
#包括
使用名称空间std;
无效重复打印(常量int num,常量char ch){
for(int i=0;i
#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;
}
#包括
#包括
使用名称空间std;
int main()
{
字符星='*';
字符空间=“”;
int numStars;//两边的星星数
cout>numStars;
string stars=string(numStars,star);//例如,如果numStars是3,它将变成'***'
int spacesBetween=numStars*2;//恒星之间的空间
对于(int i=0;i
我认为彼得·潘的回答很有可读性
但我正在努力理解该做什么以及如何去做,我想
学习,我不想只是复制
你把这个帖子标记为C++,下面我提供了一个可能的C++方法。
我使用递归(简单地阻止你复制,问题是受欢迎的)。否则,我的版本(我认为更多的C++)ISE与其他答案非常相似。
怎么办:
a) 捕获您的预期输出(3),并计算每个字符子集的数量。可能将输出V写在网格上
对至少一个其他N重复此操作。这将帮助您确定各个部分的长度(参见下面的步骤c),并且提供的代码(我的和其他)显示了一些可能的实现,网格大小如何影响计算
b) 将“星形V”问题的写入划分为逐行操作
我没有给递归构建的每一行贴上标签,一行接一行……也许这个想法的唯一标签是0..N。也许是“for循环”
c) 决定如何生产每一条生产线
我已经为每行的4个部分设计了标签
- leftMargin-带缩进的空格(以使线居中)
- 星星-每行需要N个数字(两次)
- 空间-用于清理立柱之间的区域(如足球?)
c) 对于每个行部分,我创建方法来生成所需的字符串。
(我把它们叫做左边距、星星、空格…)
我使用std::stringstream方法(来自#include sstream)和iomanip方法来生成字符串(而不是使用我自己的for循环)
d) 递归方法V1r使用一个标识终止的参数,用于创建行。V1r在创建行之前进行递归
e) 递归方法inversed_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);
}
示例输出:(7)
“附上我当前代码的照片"。请不要这样做。将代码作为格式化文本直接粘贴到问题中。否则其他人无法轻松复制代码以尝试或在评论/回答中引用。而且在移动设备上阅读更困难。任何文本日志都是如此。如果用户输入的数字不均匀怎么办?右侧是否会有1个额外的*
?我已经写好了g、 让我重述一下。我举的例子是用户输入3,这样它的每一面都会有3颗星。我澄清了我想说的。我很感激这个答案,但我正在努力理解该做什么和如何做。我想学习,我不想只是复制。@splatted抱歉我的轻率。我只是想帮助你理解通过一个清晰易读的代码进行ng。你需要我删除代码或改进我的描述吗?这根本不是问题。再一次,我真的很感谢你发布这样做的方法,但我只是试图了解代码的每一部分是如何做的,当我在做未来的硬件时,我实际上知道我在做什么。我有一位教授,他没有专门研究ak英语很好,我很难理解他,所以我试着自己学习,并尽可能多地向他学习。如果你不介意的话,Manny编写的代码对我来说似乎很熟悉,因为他/她使用了一些功能。你的代码中有一些我以前没有见过的东西。你能帮我理解他们的代码吗@Skpok好。你想用另一种方式来代替<代码>字符串(NothStar,Star)< /C>方法吗?还是用C++中的<代码>字符串类型?我发布另一个答案来查找<代码>字符串(NothStar,Star)< /C>方法。请随时告诉我。我们还没有做字符串,所以我想不做它。
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] = '\0';
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;
}
#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;
}
#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 <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);
}
real 0m1.150s
user 0m1.004s
sys 0m0.104s
*** ***
*** ***
*** ***
******
******
*** ***
*** ***
*** ***
204 us
******* *******
******* *******
******* *******
******* *******
******* *******
******* *******
******* *******
**************
**************
******* *******
******* *******
******* *******
******* *******
******* *******
******* *******
******* *******
286 us