Home > Back-end >  To print the hourglass, part is correct, please advice was less? Looking forward to learning good co
To print the hourglass, part is correct, please advice was less? Looking forward to learning good co

Time:06-02

Print the hourglass
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

 
#include
The main ()
{
Int N;
Char s;
int sum=0;
Int I=3;
Int j, a;
Int CNT=0;
The scanf (" % d % c ", & amp; N, & amp; S);

While (N> 6 & amp; & (N - 1)/2 - sum>=2) {
The sum +=I;
I +=2;
}
If (N<7) sum=1;
//output hourglass
M=I;
A=I - 2;
While (m> 1) {
M=m - 2;
If (cnt> 0 {
for(i=0; iprintf(" ");
}
}
for(i=1; i<=m; I++) {
Printf (" % c ", s);
}
printf("\n");
Cnt++;
}
CNT=CNT - 2;
While (mM=m + 2;//3
If (cnt> 0 {
for(i=0; iprintf(" ");
}
}
for(i=1; i<=m; I++) {
Printf (" % c ", s);
}
printf("\n");
CNT -;
}
If (N> 6) printf (" % d ", ((N - 1)/2 - sum) * 2 + (N - 1) % 2);
The else printf (" % d ", N - 1);
}

CodePudding user response:

You enter 103 * run it

CodePudding user response:

48 * 70 * 16 * such data
The output is 1
That is not the only mistake, 45 * data also can appear similar mistakes such as
Obviously there is something wrong with the formula above

Assuming that the hourglass bottom width is n=2 * n - 1
So, the known formula:
1 + 3 + 5 +... + 2 * N - 1=N ^ N
So, complete the hourglass character dosage is 2 * N * N - 1

For integer n input program, int l=SQRT ((n + 1)/2) knew the bottom width of the hourglass: 2 * l - 1
After all the output, the rest of the number of characters is: n - (2 * l * l - 1) remaining

Such as n=48 l=SQRT (49/2)=4 hourglass bottom width is 7, the remaining 48 - (32-1)=17
With these data, drawing and output directly by the

CodePudding user response:

#include
#include

Void SandGlass (int n, char ch) {
Int level=SQRT ((n + 1)/2);
Int I, j, len=2 * level - 1;
For (I=0; ifor (j=0; Jfor (j=0; Jprintf("\n");
}
Len=3;
For (I=1; ifor (j=0; Jfor (j=0; Jprintf("\n");
}
Printf (" % d ", n - 2 * level * level + 1);
}
The main ()
{
int n;
Char ch='*';
The scanf (" % d % c ", & amp; N, & amp; Ch);
Int level=SQRT ((n + 1)/2);
Int u=* * level 1, level 2 r=n - u;
Printf (" n=% d, level=% d, 2: % d, Remain: % d \ n ", n, level, u, r);
SandGlass (n, ch);
}

CodePudding user response:

I did out, thank you for the great god, the first while conditions to the while (N> 6 & amp; & (N - 1)/2 - sum>=) I just by the way, so happy,
A while reading your posts,

CodePudding user response:

I want to ask what is commonly used in the numerical code to test? Using classification to discuss inspection or the conditions of boundary value is? Usually with several groups of the same kind of situation? I this topic is, when the test data, cause I haven't found out the mistake,

CodePudding user response:

No fixed pattern, owing to the different problems, to construct different test data is in itself a separate proposition

On this topic, you set a loop, analog input,
Detection such as 10 *, 11 *,,,,,,, 50 * that input and output

CodePudding user response:

1 + 3 + 5 +... + 2=N * N * N - 1?
Int l=SQRT ((n + 1)/2) knew the bottom width of the hourglass: 2 * l - 1; L is how out? Bottom width come from? thank you

CodePudding user response:

Junior high school of mathematical formula,
1=1 * 1
1 + 3=2 * 2
1 + 3 + 5=3 * 3
,,,,,,


With this formula, n * n=1 + 3 + 5 +... + 2 n - 1
The hourglass is a reverse, public a 1, the total is not m (n)=2 * n * n - 1

Back calculation is, n (m)=SQRT ((n + 1)/2)

CodePudding user response:

Thank you, I understand, the application formula is much better than myself is do,
I also learned to your cycle test "" haha,
Can I trouble you to explain the following code of this one?
 
#include
#include
The main ()
{
int i;
Int j;
int n;
Int [101] s={0};
//array initialization
For (I=1; i<101; I++) {
S [I]=I;
}
//determine primes
S [1]=0;
For (I=2; iFor (j=I + 1; J<101; J++) {
If (s [I]!=0 & amp; & S [j].=0) {//why mark s [I] here!=0 & amp; & S [j].=0? In fact they also would not make zero?
If (s [I] [j] % s==0) {
S [j]=0;
}
}

}
}
//output
For (I=0, n=0; i<101; I++) {
If (s [I]!=0) {
Printf (" % 5 d, "s [I]);
n++;
}
{if (n==10)
printf("\n");
}
}

}

CodePudding user response:

Screening method of prime Numbers within 100

CodePudding user response:

Because in the past of the cycle of s assignment operation [j]=0, again into the, s [?] And s [j] could be 0
Who says this is a sieve method of prime number? !

Sieve method of prime number is not like this, that's the way the loop fill labeling, no modulus operation such as

Knowledge base inside prime subject yourself to baidu, baidu, I remember there is a sieve method of prime number theory and the original code

CodePudding user response:

S [1]=0; But the value of j start from 3 ah,,,
In the book to see,
nullnullnullnullnullnullnullnullnullnull
  • Related