画 空心菱形

Home Home
引用 | 编辑 johnroyer
2005-10-13 18:21
楼主
推文 x0
用 * 号画出一个空心的菱形


#include <stdio.h>
#include <stdlib.h>
int main()
{
int a=0,b=0,chk=1,h;
printf("input h:");
scanf("%d",&h);
for(a=0;a<h;a++) printf(&q ..

访客只能看到部份内容,免费 加入会员



献花 x2
引用 | 编辑 mmpo
2005-10-14 23:06
1楼
  
要比短是吧!~~~~
这个不用for和while也照画....

#include <stdio.h>
#include <stdlib.h>

void test(int max,int now){
if(now==-max)
  return ;
test(max,now-1);
printf("%.*s",now<1?-now:now,"       ");
printf("%.*s \n",(max-(now<1?-now:now))*2,"* * * * * * * * * * * ");  
}

int main(){
int n=8;
test(n,n);
system("pause");
}

献花 x2
引用 | 编辑 柠檬茶
2005-11-14 15:13
2楼
  
我这个是按照上面那位大大所法表的
多加一些让它能做更多种输入变化

#include <stdio.h>
#include <stdlib.h>

void test(int max,int now){
if(now==-max)
return ;
test(max,now-1);
printf("%.*s",now<1?-now:now,"     ");
printf("%.*s \n",(max-(now<1?-now:now))*2,"* * * * * * * * * * * ");  
}

int main(){
int n;
printf("input n:");
  scanf("%d",&n);
test(n,n);
system("pause");
}

献花 x0
引用 | 编辑 youchun
2005-11-14 17:18
3楼
  
如果真的要动态决定数目的话
需要动态产生所需 " " 和 "* " 的数目
不然也可以偷机, 给使用者一定不会超过的数目
复制程式
#include <stdio.h>
#include <stdlib.h>

char * space;
char * star;
void test(int max,int now){
  if(now==-max)
    return ;
  test(max,now-1);
  printf("%.*s",now<1?-now:now, space);
  printf("%.*s \n",(max-(now<1?-now:now))*2, star);
}

int main(){
  int n, i;
  printf("input n:");
  scanf("%d",&n);
  space = (char*)malloc(sizeof(char)*(n + 1));
  star  = (char*)malloc(sizeof(char)*(2*n + 1));
  for(i = 0; i < n; i++) {
    space[i] = 32; // " "
    star[2*i + 1] = 32; // " "
    star[2*i] = 42; // "*"
  }
  space[n] = 0;
  star[2*n] = 0;
  test(n,n);
  free(space);
  free(star);


献花 x0