Home > Back-end >  Help: c language to write the hourglass, partially true
Help: c language to write the hourglass, partially true

Time:03-20

Questions do something like this:
You are asked to write a program to bear on the given symbol print into the shape of an hourglass, given 17 "*", for example, requirements in the following format to print

* * * * *
* * *
*
* * *
* * * * *
"The hourglass shape", refers to the output an odd number of symbols per line; The center line all symbols; Adjacent two rows symbol number 2; Diminishing the number symbols from big to small order at first to 1, since the childhood again increasing order. The end is the same symbol.

Given any N symbols, can not form an hourglass, just asked to print out of the hourglass can drop as many symbols,

Input format:
Type in a line is given a positive integer N (1000) or less and a symbol, separated by Spaces in the middle,

The output format:
Print out the first largest hourglass shape consisting of a given symbol, the final output useless left off in a row the number of symbols,

Input the sample:
19 *
The output sample:
* * * * *
* * *
*
* * *
* * * * *
2
And my code is
 # include "stdio.h" 
Int main ()
{
Int n, I, u=0, j, sum=1, the count=0, len.
Char c;
The scanf (" % d % c ", & amp; N, & amp; C);
If (n==1)
{
Printf (" * \ n0 ");
Goto the EX.
}
If (n==2)
{
Printf (" * \ n1 ");
Goto the EX.
}
If (n> 1000 | | n<=0) return 0;
For (I=3; Sum<=n;)
{
Sum=sum + 2 * I;
I=I + 2;
count++;
}
J=n - (sum - 2 * (I - 2));
Len=I - 4;
for(i=0; i{
for(n=0; Nfor(n=0; Nprintf("\n");
Len=len - 2; U++;
}
Len=len + 4; U=u - 2;
for(i=1; i{
for(n=0; Nfor(n=0; Nprintf("\n");
Len=len + 2; U -;
}
Printf (" % d ", j);
EX:
return 0;
}

Out why the answer is partially correct ah, for help

CodePudding user response:

For your reference
 void test (int n, char * c) 
{
Int x=n/2;
Int num=SQRT (x);
Int I=0, j=0, k=0;

//part 1
For (I=num; I> 0; I -)
{
//space
for(k=0; K{
Printf (" ");
}
For (j=2 * I - 1; J> 0; J -)
{
Printf (" % c ", c);
}
printf("\n");
}
//part 2
for(i=1; i{
For (k=I; K{
Printf (" ");
}
for(j=0; J<2 * I + 1; J++)
{
Printf (" % c ", c);
}
printf("\n");
}


}


This call
The test (99, '*');

CodePudding user response:

Reference:
 # include "stdio.h" 
Int main ()
{
Int n, I, j, sum=0, count=1;//, len; , u=0
Char c;
The scanf (" % d % c ", & amp; N, & amp; C);
/* if (n==1)
{
Printf (" * \ n0 ");
Goto the EX.
}
If (n==2)
{
Printf (" * \ n1 ");
Goto the EX.
} */
If (n> 1000 | | n<=0) return 0;
For (I=3; Sum<=n;)
{
Sum=sum + 2 * I;
I=I + 2;
count++;
}
The count=count - 1;
//j=n - (sum - 2 * (I - 2));
//len=I - 4;
For (I=count; I>=1; I -)//upper
{
for(j=0; Jfor(j=0; J<2 * I - 1; J++) printf (" % c ", c);
printf("\n");
//len=len - 2; U++;
}
//len=len + 4; U=u - 2;
For (I=I + 2; i<=count;
lower i++)//{
for(j=0; Jfor(j=0; J<2 * I - 1; J++) printf (" % c ", c);
printf("\n");
//len=len + 2; U -;
}
Printf (" % d ", n - (2 * count * count - 1));
//printf (" % d ", j);
//the EX:

return 0;
}


//35 *
//* * * * * * *
//* * * * *
//* * *
//*
//* * *
//* * * * *
//* * * * * * *
//4 please press any key to continue...

CodePudding user response:

I go to a test run on the PTA, or like the original, part of the answer wrong, seems to be some kind of special case is not applicable

CodePudding user response:

Finally a printf didn't bring \ n?

CodePudding user response:

Only consider the format and special circumstances,
One can try finally have a newline,
Two can have a try if it is zero is not output,
  • Related