使用for循环绘制三角形

时间:2014-11-07 15:43:06

标签: c loops for-loop

这是我的问题。 仅使用字符*和+,您需要形成宽度为m且高度为n(m http://prntscr.com/53xv5s

的示例
#include <stdio.h>

int main ()
{
int m, n, i, j;
scanf ("%d %d", &m, &n);
for (i=0; i<m; i++)
{
    printf ("+");
    for (j=n-1; j>0; j--)
    {
        printf ("*");            
    }
    printf ("\n");    
}    
return 0;
}

有关如何修复它的任何帮助?到目前为止,我得到的是这个 http://prntscr.com/53xvq2

2 个答案:

答案 0 :(得分:1)

这应该适合你:

#include <stdio.h>

int main () {
    int row, column, rowCount, columnCount;

    printf("Enter row and column length: \n>");
    scanf (" %d %d", &row, &column);

    for (rowCount = 0; rowCount < row; rowCount++) {

        for (columnCount = 0; columnCount < column; columnCount++) {

            if(columnCount <= rowCount)
                printf ("+");
            else
                printf ("*");
        }

        printf ("\n");
    }

    return 0;

}

答案 1 :(得分:0)

尝试:

#include <stdio.h>
int main ()
{
int m, n, i, j;
scanf ("%d %d", &m, &n);
for (i=0; i<m; i++)
{
    for (j=0; j<n; j++)
    {
        if(j <= i)
            printf ("+");
        else
            printf ("*");
    }
    printf ("\n");
}
return 0;
}

这样它将始终打印n个字符和n行 每一行都以i +&#39开头,以*

结尾
相关问题