The skills of these questions are quite strong. We need to calm down and think and understand them
six Transfer 0 element
Title No.: Exp04-Basic05, GJBook3-06-06
Title: transfer 0 element
Title Description: write a program to move all 0 elements in a given integer array to the back without using other auxiliary arrays, and the order of all non-0 elements remains the same.
Input: the first line inputs the array length n (≤ 100), and the second line randomly inputs n integers from the keyboard as the array element value.
Output: all 0 elements have been concatenated to the following integer array, each element is separated by a Western space, and there is no character after the last element.
Example 1:
Input: 10 0 3 1 0 0 0 1 2 3 0
Output: 3 1 1 2 3 0 0 0 0 0
Example 2:
Input: 10 0 0 0 0 0 0 1 2 3 4
Output: 1 2 3 4 0 0 0 0 0 0
#include <iostream> using namespace std; int main() { int n = 0; int arr[100]; cin >> n; for (int i = 0;i < n;i++) { cin >> arr[i]; } int cnt = 0; for (int i = 0;i < n;i++) { if (arr[i] != 0) { cout << arr[i] << " "; cnt++; } } for (int i = cnt;i < n;i++) { cout << "0 "; } return 0; }
If the question has no special requirements, it is easier to change the output order of the elements in the array than to change the array, and so is the next question
seven rotate right
Title No.: Exp04-Basic06, GJBook3-06-04
Title: cycle right
Title Description: write a program to move each element in a one-dimensional integer array to the right by j bits without using other auxiliary arrays.
Input:
In the first line, enter two integers. N represents the length of the array (0 < n < = 100), and j represents the number of bits shifted right circularly (J > = 0);
The second line randomly inputs n integers as array element values from the keyboard in turn.
Output:
The integer array after cyclic right shift, each element is separated by a Western space, and there is no character after the last element.
Example 1:
Input: 10 2 1 2 3 4 5 6 7 8 9 0
Output: 9 0 1 2 3 4 5 6 7 8
Example 2:
Input: 10 23 1 2 3 4 5 6 7 8 9 0
Output: 8 9 0 1 2 3 4 5 6 7
The first relatively simple method is to change the output as above
#include <iostream> using namespace std; int main() { int n, m, * a; cin >> n >> m; a = new int[n]; for (int i = 0;i < n;i++) { cin >> a[i]; } if (m > n) { m = m % n; } for (int i = n - m;i < n;i++) { cout << a[i] << " "; } for (int i = 0;i < n - m;i++) { cout << a[i] << " "; } return 0; }
The second method is to change the array and put the code below for your reference
#include <iostream> #include <vector> using namespace std; vector<int> a; int main() { int n, m, x; cin >> n >> m; if (m > n) m = m % n; int cnt = n; while (n > 0)//Input array { cin >> x; a.push_back(x); n--; } for (int i = 0;i < m;i++) { int tmp = a[cnt - 1];//Temporarily stores the last element in the vector a.erase(a.end()-1);//Delete it a.insert(a.begin(),tmp);//Insert the last element in the original vector into the beginning of the vector } for (vector<int>::iterator it = a.begin();it != a.end();it++)//Print sorted array { cout << *it << " "; } return 0; }
eight parenthesis matching
Title No.: exp04-basic01, gjbook 3 cases - 06-13
Title: bracket matching
Title Description: write a program to read the character sequence ending with '@' from the terminal, and check whether the (and), [and], {and} in the character sequence match (the number is equal and the position does not intersect).
Input: contains a string ending with '@', which may contain whitespace or other non bracketed characters.
Output: if the three types of parentheses in the string match, YES is output; Otherwise, NO is output.
Example 1:
Input:
{a,a}b{c[cc]c} {a(bb[cc]dd)a}@
Output: YES
Example 2:
Input: {a,a}b{c[cc]c] {a(bb[cc]dd)a}@
Output: NO
#include <iostream> #include <vector> using namespace std; vector<char> a; int main() { char ch; cin >> ch; int cnt = 0; while (ch != '@') { switch (ch) { case '(':a.push_back('(');break; case '[':a.push_back('[');break; case '{':a.push_back('{');break; case ')': { if (a.back() == '(') a.pop_back();//Delete the last element of the vector break; } case ']': { if (a.back() == '[') a.pop_back(); break; } case '}': { if (a.back() == '{') a.pop_back(); break; } } cin >> ch; } cnt = a.size(); if (cnt == 0) { cout << "YES" << endl; } else { cout << "NO" << endl; } return 0; }
The familiar while loop and vector come again, which provides us with a very good idea for solving problems
The difficulty of this problem is to check that the brackets are equal and do not intersect
Enter a string ending in '@'. When encountering the left bracket, press in the vector. When encountering the right bracket, judge whether it matches the previous left bracket. If it matches, delete the corresponding left bracket, and then continue to read the next character
Finally, if a.size()==0, the brackets match, otherwise they do not match
nine Check matrix repeat element
Title No.: Exp04-Basic07, GJBook3-06-01
Title: duplicate elements of inspection matrix
Title Description: write a program to judge whether there are the same elements in any given n*n two-dimensional integer array.
Input: input the number of array lines n (≤ 10) in the first line, and input n*n integers randomly as the array element value in the second line.
Output: if there are the same elements in the array, YES is output; Otherwise, NO is output.
Example 1:
Input: 3 1 2 3 4 5 6 7 8 9
Output: NO
Example 2:
Input: 3 1 1 2 3 4 5 6 7 8
Output: YES
#include <iostream> using namespace std; int main() { int n, * a; cin >> n; a = new int[n * n]; for (int i = 0;i < n * n;i++) { cin >> a[i]; } int flag = 0; for (int i = 0;i < n * n - 1;i++) { for (int j = i + 1;j < n * n;j++) { if (a[i] == a[j]) flag = 1; } } if (flag == 1)cout << "YES" << endl; else cout << "NO" << endl; return 0; }
The core of judging repetition lies in the nesting of two for loops in the middle. This sorting method can ensure that there is no repetition and leakage in the comparison elements. You can't write only one for loop, otherwise it is easy to miss the comparison between some elements
ten Spiral matrix
Title No.: Exp04-Enhance05, freshman-1050, GJBook3-0622
Title: spiral matrix
Title Description:
Spiral matrix refers to a spiral digital matrix. Its numbers continue to grow from the first row to the right, down, left and up, and so on. The third-order helix matrix is shown below.
Input: enter a positive integer N (N < = 30) to indicate the order of the matrix.
Output: the integer 1~N^2 is output in the form of spiral matrix, with n rows and N columns in total. Each digital output occupies the width of 4 western characters, right aligned, and there are no redundant characters after the last number.
Example:
Input: 15
Output:
Finally, I saw the ultimate big Boss, spiral matrix. It is said that many big factories love to test this question,
There are two main ideas for solving problems,
One is array method: find the subscript relationship of each increasing element and use circular output
The second is recursive method: this problem gives an N*N-order square matrix, so it is easy to think about it with recursive method
Finally, it comes down to the elements of reasonable judgment a[i][j]
Since the school may not have talked about recursive algorithm, we mainly explain the array method here
1. First, consider the case when N is odd or N is even
a00 | a01 | a03 | a04 | a05 |
a10 | a11 | a12 | a13 | a14 |
a20 | a21 | a22 | a23 | a24 |
a30 | a31 | a32 | a33 | a34 |
a40 | a41 | a42 | a43 | a44 |
The output idea is to output the outer N/2 layer first, and then output the middle a[N/2][N/2]
a00 | a01 | a03 | a04 |
a10 | a11 | a12 | a13 |
a20 | a21 | a22 | a23 |
a30 | a31 | a32 | a33 |
When n is an even number, it is relatively simple to directly output the N/2 layer cycle
2. Observe the elements with the same color on the two tables to determine the output order of each element
The output of each layer shall be written into four cycles, and each cycle outputs N-1 elements, which are output from the outside to the inside in turn
int k = 1; for (i = 0;i < n / 2;i++)//Judge the number of cycle layers { for (j = i;j < n - i - 1;j++)//upper a[i][j] = k++; for (j = i;j < n - i - 1;j++)//right a[j][n - i - 1] = k++; for (j = n - i - 1;j > i;j--)//lower a[n - i - 1][j] = k++; for (j = n - i - 1;j > i;j--)//Left a[j][i] = k++; } if (n % 2 == 1)//When N is an odd number, assign values to the remaining elements in the middle separately a[n / 2][n / 2] = k;
The final code is below
#include <iostream> #include <iomanip> using namespace std; int main() { //n-order spiral matrix int n, m; int i, k, j; cin >> n; int** a = new int* [n]; for (int i = 0;i < n;i++) a[i] = new int[n]; k = 1; for (i = 0;i < n / 2;i++) { for (j = i;j < n - i - 1;j++) a[i][j] = k++; for (j = i;j < n - i - 1;j++) a[j][n - i - 1] = k++; for (j = n - i - 1;j > i;j--) a[n - i - 1][j] = k++; for (j = n - i - 1;j > i;j--) a[j][i] = k++; } if (n % 2 == 1) a[n / 2][n / 2] = k; for (int i = 0;i < n;i++) { for (int j = 0;j < n;j++) { cout << setfill(' ') << setw(4) << a[i][j]; } cout << endl; } return 0; }
Finally, don't forget to use the iomanip header file to complete the output format required by the topic
cout << setfill(' ') << setw(4) << a[i][j];//Set the width to 4 and fill it with spaces