Algorithm design and analysis 002
1. Sequence of basic exercises
Given a sequence of length N, the sequence is arranged from small to large. 1 < = n < = 200 input Description:
The first line is an integer n.
The second row contains n integers, which are the numbers to be sorted, and the absolute value of each integer is less than 10000.
Input example:
5
8 3 6 4 9
Output Description:
Output a row, and output the sorted sequence from small to large.
Output example:
3 4 6 8 9
Example code:
#include <iostream> #include<algorithm> using namespace std; int main() { int n; cin >> n; int array[n]; for (int i = 0; i < n; i++) { cin >> array[i]; } sort(array, array + n); for (int i = 0; i < n; i++) { cout << array[i] << " "; } }
2 basic practice time conversion
Given a time t in seconds, it is required to express the time in the format of "::. Represents time, minutes, and seconds. They are all integers without leading "0". For example, if t=0, the output should be "0:0:0"; If t=3661, output "1:1:1". Enter Description:
The input has only one line, which is an integer t (0 < = T < = 86399).
Input example:
0
Output Description:
The output has only one line, which is the time expressed in the format of "::" without quotation marks.
Output example:
0:0:0
Example code:
#include <iostream> #include<algorithm> using namespace std; int main() { unsigned int time; int hour = 0; int minute = 0; int second = 0; cin >> time; while(time >= 60) { minute++; time -= 60; } second = time; while(minute >= 60) { hour++; minute -= 60; } if (hour == 24) hour = 0; cout << hour << ":" << minute << ":" << second << endl; }
3. Basic practice matrix multiplication
Given an N-order matrix A, output the M-power of a (M is a nonnegative integer)
For example:
A =
1 2
3 4
The second power of A
7 10
15.22 input Description:
The first row is a positive integer N, M (1 < = N < = 30, 0 < = M < = 5), representing the order of matrix A and the required idempotent
Next, N rows, N non negative integers with absolute values not exceeding 10 in each row, describe the value of matrix A
Input example:
2 2
1 2
3 4
Output Description:
Output a total of N rows, n integers per row, representing the matrix corresponding to the M-power of A. Adjacent numbers are separated by a space
Output example:
7 10
15 22
Code example:
#include<iostream> #include<cstring> using namespace std; int start[50][105]; int tmp[50][105]; int last[50][105]; void Display(int a[][105], int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cout << a[i][j] << " "; cout << endl; } } void Getmatrix(int start[][105], int tmp[][105], int last[][105], int n, int m) { for (int i = 1; i < m; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { int temp = 0; for (int x = 0; x < n; x++) { temp += start[j][x] * tmp[x][k]; last[j][k] = temp; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { tmp[i][j] = last[i][j]; } } } } int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> start[i][j]; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { tmp[i][j] = start[i][j]; } } if (m == 0) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) last[i][j] = 1; } } Display(last, n); } else if (m == 1) { Display(start, n); } else { Getmatrix(start, tmp, last, n, m); Display(last, n); } return 0; }
4. Division of algorithm training number
Divide the integer n into k parts, and each part cannot be empty, and any two parts cannot be the same (regardless of order).
For example: n=7, k=3, the following three methods are considered to be the same.
1,1,5; 1,5,1; 5,1,1;
Ask how many different methods there are. Enter Description:
n,k
Input example:
7 3
Output Description:
An integer, that is, a different division
Output example:
4 {four methods are: 1, 1, 5; 1, 2, 4; 1, 3, 3; 2, 2, 3;}
Code example:
#include<iostream> #include<math.h> #include<memory.h> using namespace std; int main() { int n, k; cin >> n >> k; int dp[240][15]; memset(dp,0, sizeof(dp)); for (int i = 1; i <= n; i++) for (int j = 1; j <= i; j++) if (j == 1) dp[i][j] = 1; else dp[i][j] = dp[i - j][j] + dp[i - 1][j - 1]; cout << dp[n][k] << endl; return 0; }
#include<iostream> using namespace std; int Dfs(int n, int k) { if (n == 0 || k == 0 || n < k) { return 0; } if (n == 1 || n == k) { return 1; } else { return Dfs(n - k, k) + Dfs(n - 1, k - 1); } } int main() { int n, k; cin >> n >> k; cout << Dfs(n, k) << endl; return 0; }