在C代码中打印底部带有*的倒三角形

时间:2017-10-24 06:30:46

标签: c

抱歉,我再次需要帮助。我收到了一份需要在屏幕上打印的作业。但是,我必须满足各种要求。说实话,我真的卡住,因为当我在线检查时,我必须考虑空间,它不会告诉我如何编辑三角形底部的小尖端到另一个。这就是我所做的,但考虑到我必须做的案件的数量,它真的很疯狂,我不认为它可行。请帮忙!非常感谢你。

1)顶部基本长度必须是用户输入,最大数量最多为79(基本长度= 79)。

2)三角形必须是对称的。

xxxxxxxxx
 xxxxxxx
  xxxxx
   xxx
    *

#include<stdio.h>

int main()
{
int i, space, base_length, star;
printf("Enter the base length:\n");
scanf("%d",&base_length);

if (base_length==15)
{
    printf("XXXXXXXXXXXXXXX\n");
    printf(" XXXXXXXXXXXXX\n");
    printf("  XXXXXXXXXXX\n");
    printf("   XXXXXXXXX\n");
    printf("    XXXXXXX\n");
    printf("     XXXXX\n");
    printf("      XXX\n");
    printf("       *\n");
}
else if (base_length==13)
{
    printf(" XXXXXXXXXXXXX\n");
    printf("  XXXXXXXXXXX\n");
    printf("   XXXXXXXXX\n");
    printf("    XXXXXXX\n");
    printf("     XXXXX\n");
    printf("      XXX\n");
    printf("       *\n");
}
else if (base_length==11)
{
    printf("  XXXXXXXXXXX\n");
    printf("   XXXXXXXXX\n");
    printf("    XXXXXXX\n");
    printf("     XXXXX\n");
    printf("      XXX\n");
    printf("       *\n");
}

else if (base_length==9)
{
    printf("   XXXXXXXXX\n");
    printf("    XXXXXXX\n");
    printf("     XXXXX\n");
    printf("      XXX\n");
    printf("       *\n");
}

else if (base_length==7)
{
    printf("    XXXXXXX\n");
    printf("     XXXXX\n");
    printf("      XXX\n");
    printf("       *\n");
}

else if (base_length==5)
{
    printf("     XXXXX\n");
    printf("      XXX\n");
    printf("       *\n");
}

else if (base_length==3)
{
    printf("      XXX\n");
    printf("       *\n");
}

else if  (base_length==1)
{
    printf("       *\n");
}
else
{
    printf("The maximum base length permitted is 79 and has to be an odd number.\nPlease try again with a valid number.\nThank You!!!");
}

return;

}

2 个答案:

答案 0 :(得分:0)

注意模式并用迭代替换它。

像这样

#include <stdio.h>

int main(void){
    int i, base_length, space, x;
    int bias_space = 7, rows;
    const char *X79 = //Prepare in advance because the maximum length is fixed
        "XXXXXXXXXX"
        "XXXXXXXXXX"
        "XXXXXXXXXX"
        "XXXXXXXXXX"
        "XXXXXXXXXX"
        "XXXXXXXXXX"
        "XXXXXXXXXX"
        "XXXXXXXXX";

    printf("Enter the base length:\n");
    scanf("%d", &base_length);

    if(base_length < 1 || base_length > 79 || (base_length & 1) == 0){
        printf("The maximum base length permitted is 79 and has to be an odd number.\nPlease try again with a valid number.\nThank You!!!");
        return -1;
    }
    rows = base_length / 2;//Number of rows excluding the top
    bias_space -= rows;
    if(bias_space < 0) //When there are more than 7 lines, there is no bias space.
        bias_space = 0;

    for(i = rows, space = 0, x = base_length; i--; space++, x -= 2)
        printf("%*s%.*s\n", bias_space + space, "", x, X79);
    printf("%*s%c\n", bias_space + space, "", '*');
}

答案 1 :(得分:-1)

您可以使用带帮助功能的简单递归函数:

$t = array();         
foreach($selectdata as $r)
{
    $t[] = $r->package_id; 
}
$explode =  explode(",",$t);
echo "<pre>";
print_r($explode);