問題セットでは、ハッシュを使用して半ピラミッドを作成するよう求められます。これがどのように見えるかの画像へのリンクです-
私はアイデアを思いつき、スペースを出力するまでプログラムを書きました(前半をテストできるように、スペースを「_」に置き換えました.
しかし、プログラムを実行しようとすると、do-while ループを超えません。つまり、ピラミッドの高さを尋ね続け、for ループをまったく実行していないようです。複数のアプローチを試しましたが、この問題は解決しないようです。
どんな助けでも大歓迎です!
以下は私のコードです-
# include <cs50.h>
# include <stdio.h>
int main(void)
{
int height;
do
{
printf("Enter the height of the pyramid: ");
height = GetInt();
}
while (height > 0 || height < 24);
for (int rows = 1; rows <= height, rows++)
{
for (int spaces = height - rows; spaces > 0; spaces--)
{
printf("_");
}
}
return 0;
}
このプログラムを実行すると、次の出力が得られます。
Enter the height of the pyramid: 11
Enter the height of the pyramid: 1231
Enter the height of the pyramid: aawfaf
Retry: 12
Enter the height of the pyramid: