Question A: Exercise 6-4 Ordered Insertion
Title Description
There is a sorted array, which requires you to enter a number and insert it into the array according to the rules of the original sorting.
Assuming the length of the array is 10, the first 9 numbers in the array (which require input from the keyboard and satisfy the input order from smallest to largest) are sorted from smallest to largest.
Then enter an integer from the keyboard, and insert the integer into the previous 9 numbers so that the final 10 numbers are still small to large.
input
The first line inputs nine integers separated by spaces, requiring input from smallest to largest.
Enter an integer on the second line
output
Output these 10 numbers from small to large, one line at a time.
sample input
1 11 21 31 41 51 61 71 81
45
sample output
1
11
21
31
41
45
51
61
71
81
Tips
When defining an array, define the length of the array to 10.
codes
#include <iostream> #include <stdio.h> using namespace std; int main() { int a[10],temp; for(int i=0;i<10;i++) scanf("%d",&a[i]); // scanf("%d%d%d%d%d%d%d%d%d",&a[0],&a[1],&a[2],&a[3],&a[4],&a[5],&a[6],&a[7],&a[8]); // Scanf ('%d', &a[9]); at first I thought it was necessary to write this in order to make the first line blank, then I returned to the second line and found that direct input was fine for(int i=9;i>0;i--) { if(a[i]>a[i-1]) break; temp=a[i]; a[i]=a[i-1]; a[i-1]=temp; } for(int i = 0;i<10;i++) printf("%d\n",a[i]); }
Question B: Exercise 6-5 Array Elements Inverted
Title Description
Restore values in an integer array of length 10 in reverse order.
For example: the original order is 1,2,3,4,5,6,7,8,9,0, requiring 0,9,8,7,6,5,4,3,2,1
input
Enter 10 integers separated by spaces from the keyboard.
output
Output these 10 numbers in reverse order, each on a line.
sample input
1 2 3 4 5 6 7 8 9 0
sample output
0
9
8
7
6
5
4
3
2
1
codes
#include <iostream> #include <stdio.h> using namespace std; int main() { int a[10]; for(int i=0;i<10;i++) scanf("%d",&a[i]); for(int i = 9;i>=0;i--) printf("%d\n",a[i]); }
Question C: Exercise 6-6 Yang Hui Triangle
Title Description
Enter Yang Hui Triangle in the following format as required
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Up to 10 layers of output
input
The input contains only a positive integer n, which represents the number of layers of Yang Hui triangle that will be output.
output
For this input, output the Yang Hui triangle of the corresponding number of layers separated by a space between the integers of each layer
sample input
5
sample output
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
codes
#include <iostream> #include <stdio.h> using namespace std; int main() { int a[10][10],n; scanf("%d",&n); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(j==0||j==i) a[i][j]=1; else a[i][j]=a[i-1][j-1]+a[i-1][j]; } } for(int i=0;i<n;i++) { for(int j=0;j<=i;j++) printf("%d ",a[i][j]); printf("\n"); } }
Question D: Decrypt Exercise 6-12
Title Description
A line of text has been translated into a password as follows:
A–>Z a–>z
B–>Y b–>y
C–>X c–>x
... ...
That is, the first letter becomes 26 letters, the first letter becomes (26-i+1) letters, and the non-alphabetic characters remain unchanged.Requires that the original text be translated according to the password and output.
input
Enter a line of ciphertext
output
The decrypted text will be on a separate line.
sample input
ZYX123zyx
sample output
ABC123abc
codes
#include <iostream> #include <stdio.h> using namespace std; int main() { char str[26]; scanf("%s",str); for(int i=0;i<26;i++) { if(str[i]>=65&&str[i]<=90) str[i]=90-(str[i]-65); else if(str[i]>=97&&str[i]<=122) str[i]=122-(str[i]-97); } printf("%s",str); return 0; }
Question E: Exercise 6-13 String Comparison
Title Description
Compare the sizes of the two strings s1 and s2, and output a positive number if s1 > s2, 0 if s1=s2, and a negative number if s1 < s2.
Requirements: No strcpy function is used; two strings are read in with the get function.
For example, "A" versus "C" should output negative numbers because "A" < "C", and "-2" because the A SCII code difference between "A" and "C" is 2.
Similarly, "And" and "Aid" comparison, according to the result of the second character comparison, "n" is "5 larger than" i, so it should output "5"
input
Enter a 2-line string
output
An integer representing the difference between the two strings on a single line.
sample input
And
Aid
sample output
5
codes
#include <iostream> #include <stdio.h> using namespace std; int main() { char str1[26],str2[26]; int a; gets(str1); gets(str2); for(int i=0;i<26;i++) { if(str1[i]!=str2[i]) { a=str1[i]-str2[i]; break; } } printf("%d",a); return 0; }
Question F: Example 6-1 Output Array Elements in Reverse Order
Title Description
Enter 10 integers from the keyboard, stored in a 10-length integer array, requiring the output of the 10 numbers entered to be in reverse order.
If the input is: 0,1,2,3,4,5,6,7,8,9 the output is 9,8,7,6,5,4,3,2,1,0
input
10 integers, separated by spaces
output
The 10 integers entered are output in reverse order, one line for each number.
sample input
0 1 2 3 4 5 6 7 8 9
sample output
9
8
7
6
5
4
3
2
1
0
codes
#include <iostream> #include <stdio.h> using namespace std; int main() { int a[10]; for(int i=0;i<10;i++) { scanf("%d",&a[i]); } for(int i=9;i>=0;i--) printf("%d\n",a[i]); return 0; }
Problem G: Example 6-2 Array Solving Fibonacci Array Problem
Title Description
The characteristics of Fibonacci number series: 1,2 numbers are 1,1.Beginning with the third number, the overview is the sum of the first two numbers.That is:
Requires the first 20 Fibonacci number columns to be output.
input
nothing
output
The first 20 Fibonacci number columns, one row for each number.
sample input
nothing
sample output
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
codes
#include <iostream> #include <stdio.h> using namespace std; int main() { int a[20]; a[0]=1,a[1]=1; for(int i=2;i<20;i++) a[i]=a[i-1]+a[i-2]; for(int i=0;i<20;i++) printf("%d\n",a[i]); return 0; }
Question H: Example 6-3 Bubble Sorting
Title Description
Enter 10 integers from the keyboard and sort them by bubble (from small to large).
input
10 integers separated by spaces
output
Output 10 ordered integers, each on a line.
sample input
1 3 5 7 9 2 4 6 8 0
sample output
0
1
2
3
4
5
6
7
8
9
codes
#include <iostream> #include <stdio.h> using namespace std; int main() { int a[10],temp; for(int i=0;i<10;i++) scanf("%d",&a[i]); for(int i=0;i<10;i++) for(int j=10;j>0;j--) { if(a[j]<a[j-1]) { temp = a[j]; a[j] = a[j-1]; a[j-1] = temp; } } for(int i=0;i<10;i++) printf("%d\n",a[i]); return 0; }
Question I: Example 6-4 Matrix Transposition
Title Description
Exchanges rows and columns of a 2-row 3-column matrix (two-dimensional array) into another 3-row 2-column matrix.
Ask for an example of integer data.
input
Enter two rows of data, three integers per row, separated by spaces.
output
Matrix with 3 rows, 2 data per row, separated by spaces.
sample input
1 2 3
4 5 6
sample output
1 4
2 5
3 6
codes
#include <iostream> #include <stdio.h> using namespace std; int main() { int a[2][3],b[3][2]; for(int i=0;i<2;i++) for(int j=0;j<3;j++) scanf("%d",&a[i][j]); for(int j=0;j<2;j++) { for(int i=0;i<3;i++) { b[i][j]=a[j][i]; } } for(int i=0;i<3;i++) { for(int j=0;j<2;j++) printf("%d ",b[i][j]); printf("\n"); } return 0; }
Question J: Example 6-9 String Maximum
Title Description
Enter three strings from the keyboard to find the largest one.
input
Enter 3 lines, each of which is a string.
output
One line, enter the largest of the three strings.
sample input
England
China
America
sample output
England
codes
#include <stdio.h> #include<string.h> using namespace std; int main() { char str1[20],str2[20],str3[20]; gets(str1); gets(str2); gets(str3); int i=strcmp(str1,str2); int j=strcmp(str1,str3); int k=strcmp(str2,str3); if(i>0) { if(j>0) puts(str1); else puts(str3); } if(i<0) { if(k>0) puts(str2); else puts(str3); } return 0; }