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;
}
答案 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);