打印空星三角Ç [英] print empty asterisk triangle c

查看:115
本文介绍了打印空星三角Ç的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我抬头一看,才发现,正常的三角形问题在那里。
这一个是有点棘手。


  

给定一个N,你的程序应该创建A N边尺寸星号镂空的三角形。


 例1
通知N:1
*实施例2:
通知N:2
**
*
实施例3:
通知N:3
***
**
*
实施例4:
通知N:4
****
* *
**
*
实施例5:
通知N:5
*****
* *
* *
**
*

下面是我的尝试,我只能做一个实心三角形低效

 无效Q39(){
    INT N,I,B,
    的printf(通知N:);
    scanf函数(%i的,&安培; N);
    对于(i = 0; I< N ++ I)
    {
    的printf(*);
    对于(B = 1; B< N ++ B)
    {
        如果(我== 0)
        {
        的printf(*);
        }
        否则如果(ⅰ== 1和;和b→1)
        {
            的printf(*);
        }
        否则如果(ⅰ== 2及和b→2)
        {
            的printf(*);
        }
        否则如果(ⅰ== 3及和b→3){
            的printf(*);
        }
        否则如果(ⅰ== 4和和b→4){
            的printf(*);
        }
        否则如果(ⅰ== 5和;&放大器b取代; 5){
            的printf(*);
        }
        否则如果(ⅰ== 6和;和b→6){
            的printf(*);
        }
        否则如果(ⅰ== 7和;和b大于7){
            的printf(*);
        }
        否则如果(ⅰ== 8和;和b→8){
            的printf(*);
        }
    }
        的printf(\\ n);
    }}


解决方案

您只需要觉得第一行应该充满 * 。结果
第二件事是每一行的第一个字符应该是 * 。结果
而最后一个字符也应该是 * 。在两者之间,你需要填写的空格。

  INT的main()
{
    INT N = 6;
    对(INT I = N-1; I> = 0;我 - )//使用N- 1,BCZ循环运行高达0
    {
        对于(INT J = 0; J< = I; J ++)
        {
            如果(我== N-1 ||Ĵ== 0 ||我== j)条
                的printf(*);
            其他
                的printf();
        }
        的printf(\\ n);
    }
    返回0;
}

状况如果(我== N-1 ||Ĵ== 0 ||我== j)条结果
这里我== N-1 用来让第一线应该充满 * 。结果
Ĵ== 0 是用来使每行 * 的第一个字符。每次当新的生产线开始即J = 0,将打印一个 * 字符时间。结果
我==Ĵ这是用来做最后一个字符 * 当我==Ĵ这是最后一个索引高达我们正在运行的循环。所以最后指数将打印 * 。结果
而对于所有其他值将打印空间,它将运行其他的条件。

输出

  ******
* *
* *
* *
**
*

So I've looked up and only found normal triangles questions out there. This one is a bit tricky.

Given a N, your program should create an asterisk unfilled triangle with a N side size.

Example 1 
Inform N: 1 
*

Example 2:
Inform N: 2
**
*
Example 3:
Inform N: 3
***
**
*
Example 4:
Inform N: 4
****
* *
**
*
Example 5:
Inform N: 5
*****
*  *
* *
**
*

Here's my attempt, I could only make a filled triangle inefficiently

void q39(){
    int n,i,b;
    printf("Inform N: ");
    scanf ("%i",&n);
    for ( i = 0; i < n; ++i)
    {
    printf("*");
    for ( b = 1; b < n; ++b)
    {
        if (i==0)
        {
        printf("*");
        }
        else if (i==1 && b>1)
        {
            printf("*");
        }
        else if (i==2 && b>2)
        {
            printf("*");
        }
        else if(i==3 && b>3){
            printf("*");
        }
        else if(i==4 && b>4){
            printf("*");
        }
        else if(i==5 && b>5){
            printf("*");
        }
        else if(i==6 && b>6){
            printf("*");
        }
        else if(i==7 && b>7){
            printf("*");
        }
        else if (i==8 && b>8){
            printf("*");
        }
    }
        printf("\n");
    }

}

解决方案

you just need to think that first line should be filled with *.
Second thing is first character of every line should be *.
and last character should also be *. and in between you need to fill spaces.

int main()
{
    int n=6;
    for(int i=n-1;i>=0;i--) // using n-1, bcz loop is running upto 0
    {
        for(int j=0;j<=i;j++)
        {
            if(i==n-1 || j==0 ||i==j)
                printf("*");
            else
                printf(" ");
        }
        printf("\n");      
    }
    return 0;
}

The condition if(i==n-1 || j==0 ||i==j)
here i==n-1 is used so that first line should be filled with *.
j==0 is used to make first character of every line *. every time when new line starts i.e j=0 it will print one * character.
i==j this is used to make last character * when i==j that is last index upto which we are running loop. so at last index it will print a *.
And for all other values it will print space as it will run else condition.

OUTPUT

******
*   *
*  *
* *
**
*

这篇关于打印空星三角Ç的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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