金字塔编程打印 [英] Pyramid Programming Print

查看:159
本文介绍了金字塔编程打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码打印出一个模式...我不明白评论行..请你告诉我



The below code print a pattern...and i am not understand the comment line..Can u please tell me

#include <stdio.h>
 
int main()
{
   int row, c, n, temp;
 
   printf("Enter the number of rows in pyramid of stars you wish to see ");
   scanf("%d",&n);
 
   temp = n;
 
   for ( row = 1 ; row <= n ; row++ )
   {
      for ( c = 1 ; c < temp ; c++ )
         printf(" ");
 
      temp--;
 
      for ( c = 1 ; c <= 2*row - 1 ; c++ )// what is mean by this line?? please explain......
         printf("*");
 
      printf("\n");
   }
 
   return 0;
}

推荐答案

这是一个循环:它有三个部分:

It's a loop: it has three parts:
for ( A ; B ; C)

其中:

A是初始设置。在你的情况下,在进入循环之前将c设置为1.

B是每次代码绕循环时执行的测试。如果是,则执行代码。如果不是,则循环结束。在你的情况下,它检查c是否仍然小于或等于行的两倍减去一。

C是阶跃变化 - 它定义了在循环结束时发生的事情,重复测试以确保循环在应该结束时结束。在你的情况下,它只添加一个c



因此,如果行为2,循环中的代码将执行3次。 (2 * 2 - 1 == 4 - 1 == 3,所以当c超过3时,循环将停止。)

Where:
A is the initial set up. In your case is sets "c" to 1 before it enters the loop.
B is the test that is performed each time the code is about to go round the loop. If it is true, then the code is executed. If not, the loop ends. In your case it checks that "c" is is still less than or equal to twice the rows minus one.
C is the step change - it defines what happens at the end of the loop, before the test is repeated to ensure that the loop ends when it is supposed to. In your case, it just adds one to "c"

So if rows was 2, the code in the loop would be executed 3 times. (2 * 2 - 1 == 4 - 1 == 3, so when c exceeds 3 the loop will stop.)


这篇关于金字塔编程打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆