PTA L1-002 打印沙漏
The hardest thing is to name a variable.
#include<stdio.h>
int first_line_num=1;
int line=1;
int all=1;
void get_line_and_first(int);
void print_out(char);
int main(void)
{
int num_input;
char symble;
if (scanf("%d %c",&num_input,&symble)==2)
;
else
return 0;
get_line_and_first(num_input);
print_out(symble);
printf("%d\n",num_input-all);
return 0;
}
void get_line_and_first(int num_input)
{
int add_line=6;
while(all<=num_input)
{
all+=add_line;
add_line += 4;
line+=2;
}
add_line -=4;
all -= add_line;
line -= 2;
first_line_num = line;
}
void print_out(char symble)
{
int i;
int l=1;
int blank,sym;
while(l<=(line/2+1))
{
blank = l-1;
sym = first_line_num - blank*2;
for (i=0;i<blank;i++)
printf(" ");
for (i=0;i<sym;i++)
printf("%c",symble);
printf("\n");
l++;
}
while(l<=line)
{
blank = first_line_num/2-1-(l-line/2-2);
sym = first_line_num - blank*2;
for (i=0;i<blank;i++)
printf(" ");
for (i=0;i<sym;i++)
printf("%c",symble);
printf("\n");
l++;
}
}