私は、ハーバードのオンライン 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;
}