如何在数字统一的地方打印正方形? [英] How to print an square where the numbers grow an unity?

查看:61
本文介绍了如何在数字统一的地方打印正方形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午好,我想知道如何打印一个正方形,其中数字变成一个整体,如下所示(取决于正方形线的数量,在这种情况下为5条线):

Good afternoon, I want to know how can I print an square where the numbers grow an unity, as the following (depending of the number of square lines, in this case, 5 lines):

00000
01110
01210
01110
00000



如何使用C ++/C 来做到这一点?

最好的问候.
ÁngelManuel.

[注意:我试图通过Google进行查找,但找不到任何答案]



How can I do that by C++/C?

Best regards.
Ángel Manuel.

[NOTE: I''ve tried to found that by Google but I can''t found any answer]

推荐答案

这是另一种解决方案,这次是非递归的(即动态的):
Here is another solution, this time non-recursive (i.e dynamic):
#include <stdio.h>

#define MAX (5-1)
int gSquare [MAX+1][MAX+1];

/* copy a source row to destination row */
void cpRow (int dstRow,int srcRow){
    for (int col=0; col<= MAX; col++)
        gSquare[dstRow][col]= gSquare[srcRow][col];
}


/* fill all the cells of a given row with zeros */
void ZerFil (int row){
    for (int col=0; col<= MAX; col++)
        gSquare[row][col]=0;
}
/* Print out a given row on screen */
void PrnRow (int row){
    for (int col=0;col<=MAX;col++)
        printf ("%d\t",gSquare [row][col]);
    printf ("\n");
}

/* Dynamically define each rows values based on previous rows */
void CalcRow (int row){
    if (2 * row > MAX+1) {/* we are on lower half */
        /* copy symmetrically ~ mirror flip */
        cpRow(row, MAX-row);
        return;
    }

    cpRow (row, row-1); /* Initiate me as previous row */
    /* increase required cells */
    for (int i=row; i<= MAX-row; i++) 
        gSquare[row][i]++;
}

int main() {
    /* Zero the initial row as zero for base of calculation */
    ZerFil(0);
    PrnRow (0);

    for (int row=1; row<=MAX;row++){
        CalcRow (row);
        PrnRow (row);
    }
    return 0;
}


void printSquare(int sideLength)
{
    int curCol, curRow;
    int val;
    for (curRow=0; curRow<sideLength; curRow++)
    {
        for (curCol=0; curCol<sideLength; curCol++)
        {
            /* CALCULATE val HERE
            ...
            ...
            ...
            */

            printf("%d",val);
        }
        printf("\n");
    }
}


这可以如下递归进行:
This can be done recursively as follows:
#include <stdio.h>

int padOffset (int row, int n){
    int off1 = row - n;
    if (off1 < 0)
        off1 = off1 * (-1);
    return(row<off1)? row:off1;
}
int diffCount (int row, int col, int n){
    int loc1 =  padOffset(row, n);
    int loc2 = n - loc1;
    int dif = (loc1 ==row)?1:-1; //determine sign
    if (dif<0) {loc1++; loc2--;} //strechen halfway down
    if (loc1> col || col > loc2)
        dif=0;
    return dif;
}
int getCell (int row, int col, int n){
    if (row <= 0)
        return row;
    int inc = diffCount(row,col,n);
    return getCell(row-1,col,n)+ inc;
}
int main() {
    int n;
    printf ("Enter n:");scanf ("%d",&n);
    n--; // indexing at zero!
    for (int row=0; row<=n;row++){
        for (int col=0;col<=n;col++)
            printf ("%d\t",
            getCell(row,col,n));
        printf ("\n");
    }
    return 0;
}


这篇关于如何在数字统一的地方打印正方形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆