***算法:把菱形的中心看成坐标的原点(0,0),由此可以知道,如果|x| + |y| <= n;则打印输出"*"号,否则打印输出" "
int mai(){
int n;
printf("请输入来那个菱形边长:");
scanf("%d",&n);
//打印实心菱形
for (int x = -(n-1); x <= n-1; x++){
for (int y = -(n-1); y <= n-1; y++){
if(abs(x) + abs(y) <= n-1){
printf("*");
}else{
printf(" ");
}
}
printf("\n");
}
//打印空心菱形
for (int x = -(n-1); x <= (n-1); x++){
for (int y = -(n-1); x <= (n-1); y++){
if(abs(x) + abs(y) == (n-1)){
printf("*");
}else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
时间: 2024-10-08 20:02:59