1.杨辉三角问题
#include<stdio.h>
#include<string.h>
void yhRiangle(int height);
int main(int argc, char const *argv[])
{
yhRiangle(30);
return 0;
}
void yhRiangle(int height)
{
int riangle[height][height*2];
for (int i = 0; i < height; i++)
{
int lineLength = 0;
int isBegin = 1;
int isShow = 0;
for (int j = 0; j < height*2; j++)
{
int value = riangle[i-1][j-1]+riangle[i-1][j+1];
int lastFirstPointLocation[2];
int lastEndPointLocation[2];
if (isShow==1 && isBegin==0)
{
printf(" ");
riangle[i][j] = 0;
isShow=0;
}else{
isShow=1;
if (i==0)
{
if (j==height)
{
printf("1");
riangle[i][j] = 1;
lastFirstPointLocation[0] = j;
lastFirstPointLocation[1] = i;
lastEndPointLocation[0] = j;
lastEndPointLocation[1] = i;
lineLength++;
}else{
printf(" ");
riangle[i][j] = 0;
}
}
else if(lastFirstPointLocation[0]-1==j && lastFirstPointLocation[1]+1==i && isBegin==1)
{
printf("1");
riangle[i][j] = 1;
lastFirstPointLocation[0] = j;
lastFirstPointLocation[1] = i;
lineLength++;
isBegin = 0;
}
else if(lastEndPointLocation[0]+1==j && lastEndPointLocation[1]+1==i && lineLength<=i)
{
printf("1");
riangle[i][j] = 1;
lastEndPointLocation[0] = j;
lastEndPointLocation[1] = i;
lineLength++;
}
else if (value<=0)
{
printf(" ");
riangle[i][j] = value;
}else if(isBegin==0 && lineLength<i && value!=1)
{
printf("%d",value);
riangle[i][j] = value;
lineLength++;
}
}
}
printf("\n");
}
}
2.思维导图