1, String type

[1] Solid graphics (1-5,12)

1. Input n value and output rectangle as shown in the figure

```*****
*****
*****
*****
*****
```
```#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
for(int i=0 ; i<n ; i++) {
for(int j=0 ; j<n ; j++)
cout<<"*";
cout<<endl;
}
return 0;
}
```

2. Input n value and output parallelogram as shown in the figure

```        * * * * *
* * * * *
* * * * *
* * * * *
* * * * *
```
```#include <stdio.h>
int main() {
int n;
scanf("%d",&n);
for(int i=0 ; i<n ; i++) {
for(int j=n-i-1; j>0 ; j--)
printf("%2c",'\0');
for(int j=0 ; j<n ; j++)
printf("%-2c",'*');
printf("\n");
}
return 0;
}
```

3. Input n value and output isosceles triangle with height n as shown in the figure

```   		*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
```
```#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
for(int i=1 ; i<=n ; i++) {
for(int j=0 ; j<n-i ; j++)
printf("%2c",' ');
for(int j=0 ;j<2*i-1 ; j++)
printf("%-2c",'*');
cout<<endl;
}
return 0;
}
```

4. Input n value and output the inverted isosceles triangle with height n as shown in the figure

```* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
```
```#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
for(int i=n ; i>0 ; i--) {
for(int j=0 ; j<n-i ; j++)
printf("%2c",'\0');
for(int j=0 ;j<2*i-1 ; j++)
printf("%-2c",'*');
cout<<endl;
}
return 0;
}
```

5. Input n value and output isosceles trapezoid with n height and upper bottom as shown in the figure

```		* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * * *
```
```#include <iostream>
using namespace std;
int main() {
/*int n;
cin>>n;
for(int i=3; i<=n+2 ; i++) {
for(int j=0 ; j<n-i+2; j++)
cout<<"  ";
for(int j=0 ;j<2*i-1 ; j++)
cout<<"* ";
cout<<endl;
}*/ //  Error, the number of * in the first line will change with the change of n. it is not a constant and cannot be calculated directly from i=3
int n;
cin>>n;
for(int i=1 ; i<=n; i++) {
for(int j=1 ; j<=n-i ; j++)
cout<<"  ";
for(int j=1 ; j<=2*i-1+(n-1); j++) {
if(j == 1)
cout<<"*";
else
cout<<" *";
}
cout<<endl;
}
return 0;
}
```

Note: this type of figure is most likely to only look at the sample output graphics. If the title requires n to be a constant, this can be done. Otherwise, it is very easy to make mistakes, and the changes of graphics with the change of N are often ignored. The distance between lines is equivalent to a space, so the distance between each line * is an empty line

12. Input n value and output diamond

``` 	*
* * *
* * * * *
* * *
*
```
```#include <iostream>
using namespace std;
int main() {
void f(int n , int i);
int n;
cin>>n;
for(int i=1 ; i<=n ; i++ )
f(n,i);
for(int i=n-1; i>0 ; i-- )
f(n,i);
return 0;
}
void f(int n , int i) {
for(int j=1 ; j<=n-i ; j++)
cout<<"  ";
for(int j=1 ; j<=2*i-1 ; j++) {
if(j == 1)
cout<<"*";
else
cout<<" *";
}
cout<<endl;
}
```

[2] Hollow figure (6-11,13) [string type]

6. Input n value and output isosceles hollow trapezoid with height and upper bottom of n as shown in the figure

``` 		  * * * * * *
*             *
*                 *
*                     *
*                         *
* * * * * * * * * * * * * * * *
```
```#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
for(int i=1; i<=n ; i++) {
for(int j=0; j<n-i; j++)
cout<<"  ";
for(int j=1 ;j<=2*i-1+(n-1) ; j++) {
if(i==1 || i==n) {
if(j == 1)
cout<<"*";
else
cout<<" *";
}
else {
if(j==1)
cout<<"* ";
else if(j == 2*i-1+(n-1) )
cout<<"*";
else
cout<<"  ";
}
}
cout<<endl;
}
return 0;
}
```

7. Input n value and output the hollow regular hexagon with side length n as shown in the figure

```  	 * * * * * *
*           *
*             *
*               *
*                 *
*                   *
*                 *
*               *
*             *
*           *
* * * * * *
```
```#include <iostream>
using namespace std;
int main() {
void f(int n,int i);
int n;
cin>>n;
for(int i=1; i<n; i++)
f(n,i);
f(n,n);
for(int i=n-1; i>0; i--)
f(n,i);
return 0;
}
void f(int n,int i) {
for(int j=0; j<n-i; j++)
cout<<" ";
for(int j=1 ;j<=2*i-1+(n-1) ; j++) {
if(i==1) {
if(j==1)
cout<<"*";
else
cout<<" *";
}
else {
if(j==1 || j==2*i-1+(n-1))
cout<<"*";
else {
if(j<=i || j>i+n-1 )
cout<<" ";
else
cout<<"  ";
}
}
}
cout<<endl;
}
return 0;
}
```

8. Input n value and output X graph

```*         *
*       *
*     *
*   *
* *
*
* *
*   *
*     *
*       *
*         *
```
```#include <iostream>
using namespace std;
int main() {
void f(int n ,int i);
int n;
cin>>n;
for(int i=n ; i>1 ; i--)
f(n,i);
for(int i=1 ; i<=n ; i++)
f(n,i);
return 0;
}
void f(int n ,int i) {
for(int j=1 ; j<=n-i; j++)
cout<<" ";
for(int j=1 ; j<=2*i-1 ; j++) {
if(j == 1 || j ==2*i-1 )
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
return 0;
}
```

Note: for the figure with upper and lower symmetry, the general practice is to divide it into upper part, middle part and lower part. For the upper and lower parts, because of their symmetry, many duplicate codes will be generated. At this time, you can customize functions to deal with duplicate codes. For the middle part, sometimes it can be written in the upper part (lower part), and sometimes it needs to be analyzed separately. At this time, we must look at the sample analysis.

9. Input n value and output Z graph

```* * * * *
*
*
*
* * * * *
```
```//array
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
char s[n][2*n-1];
for(int i=0 ; i<n ; i++) {
for(int j=0 ; j<2*n-1 ; j++) {
if(i == 0 || i == n-1) {
if(j%2 == 0) {
s[i][j]='*';
cout<<s[i][j];
}
else {
s[i][j]=' ';
cout<<s[i][j];
}
}
else {
if(j == 2*(n-1-i)) {
s[i][j]='*';
cout<<s[i][j];
}
else {
s[i][j]=' ';
cout<<s[i][j];
}
}
}
cout<<endl;
}
return 0;
}
```
```//Non array
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
for(int i=1 ; i<=n ; i++) {
for(int j=1 ; j<=n ; j++) {
if(i == 1 || i == n) {
if(j == 1)
cout<<"*";
else
cout<<" *";
}
else {
if(j == n-i+1)
cout<<"*";
else
cout<<"  ";
}
}
cout<<endl;
}
return 0;
}
```

Note: the output of graphics can also be processed with arrays. Sometimes the processing will be simple, and sometimes the processing will be very complicated. The specific requirements depend on the subject.

10. Input n value and output K graph

```*     *
*   *
* *
*
* *
*   *
*     *
```
```#include <iostream>
using namespace std;
int main() {
void f(int n, int i);
int n;
cin>>n;
for(int i=n ; i>1 ;i--)
f(n,i);
for(int i=1 ; i<=n ;i++)
f(n,i);
return 0;
}
void f(int n, int i) {
for(int j=1 ; j<=i ; j++) {
if(j == 1)
cout<<"* ";
else if(j == i)
cout<<"*";
else
cout<<"  ";
}
cout<<endl;
}

```

11. Input N value and output N graph

```*    *
**   *
* *  *
*  * *
*   **
*    *
```
```#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
char s[n][n];
for(int i=0 ; i<n ; i++) {
for(int j=0 ; j<n ; j++) {
if(j == 0 || j == n-1 || j == i) {
s[i][j]='*';
cout<<s[i][j];
}
else {
s[i][j]=' ';
cout<<s[i][j];
}
}
cout<<endl;
}
return 0;
}
```

13. Input n value and output inverted V figure

```    *
* *
*   *
*     *
*       *
```
```#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
for(int i=1 ; i<=n ; i++) {
for(int j=1 ; j<=n-i; j++)
cout<<" ";
for(int j=1 ; j<=2*i-1 ; j++) {
if(j == 1 || j ==2*i-1 )
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
return 0;
}
```

2, Digital pattern

[1] Solid pattern

17. The output number forms a rectangle (each line is added from left to right, and the end of each line is not set to 0)

``` 1   2   3   4   5   6
7   8   9  10  11  12
13  14  15  16  17  18
19  20  21  22  23  24
25  26  27  28  29  30
31  32  33  34  35  36

```
```#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
int k=0;
for(int i=0; i<n ; i++ ) {
for(int j=0 ; j<n ; j++) {
k++;  //Do not set 0
printf("%4d",k);
}
cout<<endl;
return 0;
}

```

18. Output the figure of digital inverted triangle

```  1  3  6 10 15 21
2  5  9 14 20
4  8 13 19
7 12 18
11 17
16

```
```#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
int k=1;
for(int i=1 ; i<=n ; i++) {
int m=k; //k is the value of the first element in line i
for(int j=1 ; j<=n-i+1 ; j++) {
printf("%3d",m);
m+=i+j; //Calculates the value of the next element in a row
}
k+=i;  //Calculates an element in the next row
cout<<endl;
}
return 0;
}
```

Analysis: the key to this problem is to find the relationship between the output number and the number of rows and columns. Review the relationship between the numbers in each row in the graph and find that:
(1) The difference between the right number and the previous number increases by 1 step;
(2) The number in the same column is still such a relationship. The key of programming is to find the first number on the left of each row, and then use the cyclic variables of row and column to calculate to get the number at each position. use
a i j a_{ij} aij = second i i Line i j j The number in column j, then a 11 = 1 a_{11}=1 a11​=1； By the first i i Line i 1 1 The figures in column 1 are introduced into the second column
i + 1 i+1 Line i+1 1 1 The number in column 1 is a i + 1 ， a i = 1 ， 1 + i a_i+1 ，a_i=1 ，1+i ai​+1，ai​=1，1+i； Also by section j j Column j introduces the second column j + 1 j+1 The number in column j+1 is a i , j + 1 = a i , j + i + j a_i,j+1 = a_i,j+i+j ai​,j+1=ai​,j+i+j. In addition, only when j < i j<i The number is output only when J < I.

19. Input n value and output special digital rectangle

```  1  2  3  4  5
1  1  2  3  4
1  1  1  2  3
1  1  1  1  2
1  1  1  1  1
```
```#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
int k=1;
for(int i=0; i<n ; i++) {
int m=k; //Represents the first element of each line
for(int j=0 ; j<n ; j++) {
if(j<=i)
printf("%3d",k);
else {
m++;
printf("%3d",m);
}
}
cout<<endl;
}
return 0;
}
```

22. Output the digital pyramid as shown in the figure

```                 1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
```
```#include <iostream>
using namespace std;
int main() {
int k=1;
for(int i=1 ; i<=9 ; i++) {
int m=k;
for(int j=1 ; j<=9-i ; j++)
cout<<"  ";
for(int j=1 ; j<=2*i-1 ; j++) {
if(j<i)
printf("%-2d",m++);
/*Equivalent code: if (J < I){
printf("%-2d",m);
m++;
}
*/
//Therefore, when j == i, no 1 is added when entering the next statement, so if (J < I) is not if (J < = I)
else
printf("%-2d",m--);
}
cout<<endl;
}
return 0;
}
```

27. Output the multiplication table in the form of upper triangle as shown in the figure

``` 1  2  3  4  5  6  7  8  9
----------------------------
1  2  3  4  5  6  7  8  9
4  6  8 10 12 14 16 18
9 12 15 18 21 24 27
16 20 24 28 32 36
25 30 35 40 45
36 42 48 54
49 56 63
64 72
81

```
```#include <iostream>
using namespace std;

int main() {
for(int i=1; i<10 ; i++)
printf("%3d",i);
cout<<endl;
cout<<" ----------------------------\n";
for(int i=1 ; i<=9 ; i++) {
for(int j=1 ; j<=9 ; j++) {
if(i<=j)
printf("%3d",i*j);
else
printf("%3c",'\0');
}
cout<<endl;
}
return 0;
}

```

28. Output the lower triangle multiplication table as shown in the figure

```  1  2  3  4  5  6  7  8  9
----------------------------
81
64 72
49 56 63
36 42 48 54
25 30 35 40 45
16 20 24 28 32 36
9 12 15 18 21 24 27
4  6  8 10 12 14 16 18
1  2  3  4  5  6  7  8  9
```
```#include <iostream>
using namespace std;

int main() {
for(int i=1; i<10 ; i++)
printf("%3d",i);
cout<<endl;
cout<<" ----------------------------\n";
for(int i=9 ; i>0 ; i--) {
for(int j=1 ; j<=9 ; j++) {
if(i<=j)
printf("%3d",i*j);
else
printf("%3c",'\0');
}
cout<<endl;
}
return 0;
}
```
Analysis of printf ('% MC','character ');:

When dealing with spaces, we can use a null character ('\ 0') to replace spaces, which is a new approach; when using% mc, we can reduce unnecessary space input. For example, the output "*" can be replaced by% - 2c, which can reduce the space before the first character in each line and reduce unnecessary input

[2] Hollow pattern

24. Input the height of the top line character and figure, and output diamond

```  		A                       A
B	  B					   B B
C		C				  C   C
D           D				 D     D
E        1      E			E   2   E
D           D				 D     D
C       C				  C   C
B   B					   B B
A					    A

```
```#include <iostream>
using namespace std;

int main() {
void f(int n,int i,char ch);
int n;
char ch;
do {
cin>>ch>>n;
}while(!isalpha(ch)); //Jump out of loop if and only if ch is a letter (1)
for(int i=0 ; i<n ; i++) {
if(!isalpha(ch)) {     //When ch is not a letter
if(islower(ch-1))  //When ch-1 is Z, ch=A, forming a closed cycle (2)
ch='a';
else
ch='A';
}
f(n,i,ch);
ch++;
}
ch--;
for(int i=n-2; i>=0; i--) {
ch--;
if(!isalpha(ch)) {
if(islower(ch-1))  //When ch-1 is A, ch=Z, forming A closed cycle (3)
ch='z';
else
ch='Z';
}
f(n,i,ch);
}
return 0;
}
void f(int n,int i,char ch) { //1
for(int j=0 ; j<n-i-1; j++ )
cout<<"  ";
for(int j=0 ; j<2*i+1 ; j++ ) {
if(j == 0)
cout<<ch<<" ";
else if(j == 2*i)
cout<<ch;
else
cout<<"  ";
}
cout<<endl;
}
/*
void f(int n,int i,char ch) {  //2
for(int j=0 ; j<n-i-1; j++ )
cout<<" ";
for(int j=0 ; j<2*i+1 ; j++ ) {
if(j == 0 ||j == 2*i)
cout<<ch;
else
cout<<" ";
}
cout<<endl;
}  */
return 0;
}
```

Analysis: pay attention to the three comments of the code:
(1) The input is input by hand. If you input A letter at the beginning, it forms A closed loop. Therefore, if you input A non alphabetic character at the beginning, the result is always from A, which is inconsistent with the meaning of the title (input the top line character and the height of the figure, output the diamond); therefore, it will be omitted if you don't pay attention.
(2) (3) the title means that the final result is composed of letters. If you don't write these two pieces of code, the result will be non alphabetic. Therefore, it should form a closed loop
(4) When calling a user-defined function, if you do not use a pointer, you cannot directly change the value of the parameter in the user-defined function, because the function only plays the role of calling

[3] More difficult

Tags: C Algorithm C++

Posted by olsrey on Tue, 21 Sep 2021 14:22:18 +0530