我需要像這樣撰寫一個數字模式金字塔:

這是我的代碼:
#include<stdio.h>
int main()
{
int i, j, rows = 5;
for (i = 1; i <= 5; i )
{
for (j = 1; j <= i; j )
{
printf("%d ", j);
}
printf("\n");
}
for (i = 0; i <= 5; i )
{
for (j = 0; j <= i; j )
{
printf("%d ", j);
}
printf("\n");
}
}
Where am I doing wrong? I need to flip somehow the first triangle pattern and mix it with the second. Please help.
uj5u.com熱心網友回復:
你的金字塔有 6 層高。最大數量是 5。這不是巧合。有直接關系。
如果n是5,你需要做 6 次,從0到n。有一個回圈。
for (int i = 0; i <= n; i ) {
...
}
在每一行上,您需要從ito列印0,然后從1to列印i。那是兩個回圈。
for (int j = i; j > 0; j--) {
...
}
for (int j = 1; j <= i; j ) {
...
}
當然,還需要縮進多個與 成反比的空格i,這又是一個回圈。
for (int j = 0; j <= /* Fill in here for inverse relationship */; j ) {
...
}
uj5u.com熱心網友回復:
我需要以某種方式翻轉第一個三角形圖案并將其與第二個混合。
TL;DR訣竅非常簡單:您的行索引i應該從|n - j|(或abs(n - j))開始,其中j是列索引并且n是數字。
您可以這樣做:
for (int j = 0; j < ncols; j) {
for (int i = abs(n - j); i < nrows; i) {
matrix[i][j] = '0' abs(n - j);
}
}
解釋:
想象一下你有一個矩陣。為了建立一個帶有數字的金字塔n,你需??要:
n 1行(因為您將0包含在頂部)。讓我們稱之為nrows。2n 1列(2n因為數字將連續出現兩次,1因為每行會有一個0)。讓我們稱之為ncols。
0 1 2 3 4 5 6 7 8 9 10
- - - - - - - - - - -
0 | 0
1 | 1 0 1
2 | 2 1 0 1 2
3 | 3 2 1 0 1 2 3
4 | 4 3 2 1 0 1 2 3 4
5 | 5 4 3 2 1 0 1 2 3 4 5
首先,您要填充前半部分,即從列0到n:for (int j = 0; j <= n; j)。
J | End | Start | Pattern of Start
---------------------------------------
0 | N | N | N - 0
---------------------------------------
1 | N | N - 1 | N - 1
---------------------------------------
2 | N | N - 2 | N - 2
---------------------------------------
... | ... | ..... | N - J
---------------------------------------
N | N | 0 | N - N
模式是:for (int i = n - j; i < nrows; i)。你會用n - j.
其次,您要填寫后半部分,即從列n 1到2n:for (int j = n 1; j < ncols; j)。
J | End | Start | Pattern of Start
------------------------------------------
N 1 | N | 1 | N 1 - N = J - N
------------------------------------------
N 2 | N | 2 | N 2 - N = J - N
------------------------------------------
N 3 | N | 3 | N 3 - N = J - N
------------------------------------------
..... | ... | ..... | N X - N = J - N
------------------------------------------
2N | N | N | 2N - N = J - N
模式是:for (int i = j - n; i < nrows; i)。你會用j - n.
現在,您可以執行兩個單獨for的回圈來填充前半部分和后半部分。
// Fill first half
for (int j = 0; j <= n; j) {
for (int i = n - j; i < nrows; i) {
matrix[i][j] = '0' n - j;
}
}
// Fill second half
for (int j = n 1; j < ncols; j) {
for (int i = j - n; i < nrows; i) {
matrix[i][j] = '0' j - n;
}
}
for您可以通過替換n - j和j - n來撰寫一個回圈abs(n - j),因為它們都是正值(參見上面的 TL;DR 或下面的代碼)。
完整功能代碼:
#include <stdio.h>
#include <stdlib.h>
void print_matrix(const int nrows, const int ncols, const char matrix[nrows][ncols])
{
for (int i = 0; i < nrows; i) {
for (int j = 0; j < ncols; j)
printf("%c ", matrix[i][j]);
printf("\n");
}
}
void init_matrix(const int nrows, const int ncols, char matrix[nrows][ncols], const char value)
{
for (int i = 0; i < nrows; i)
for (int j = 0; j < ncols; j)
matrix[i][j] = value;
}
void print_pyramid(const int n)
{
// Calculate the number of rows and columns
const int nrows = n 1;
const int ncols = n * 2 1;
char matrix[nrows][ncols];
// Initialization
init_matrix(nrows, ncols, matrix, '\0');
// Fill the matrix
for (int j = 0; j < ncols; j) {
for (int i = abs(n - j); i < nrows; i) {
matrix[i][j] = '0' abs(n - j);
}
}
// Printing
print_matrix(nrows, ncols, matrix);
}
int main(void)
{
print_pyramid(5);
}
uj5u.com熱心網友回復:
您必須在一個運行 6 次的 for 回圈中包含所有 3 個 for 回圈。首先,您必須列印空格。然后是三角形的前半部分,然后是另一半。之后,您必須列印\n.
#include<stdio.h>
int main()
{
int i, j,k,l;
for (i = 0; i <=5; i )
{
for (j = 5; j > i; j--)
{
printf(" ");
}
for (k = i; k >=0; k--)
{
printf("%d ", k);
}
for (l = 1; l <=i; l )
{
printf("%d ", l);
}
printf("\n");
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/450411.html
