当前位置: 代码迷 >> C语言 >> [讨论]关于一个制造钻石型东西的问题
  详细解决方案

[讨论]关于一个制造钻石型东西的问题

热度:131   发布时间:2006-04-28 18:03:00.0
[讨论]关于一个制造钻石型东西的问题

#include<stdio.h>
main()
{
int num;

while (num != 0){
if (num == 0)
return 0;
printf("Enter a number (3-77):\t");
scanf("%ld", &num);
while (num>77 || num<3){
printf("Enter error!\n");
printf("Please enter a number(3-77):");
scanf("%d", &num);
}
for (int i=0; i<=(num-1)/2; i++)
{
for (int k=0; k<=num/2-i; k++)
printf(" ");
for (int j=0; j<=2*i; j++)
printf("*");
printf("\n");
}
for (int w=0; w<=num/2; w++){
for (int m=-1; m<=w; m++)
printf(" ");
for (int n=1; n<=(num-2)-2*w; n++)
printf("*");
printf("\n");
}
}
},请问里面i<=(num-1)/2,k<=num/2-i,k<=num/2-i,w<=num/2,m<=w分别代表什么???因为我把数字改了以后有不同结果,还有printf(" ");有什么作用,因为删除了整个图形就变了

搜索更多相关的解决方案: 钻石  制造  

----------------解决方案--------------------------------------------------------

你的程序有很多错误,


以下是引用louisapple在2006-4-28 18:03:00的发言:

#include<stdio.h>
main()
{
int num;

while (num != 0){   /*既然num不等于0,又干吗if(num==0)?*/
if (num == 0)
return 0;
printf("Enter a number (3-77):\t");
scanf("%ld", &num); /*再看下num是整形还是长整形?*/
while (num>77 || num<3){
printf("Enter error!\n");
printf("Please enter a number(3-77):");
scanf("%d", &num);
}
for (int i=0; i<=(num-1)/2; i++)
{
for (int k=0; k<=num/2-i; k++)
printf(" ");            /*该语句用来输出空格,调整图形的位置*/
for (int j=0; j<=2*i; j++)
printf("*");
printf("\n");
}
for (int w=0; w<=num/2; w++){      /*没见过在这里定义的*/
for (int m=-1; m<=w; m++)

printf(" ");
for (int n=1; n<=(num-2)-2*w; n++)
printf("*");
printf("\n");
}
}
},请问里面i<=(num-1)/2,k<=num/2-i,k<=num/2-i,w<=num/2,m<=w分别代表什么???因为我把数字改了以后有不同结果,还有printf(" ");有什么作用,因为删除了整个图形就变了

你把你想要输出的图形想一下,自然就知道为什么有i<=(num-1)/2,k<=num/2-i,k<=num/2-i,w<=num/2,m<=w这些了,


----------------解决方案--------------------------------------------------------
改了之后的代码,运行结果如图
#include<stdio.h>
main()
{
int num,i,k,j,w,m,n;
scanf("%ld", &num);
while (num != 0){
if (num == 0)
return 0;
printf("Enter a number (3-77):\t");
scanf("%ld", &num);
while (num>77 || num<3){
printf("Enter error!\n");
printf("Please enter a number(3-77):");
scanf("%d", &num);
}
for (i=0; i<=(num-1)/2; i++)
{
for (k=0; k<=num/2-i; k++)
printf(" ");
for (j=0; j<=2*i; j++)
printf("*");
printf("\n");
}
for (w=0; w<=num/2; w++){
for (m=-1; m<=w; m++)
printf(" ");
for (n=1; n<=(num-2)-2*w; n++)
printf("*");
printf("\n");
}
}
}

----------------解决方案--------------------------------------------------------

  相关解决方案