使用嵌套循环在C中打印星形(’*’)钻石?
当用户为钻石输入5时,我希望能够打印出这样的钻石。 但也适用于任何奇数且大于0的值。
我有一个代码,用于为用户输入5制作钻石,但不适用于所有奇数输入..
half = (size/2)+1; for (a=1; a <= half ; a++) /*top to mid row of diamond*/ { for (b=a; b<half;b++) { printf(" "); } for (c= size -2* a; c <= half; c++) { printf("*"); } printf("\n"); } for (a = 1; a 0;b--) { printf(" "); } for (c = size-2*a; c >0 ;c--) { printf("*"); } printf("\n"); } return 0; }
任何帮助将不胜感激。谢谢。
麦克风
几乎可以肯定的功课,所以这里有一个线索。
计算一行前的空格数和该行中的星数。 你要找的是行号和这两个值之间的关系。
然后你可以使用两个连续的for
循环,一个增加星数,另一个减少它。
在每个循环中,将有两个连续的循环,用于打印所需数量的空格,后跟所需数量的星号,后跟换行符。
如果您在阅读完上述内容后仍然遇到问题,请考虑一下。 对于输入(奇数,当你在评论中强制执行) n
,空间计数从(n - 1) / 2
,星数从1
。 对于每个后续行,空间计数减少1
,星数增加2
。
这一直到空间计数达到0
的点,然后你转过身去另一个方向,确保你不打印那条中间线两次。
一旦星数达到0
,你就完成了。
现在你只需将该规范转换为代码:-)
现在,既然你已经在评论中指出你有兴趣制作自己的解决方案,而不仅仅是交代码,我觉得很自然能给你一些东西,你可以检查自己的解决方案。 这是我要使用的伪代码:
# Input data, check and init counters. input n make sure n is odd and greater than 2 set numspaces to (n-1) / 2 set numstars to 1 # Gradually get wider until just before middle line. while numspaces > 0: for i = 1 to numspaces: output " " for i = 1 to numstars: output "*" output newline subtract 1 from numspaces add 2 to numstars # Gradually get thinner until end. while numstars > 0: for i = 1 to numspaces: output " " for i = 1 to numstars: output "*" output newline add 1 to numspaces subtract 2 from numstars
而且,作为最后的练习,你可以重构:
for i = 1 to numspaces: output " " for i = 1 to numstars: output "*" output newline
进入一个单独的函数,因为它在两个循环之间是通用的。
现在,既然你已经有了自己的代码,那么这里是用于概念validation的Python代码,包括完整性:
def lineout (sp, st): s = "" for i in range (sp): s = "%s "%(s) for i in range (st): s = "%s*"%(s) print s n = 21 numspaces = (n-1) / 2 numstars = 1 while numspaces > 0: lineout (numspaces, numstars) numspaces -= 1 numstars += 2 while numstars > 0: lineout (numspaces, numstars) numspaces += 1 numstars -= 2
如果您使用更现代的function,那么您可能会在Python中更简洁地编写它,但这样可能会破坏快速理解和轻松翻译的目的。 只需将n
更改为您想要的任何数字(奇数和大于2,提供所得到的钻石将适合您的终端)并享受输出:-)
* *** ***** ******* ********* *********** ************* *************** ***************** ******************* ********************* ******************* ***************** *************** ************* *********** ********* ******* ***** *** *
这里有两件事:
-
缩进更改(-1到“mid”,“+1”后面)
-
星数变化(+2到“中”,-2“后”)
现在,这可以通过两个循环完成(一个用于顶部到“mid”和一个“after”),但是值也可以用一点数学来确定。 让我们探讨一下 🙂
这是数字:
s(空格)x(星)n(行号) __X 2 1 0 _XXX 1 3 1 XXXXXX 0 5 2 _XXX 1 3 3 __X 2 1 4
首先要注意s
是关于“mid”的对称:
- s = abs(2 – n)
然后x
与s
相关(如上面的增量中所述,为2):
- x = 5 – (s * 2)= 5 – (abs(2 – n)* 2)
希望能给出一些见解!
你现在可能正在上初级C级课程,而且和其他人一样,我会阻止你复制,但是这里的参考是一个答案:
为了使答案变得简单,您需要知道的是要打印多少空格和星星。
顺序如下: [print spaces] [print stars] [print spaces] [print’\ n’] 。
观察中间行我们需要: (0)空格, (numStars)星,(0)空格 。
在上面和下面的行中,我们需要: (1)空格, (numStars – 2)星,(1)空格 。
等等…
这应该让你觉得要计算空间数,你需要计算距离中间行的距离。 另请注意,每行远离中间,星数会减少2。
#include #include int main(int argc, char** argv) { int num, middle, spaceCount, starCount; printf("Enter a num:"); scanf("%d",&num); middle = (num-1)/2; for (int i = 0; i < num; i++){ spaceCount = abs(middle - i); starCount = num - 2*abs(middle - i); for(int c = 0; c < spaceCount; c++) printf(" "); for(int c = 0; c < starCount; c++) printf("*"); for(int c = 0; c < spaceCount; c++) printf(" "); printf("\n"); } return 0; }
由于我们计算当前行和中间行之间的距离,我们需要知道绝对值。 abs函数是标准C函数来做到这一点。
我有点挣扎。 对不起,我的最终代码是C ++,而不是严格的C,因为我在10年内没有使用过C. 对于那些试图了解如何为自己解决这个问题的人来说,这是一个更罗嗦的解释。 在我开始之前,你可以做的一件事就是帮助自己开始:
int numStars = 1,numSpaces = 10;
numSpaces并不重要,只要它足够小以适应你的屏幕……你可以插入任何数字并稍后测试。 然后,计算出你的算法,稍后你可以处理输入。
此外,星星之后的空格数与此问题无关。
然后,解决如何输出钻石顶部的问题。 我用一个计数器循环来打印空格,直到我打印出第一行的空格数,然后是第二个循环(不是嵌套的)来打印该行所需的星数。 我们知道在第1行中,我们需要打印10次空间和一次星形。
一旦行具有所需的空格和星号数,就转到换行符(“\ n”或endl)。 但是下一行你需要打印2个星星,少1个空格。 所以你必须适当增加/减少。
当你到达中间行之前的那一行时,你会停下来。 换句话说,当numSpaces大于0时,顶部循环需要继续。在循环中最终传递结束时,numSpaces递减为0,但它不会打印具有0个空格和n个星的行(2星)对于你开始的每个空间)。 这发生在你的下一个循环……
然后,您可以运行第二个循环,在结束时反转您的增量/减量,但在其他方面几乎相同。 只有这一次,它应该打印0个空格和10n个星。 然后换行,最后将numStars减少2并将numSpaces增加1。
如果你还没有使用function或者不允许实现,那就是它。 你可以轻松地调用一个函数,为每个循环传递numSpaces / numStars以消除重复的代码。
这是我在C ++中的代码:
#include using namespace std; int main() { int numStars = 1, numSpaces = 10; while (numSpaces > 0) { for (int i = 0; i < numSpaces; i++) { cout << " "; } for (int i = 0; i < numStars; i++) { cout << "*"; } cout << "\n"; numStars += 2; numSpaces--; } while (numStars > 0) { for (int i = 0; i < numSpaces; i++) { cout << " "; } for (int i = 0; i < numStars; i++) { cout << "*"; } cout << "\n"; numStars -= 2; numSpaces++; } return 0; }
我不想给你代码,你应该学习。
输入数字将是钻石的宽度,因此请务必在空格和星号之间建立一个不超过此数字的关系。
主要思想是从1星号开始,每次增加2,直到达到输入数字。
此外,如果用户输入偶数,请执行某些操作以防止错误。
这里是matlab的代码
clear all, clc n=input('Input an Odd Number '); for i=1:(n+1)/2 for m=1:(n+1)/2-i fprintf(' '); end for j=1:(2*i)-1 fprintf('*'); end fprintf('\n'); end for k=i-1:-1:1 for m=1:(n+1)/2-k fprintf(' '); end for j=1:(2*k)-1 fprintf('*'); end fprintf('\n'); end
static void Main(string[] args) { //check this out more optimized code hope it will be help full. for (int row=-2;row<=2;row++) { for (int col=-2;col<=2;col++) { if (Math.Abs(row)+Math.Abs(col)<=2) { Console.Write("*"); } else { Console.Write(" "); } } Console.WriteLine(); } Console.ReadKey(); }
#include using namespace std; int main() { int n=0, star=0, space=0, ct2=0, ct3=1; cin >> n; ct2=n/2; for(int ct=0; ct< (n/2); ct++) { for(space; space
#include main() { int i,j,k,n; scanf("%d",&n); n=(n+1)/2; for(i=1;i<=n;i++) { for(j=n;j>=i;j--) printf(" "); for(k=1;k<=(2*i-1);k++) printf("*"); printf("\n"); } for(i=1;i<=(n-1);i++) { for(j=0;j<=i;j++) printf(" "); for(k=(2*n-3);k>=(2*i-1);k--) printf("*"); printf("\n"); } }
嗨,我得到了最好的解决方案,以减少线路中的问题
int b=0; for(int a=0;b<=50;a=(b<=25) ? a+2 : a-2 ){ b+=2; for(int b=25-a;b>0;b-=2){ printf(" "); } for(int c=0;c<=a;c++){ printf("*"); } printf("\n"); }
用星号标记在C中输出钻石
#include main() { int n, i, j; printf("Enter a number: "); scanf("%d",&n); /* Create up arrow. */ for(i=1; i<=n; i++) { /* Left side. */ for(j=i; j<=n+1; j++) { printf(" "); } for(j=1; j<=1; j++) { printf("*"); } /* Middle. */ for(j=1; j