我正在研究哈佛在线 CS50 课程的一些初始问题集。我让问题正常工作,但我想知道是否有更清洁或更好的方法让程序正常工作。
该程序的目标是打印由井号标签和空格字符组成的右对齐金字塔。非常欢迎任何有关风格或技巧的指导。
/* Creating the mario program, whose goal is to create a
* pyramid by accepting input from the user to get the
* height then aligning the pyrimid to the right.
*
*/
#include <stdio.h>
#include <cs50.h>
int main(void)
{
// get user input and set to variable
printf("Height: ");
int height = GetInt();
int i, j, k;
for(i = 1 ; i < height; i++)
{
// create n-1 spaces
for(k = (height - 2); k > (i-1); k--)
{
printf("%c", ' ');
}
// create n+1 hash tags
for(j = 0; j < (i+1); j++)
{
printf("#");
}
printf("\n");
}
return 0;
}