Super star MOOC learning pass high level language programming C + + experiment 04 array of Jilin University and its application in programming (level 2021)

one   Index array sort

Title No.: Exp04-Enhance04,GJBook3-06-21

Title: index array sorting

Title Description: it is known that the integer array a with n (n ≤ 100) elements is not sorted, and an index array B saves the subscript of A. Write the program. Without changing array a, only change array B to complete the incremental sorting of a, as follows: the first element value of index group B after sorting is the subscript of the smallest element in array a.

Before sorting
 Array A: 9   seven   five   eight   0   four   one   three   two   six  
Array B: 0   one   two   three   four   five   six   seven   eight   nine
 After sorting
 Array A: 9   seven   five   eight   0   four   one   three   two   six
 Array B: 4   six   eight   seven   five   two   nine   one   three   0


Input: the first line inputs A positive integer n, and the second line randomly inputs n different integers as the elements of array A.

Output: the first line outputs n elements of array A, separated by A Western space; The second line outputs n elements of array B, which are separated by A Western space; There are no characters after the last element of each line.
 

Example 1:

Input:
10
9 7 5 8 0 4 1 3 2 6
Output:
9 7 5 8 0 4 1 3 2 6
4 6 8 7 5 2 9 1 3 0

Example 2:

Input:
5
9 8 7 6 5
Output:
9 8 7 6 5
4 3 2 1 0
#include <iostream>

using namespace std;

int main()
{
	int n, * a, * b;
	cin >> n;
	a = new int[n];
	b = new int[n];
	for (int i = 0;i < n;i++)
	{
		cin >> a[i];
		b[i] = i;
	}
	for (int i = 0;i < n - 1;i++)
		cout << a[i] << " ";
	cout << a[n - 1] << endl;

	for (int i = 0;i < n - 1;i++)
	{
		for (int j = 0;j < n - i - 1;j++)
		{
			if (a[j] > a[j + 1])
			{
				int tmp1 = a[j];
				a[j] = a[j + 1];
				a[j + 1] = tmp1;
				int tmp2 = b[j];
				b[j] = b[j + 1];
				b[j + 1] = tmp2;

			}
		}
	}
	for (int i = 0;i < n - 1;i++)
		cout << b[i] << " ";
	cout << b[n - 1];


	return 0;
}

Dynamic array opens up appropriate space according to the input n and makes rational use of memory

a[n]   Save the input data, b[n] save the array subscript

Since you want to change array B to complete the incremental sorting of A without changing array A, output A first,

It is also a two-layer for loop. In the for loop, the corresponding elements of a[n] and b[n] must be exchanged at the same time, rather than only the corresponding elements in b. This is to ensure that the elements in a correspond to the subscripts one by one

Another important point is that the processing of spaces at the end of lines must be handled, otherwise the program can't pass

two   Joseph problem

Title No.: Exp04-Enhance02, GJBook3-06-26

Title: Joseph problem

Title Description:

In ancient times, a judge wanted to sentence n prisoners to death. He had an absurd logic, which arranged the prisoners head to tail in a circle, and all counts began with 1; Then count from the s-th person, and pull out for execution every time you count to the m-th prisoner; Then count m more, and the prisoners who count will be executed; But the last remaining prisoner can be pardoned. Program, give the order of origin, and tell who survived.

Input: three positive integers n (≤ 1000), s and m can be represented by int type variables.

Output: the numbers of the executed persons are output in turn. Each number is separated by a Western space, and there are no characters after the last number.
 

Example:

Input: 6   one   five
Output: 5   four   six   two   three   one

#include <iostream>

using namespace std;

int main()
{
	int n, m, i, x = 1, y = 0;
	cin >> n >> x >> m;
	int* arr;
	arr = new int[n + 1];
	for (i = 1; i <= n; i++)
		arr[i] = i;
	for (y = 0; y < n; y++)//Number of dead (all dead)
	{
		for (i = 1; i <= m; i++)
		{
			while (arr[x] == 0)
			{
				x++;
				if (x > n)
					x = 1;
			}//Dead people are meaningless
			if (arr[x] != 0)
			{
				x++;
				if (x > n)
					x = 1;
			}//Keep counting
		}
		if (x == 1)
		{
			arr[n] = 0;
			cout << n;
		}
		else
		{
			arr[x - 1] = 0;
			cout << (x - 1);
		}
		if (y != n - 1)
			cout << " ";
	}


	return 0;
}

The code is put here first. I hope it can help you understand.

In addition. Joseph problem is a classic algorithm problem. I will explain Joseph problem in a separate issue in the future  

three   n-multiple relation

Title No.: Exp04-Basic02

Title: n-multiple relationship

Title Description:

Given several different positive integers (< 10000) and n (n < 5), calculate how many pairs of these positive integers satisfy: one is n times of the other. For example, when n=3, the answer is 2; because 3 is 3 times of 1 and 9 is 3 times of 3.


Input: input the first line to give the value of positive integer n, and then include multiple groups of test data. Each group of data can contain up to 100 integers, occupying one line and ending with the number 0 (not included in the 100 integers). The test data does not exceed 20 groups, and the last line only includes - 1, indicating the end of the input data.

Output: for each group of input data, output a line, and give how many pairs satisfy that one is n times the other. (Note: there are no extra characters such as line feed at the end of the last line.)
 

Example:

Input:
2
1 4 3 2 9 7 18 22 0
2 4 8 10 0
7 5 11 13 1 3 0
-1
Output:
3
2
0

#include <iostream>
#include <vector>

using namespace std;

vector<int> v1;
vector<int> v2;

int main()
{
	int n, m, x;
	int cnt = 0;
	cin >> n;
	while (1)
	{
		v1.clear();
		cin >> x;
		if (x == -1)
			break;
		while (x != 0)
		{
			v1.push_back(x);
			cin >> x;
		}
		cnt++;
		int res = 0;
		for (int i = 0;i < v1.size();i++)
		{
			for (int j = i + 1;j < v1.size();j++)
			{
				if (v1[i] == v1[j] * n || v1[j] == v1[i] * n)
					res++;
			}			
		}
		v2.push_back(res);
	}
	for (int i = 0;i < v2.size();i++)
	{
		cout << v2[i];
		if (i != v2.size() - 1)
			cout << endl;

	}

	return 0;
}

With the header file of vector, you can use the very convenient dynamic array vector  

How to read in the input format according to the requirements of the topic is the key to this topic

four   Matrix transpose

Title No.: Exp04-Basic08,GJBook3-06-03

Title:   Matrix transpose

Problem Description: write a program to transpose 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: output all elements in the transposed array in the order of first column and last column, from left to right. There are n elements in each line, and each element in the same line is separated by a Western space; the last element in each line has no other characters except the necessary carriage return line feed character.

Example 1:

Input:
3
1 2 3
1 2 3
1 2 3
Output:
1 1 1
2 2 2
3 3 3

Example 2:

Input:
3
1 1 1
2 2 2
3 3 3
Output:
1 2 3
1 2 3
1 2 3

#include <iostream>

using namespace std;

int main()
{
	int n;
	cin >> n;
	int** a = new int* [n];
	for (int i = 0;i < n;i++)
		a[i] = new int[n];
	for (int i = 0;i < n;i++)
	{
		for (int j = 0;j < n;j++)
		{
			cin >> a[i][j];
		}
	}
	for (int i = 0;i < n;i++)
	{
		for (int j = 0;j < n - 1;j++)
		{
			cout << a[j][i] << " ";
		}
		cout << a[n - 1][i] << endl;
	}

	return 0;
}

Note the creation of dynamic two-dimensional array at the beginning

int **a=new int*[n];

 for(int i=0;i<n;i++)

a[i]=new int[n];

five   Check the symmetry of the main diagonal of the matrix

Title No.: Exp04-Basic09, GJBook3-06-02

Title: check the symmetry of the main diagonal of the matrix

Title Description: write a program to judge whether any given two-dimensional integer array of n*n is symmetrical about the main diagonal.
 

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 the array is symmetrical about the main diagonal, output YES; otherwise, output NO.

Example 1:

Input:

3
1 2 3
2 1 2
3 2 1
Output:
YES

Example 2:

Input:

3
0 0 1
2 1 2
3 2 1
Output:
NO

#include <iostream>

using namespace std;

int main()
{
	int n;
	cin >> n;
	int** a = new int* [n];
	for (int i = 0;i < n;i++)
		a[i] = new int[n];

	for (int i = 0;i < n;i++)
	{
		for (int j = 0;j < n;j++)
		{
			cin >> a[i][j];
		}
	}
	int flag = 1;
	for (int i = 1;i < n;i++)
	{
		for (int j = i + 1;j < n;j++)
		{
			if (a[i][j] != a[j][i])
				flag = 0;

		}
	}
	if (flag == 1)
		cout << "YES" << endl;
	else if (flag == 0)
		cout << "NO" << endl;

	return 0;
}

Tags: C++ Back-end

Posted on Sat, 04 Dec 2021 18:52:51 -0500 by ltbaggz