[英]Programming in C - CS50 Mario pyramid blocks print - For loops difficulties
我正在從 CS50 課程中解決這個問題。 我還是個初學者。 我需要編程的是:
在任天堂的超級馬里奧兄弟世界 1-1 結束時,馬里奧必須登上右對齊的金字塔金字塔,如下所示。
馬里奧跳上右對齊金字塔的屏幕截圖
讓我們用 C 重新創建那個金字塔,盡管是在文本中,使用哈希 (#) 表示積木,如下所示。 每個散列都比它高一點,所以金字塔本身也比它高。
# ## ### #### ##### ###### ####### ########
我們將編寫的程序將稱為 mario。 讓我們通過首先提示用戶輸入介於 1 和 8(包括 1 和 8)之間的正整數,讓用戶決定金字塔應該有多高。
但是我嘗試了很多方法,其中兩種是:
在查看了一些 Stack Overflow 嘗試之后,它現在看起來像這樣:
#include <cs50.h>
#include <stdio.h>
string hash(int);
int main(void)
{
int n;
do
{
n = get_int("Height: ");
}
while (n < 0 || n > 8);
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n-1-i; j++)
{
for(int j = 0; j < i+1; j++)
{
printf(".");
}
printf("#");
}
printf("\n");
}
}
接下來我可以嘗試什么?
Suriyu,補充一下Weather Vane所說的。 要通過 Check50,您仍需要對代碼進行小幅調整,使其通過所有 CS50 測試。
對於 do-while 循環,當 n = 0 時, n <=0
而不是n < 0
來要求輸入,因為規范要求最少一塊磚(1 到 8 塊)。
您只需要兩個循環,不要打印問題集中未指定的額外字符,例如: printf(".");
CS50 一切順利,這將是一次有趣的體驗!
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int n;
do
{
n = get_int("Height: ");
}
while (n < 1 || n > 8);
// this for loop makes new lines
for (int i = 0; i < n; i++)
{
// here I have two for loops nested inside the above for loop,
// I previously made the mistake of having two inner loops nested.
// this 2nd for loop prints n-1-i spaces
// because if n=5, then in the 4th row, there will be 5-1-3 spaces/dots
for (int j = 0; j < n - 1 - i; j++)
{
printf(" ");
}
// this 3rd for loop prints i+1 hashes
// because if n=5, then in the 4th row, there will be 3+1 hashes.
// (3 because you count from 0)
for (int j = 0; j < i + 1; j++)
{
printf("#");
}
printf("\n");
}
}
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int n;
do
{
n = get_int("Height of the pyramid is:\n");
}
while (n < 1 || n > 8); //condition to get a number from 1-8 from the user
for (int i = 0; i < n; i++) //loop for height
{
for (int j = 0; j < n - 1 - i; j++) //loop for spaces on left pyramid
{
printf(" ");
}
for (int k = 0; k < i + 1; k++) // loop for hashes on left pyramid
{
printf("#");
}
printf(" "); // spacing between pyramids
for (int p = 0; p <= i; p++) //loop for right pyramid
{
printf("#");
}
printf("\n");
}
}
如果您決定嘗試,這是問題的高級版本。
這是一種不同的方法。 這個版本不是迭代打印空白,然后迭代打印數字符號,而是創建一個緩沖區(大小由預編譯器常量定義 - 當前設置為 8,如果你想允許更大的金字塔,請更改它),然后為金字塔中的每一行它首先用數字符號填充緩沖區,然后用適當數量的空格覆蓋行的開頭,然后打印它:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#define MAXSIZE 8
int main(void)
{
int size, spaces;
char buf[MAXSIZE+1];
do
size = get_int("Height: ");
while (size < 0 || size > MAXSIZE);
buf[size] = '\0';
for(spaces = size-1 ; spaces >= 0 ; --spaces)
printf("%s\n", (char *)memset(memset(buf, '#', size), ' ', spaces));
}
這是另一種方法,它在內存中的數組中構建整個輸出塊,然后使用對puts
的單個調用打印它:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#define MAXSIZE 8
#define TOTSIZE ((MAXSIZE+1) * MAXSIZE)
int main(void)
{
int size, spaces;
char buf[TOTSIZE+1];
do
size = get_int("Height: ");
while (size < 0 || size > MAXSIZE);
memset(buf, '\n', (size+1)*size);
buf[((size+1)*size)] = '\0';
for(char *p = buf, spaces = size-1 ; *p != '\0' ; p += size+1, --spaces)
memset(memset(p, '#', size), ' ', spaces);
puts(buf);
}
這是一個可能效果最好的選項:
從 cs50 導入 get_int
為真:
n=get_int("Enter Height: ")
if n>=1 and n<=8:
break
對於范圍內的 i (0, n-1):
print(" " * (n - (i+1)) + "#" * (i+1))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.