我应该如何修改此代码以使用给定字符串中的字母打印菱形图案?
How should I modify this code to print a diamond pattern using the letters from a given string?
对于一项作业,我需要编写一个程序,该程序接受用户输入的字符串并使用给定字符串的字母输出菱形图案。 (不使用数组)。
例如:
> Enter a word: hello
h
e e
l l
l l
o o
l l
l l
e e
h
我一直无法理解如何为此类问题操作嵌套 for 循环,因此我尝试获取类似问题的源代码并进行修改。
#include <iostream>
using namespace std;
int main()
{
int n,k,c,space=1;
cout<<"Enter the number of Rows..."<<endl;
cin>>n;
space=n-1;
for(k=1;k<=n;k++)
{
for(c=1;c<=space;c++)
{
cout<<" ";
}
space--;
for(c=1;c<=2*k-1;c++)
{
cout<<"*";
}
cout<<endl;
}
space=1;
for(k=1;k<=n;k++)
{
for(c=1;c<=space;c++)
{
cout<<" ";
}
space++;
for(c=1;c<=2*(n-k)-1;c++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}
这将打印一个由星号组成的三角形。最初我以为我可以稍微编辑一下变量并使用源代码来为我处理间距,但显然因为我真的不明白变量首先是如何影响问题的,所以我还没有走得太远.如果有人能向我解释我应该如何解决这个问题,我将不胜感激。
解决此类问题的一个好方法是从一张图片开始,例如您在问题中链接的图片。现在,对于五个字母的单词 "Hello",你可以从图中看出 H 以位置 4 为中心。(记住,数组和字符串索引从 0 开始。)下一步还要看其他示例.例如,让我们看看菱形对 3 个字母的单词 "cat".
的要求是什么
c
a a
t t
a a
c
这一次,字母'c'以位置2为中心,做这些例子的目的是为了找到一个规律;在这里,我们发现模式是第一个字母总是以位置 word length - 1
为中心。因此,第一个字母所在的行中有 word length - 2
个前导 space。
下一行呢?请注意在重复的字母之间如何少了一个前导 space 和一个额外的 space。因此,我们有 word length - 2 - 1
前导 space 和 1
space 分隔它们。
第三行呢?现在我们在字母和 word length - 2 - 2
前导 space 之间有三个 space。你开始看到一种模式了吗?查看第一个示例 ("Hello") 的第四行和第五行并尝试计算前导 space 和中间字母 space 的数量如何。 试一试后请阅读下文。
每次我们往下走一排,我们就失去一个领先者space。除第二行外,每往下一行,我们还会在字母之间获得两个 space。 现在,您可以将此模式转换为公式吗? 再一次,看看你能不能想出一个公式,然后继续阅读。
我们了解到前导space的数量等于word length - 1 - row
(其中row
从0开始),space的数量等于-字母之间等于 row * 2 - 1
。 (注意这个公式如何正确处理第二行的情况。)
因此,您的代码应如下所示:
// Word of caution: I have not tested this code.
using namespace std; // Bad practice in production code; used here for simplicity.
// *snip*
string my_word;
cin >> my_word;
int middle_index = my_word.length() - 1;
for (int r = 0; r < my_word.length; ++r) {
// This code prints the top part of the diamond.
for (int ls = 0; ls < middle_index - r; ++ls) {
cout << " "; // Print out the leading spaces.
}
cout << my_word[r]; // You can replace this with my_word.substr(r, 1)
// if you are unallowed to treat strings like arrays.
// r == 0 is a special case since we only print one of those letters.
if (r == 0) {
continue;
}
// Otherwise, we simply need to print the number of spaces in-between.
for (int bs = 0; bs < 2 * r - 1; ++bs) {
cout << " ";
}
cout << my_word[r];
}
现在要打印钻石的另一半,您必须执行与上述类似的操作。请记住从 word length - 2
开始循环(请参阅您的图片以了解原因)并每次递减循环索引。也不要忘记 r == 0
仍然是一个特例,因为你只打印一次。
对于一项作业,我需要编写一个程序,该程序接受用户输入的字符串并使用给定字符串的字母输出菱形图案。 (不使用数组)。
例如:
> Enter a word: hello
h
e e
l l
l l
o o
l l
l l
e e
h
我一直无法理解如何为此类问题操作嵌套 for 循环,因此我尝试获取类似问题的源代码并进行修改。
#include <iostream>
using namespace std;
int main()
{
int n,k,c,space=1;
cout<<"Enter the number of Rows..."<<endl;
cin>>n;
space=n-1;
for(k=1;k<=n;k++)
{
for(c=1;c<=space;c++)
{
cout<<" ";
}
space--;
for(c=1;c<=2*k-1;c++)
{
cout<<"*";
}
cout<<endl;
}
space=1;
for(k=1;k<=n;k++)
{
for(c=1;c<=space;c++)
{
cout<<" ";
}
space++;
for(c=1;c<=2*(n-k)-1;c++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}
这将打印一个由星号组成的三角形。最初我以为我可以稍微编辑一下变量并使用源代码来为我处理间距,但显然因为我真的不明白变量首先是如何影响问题的,所以我还没有走得太远.如果有人能向我解释我应该如何解决这个问题,我将不胜感激。
解决此类问题的一个好方法是从一张图片开始,例如您在问题中链接的图片。现在,对于五个字母的单词 "Hello",你可以从图中看出 H 以位置 4 为中心。(记住,数组和字符串索引从 0 开始。)下一步还要看其他示例.例如,让我们看看菱形对 3 个字母的单词 "cat".
的要求是什么 c
a a
t t
a a
c
这一次,字母'c'以位置2为中心,做这些例子的目的是为了找到一个规律;在这里,我们发现模式是第一个字母总是以位置 word length - 1
为中心。因此,第一个字母所在的行中有 word length - 2
个前导 space。
下一行呢?请注意在重复的字母之间如何少了一个前导 space 和一个额外的 space。因此,我们有 word length - 2 - 1
前导 space 和 1
space 分隔它们。
第三行呢?现在我们在字母和 word length - 2 - 2
前导 space 之间有三个 space。你开始看到一种模式了吗?查看第一个示例 ("Hello") 的第四行和第五行并尝试计算前导 space 和中间字母 space 的数量如何。 试一试后请阅读下文。
每次我们往下走一排,我们就失去一个领先者space。除第二行外,每往下一行,我们还会在字母之间获得两个 space。 现在,您可以将此模式转换为公式吗? 再一次,看看你能不能想出一个公式,然后继续阅读。
我们了解到前导space的数量等于word length - 1 - row
(其中row
从0开始),space的数量等于-字母之间等于 row * 2 - 1
。 (注意这个公式如何正确处理第二行的情况。)
因此,您的代码应如下所示:
// Word of caution: I have not tested this code.
using namespace std; // Bad practice in production code; used here for simplicity.
// *snip*
string my_word;
cin >> my_word;
int middle_index = my_word.length() - 1;
for (int r = 0; r < my_word.length; ++r) {
// This code prints the top part of the diamond.
for (int ls = 0; ls < middle_index - r; ++ls) {
cout << " "; // Print out the leading spaces.
}
cout << my_word[r]; // You can replace this with my_word.substr(r, 1)
// if you are unallowed to treat strings like arrays.
// r == 0 is a special case since we only print one of those letters.
if (r == 0) {
continue;
}
// Otherwise, we simply need to print the number of spaces in-between.
for (int bs = 0; bs < 2 * r - 1; ++bs) {
cout << " ";
}
cout << my_word[r];
}
现在要打印钻石的另一半,您必须执行与上述类似的操作。请记住从 word length - 2
开始循环(请参阅您的图片以了解原因)并每次递减循环索引。也不要忘记 r == 0
仍然是一个特例,因为你只打印一次。