Jilin University Chaoxing MOOC learning pass high level language programming C + + experiment 03 modular programming (level 2021)

1, Single choice questions (1 in total, 16.6 points)  

one   (single topic)

There are function definitions: int f(int x,int y); The following function calls are correct(     )

  • A.
  • B. int n,a=0,b=1;n=int f(x,y);
  • C. int n,a=0,b=1;n=f(a,b);
  • D.

My answer:   C

  2, Procedural questions (5 questions in total, 83.4 points)

two   Automorphic number

Title No.: Exp03-Extend01, GJBook3-12-04

Title: automorphic number

If a positive integer a satisfies the condition that the mantissa of a^2 is equal to a, then a is called an automorphic number, for example:

25 ^ 2 = 625, 76 ^ 2 = 5776, 9376 ^ 2 = 87909376 are all automorphic numbers.

Write a program to find all automorphic numbers less than or equal to n.

Input: randomly input a positive integer n (< 10000000) from the keyboard.

Output: output all automorphic numbers less than n, with a Western space between each number.
 

Example 1:

Input: 10
Output: 1   five   six

Example 2:

Input: 100
Output: 1   five   six   twenty-five   seventy-six

#include <iostream>

using namespace std;

int main()
{
	int n = 0;
	cin >> n;
	long long x = 0;
	cout << "1 ";
	for (int i = 2;i < n;i++)
	{		
		int j = 10000000;
		x = i * i;
		while (j / i >= 10)
		{
			j /= 10;
		}
		x %= j;
		if (x == i)
			cout << i << " ";
	}
	return 0;
}

Talk about the idea of this problem: x=a*a

1. First, clarify the definition of "surrender number" in the title

2. Secondly, the key to the problem is how to find the mantissa of x

Let's focus on the second point: after careful thinking, we will find that it is not useful to take the reverse order of x, and it is not very convenient to use arrays;

So we can convert it into the number of bits of a, add 1, and finally take the module.

So the question comes again, how to find the number of digits of a?

Let's give this task to the while loop,

int j = 10000000;
while (j / i >= 10)
{
	j /= 10;
}

The resulting j value is exactly the number of digits of a plus 1,

x%j is exactly the mantissa of x, and then compare it with a

  three   Combinatorial problem

Title No.: Exp03-Basic01, GJBook3-05-02

Title: combinatorial problem

Title Description: write a program, input the values of m and n, calculate and output the value of function f.

Input: an integer m and an integer n (m,n ≤ 20).

Output: value of function f.
 

Example 1:

Input: - 1    two
Output: - 1

Example 2:

Input: 2    two
Output: 1

If you are not familiar with factorial, you should read it Super star MOOC learning pass high level language programming C + + experiment 02 branch and loop programming (level 2021) (1) _S_CuRrY666 blog CSDN blog   Or talk less and go directly to the code

#include <iostream>

using namespace std;

long long jiec(int n)
{
	long long sum = 1;
	for (int i = 1;i <= n;i++)
	{
		sum *= i;
	}
	return sum;
}
int main()
{
	int m, n;
	cin >> m >> n;
	if ((m < n) && (m > 0) && (n > 0))
		cout << "0" << endl;
	else if ((m == n) && (m > 0) && (n > 0))
		cout << "1" << endl;
	else if ((m > n) && (m > 0) && (n > 0))
	{
		long long sum;
		sum = jiec(m) / jiec(m - n) / jiec(n);
		cout << sum << endl;
	}
	else cout << "-1" << endl;
	return 0;
}

This inscription describes a jiec function to find factorials. Because there are three factorials involved, it will be very inconvenient to put factorials in the main function  

four   Reverse order number

Title No.: Exp03-Basic02, GJBook3-05-06

Title: reverse order number

Title Description: write a function to find the inverse number of any bit natural number. For example, 5432 is the inverse number of 2345.

Input: any integer (< 10 ^ 18).

Output: if the input is not a natural number, NULL is output; otherwise, the corresponding reverse order number is output.


Example 1:

Input: 120
Output: 21

Example 2:

Input: 9999999999999999999
Output: 9999999999999999999

Example 3:

Input: - 1357
Output: NULL

#include <iostream>

using namespace std;

int main()
{
	long long m;
	int n = 0;
	cin >> m;
	if (m <= 0)
		cout << "NULL" << endl;
	else if (m > 0)
	{		
		while (m > 0) 
		{			
			n++;
			int a = 0;
			a = m % 10;
		    if (a != 0 || n != 1)
				printf("%d", a);
			m /= 10;
		}
	}
	return 0;
}

The most important point of this program is the while loop in the middle. Observe the output example. If the mantissa of the input number is 0, it will be omitted. Therefore, judge the last number. First define n=0, enter the while loop and then n=1. If a=0, do not output, and then output directly in reverse order.

five   Polygon perimeter

Title No.: Exp03-Basic03

Title: polygon perimeter

Title Description: write a program to calculate the polygon perimeter of n (0 < n < = 10) vertex coordinates input clockwise by the keyboard (the test data has ensured that when n > 2, each point can be connected in turn according to the input direction to form a closed n-sided shape).

Input: input an integer in the first line as the n value; after that, there are two floating-point numbers (double) in each line, which are the coordinates of each vertex of the polygon.

Output: perimeter of polygon, accurate to 2 decimal places.
 

Example 1:

Input:
4
0  0
0  1
1  1
1  0
Output: 4.00

Example 2:

Input:
1
2 3
Output:
0.00

Example 3:

Input:
2
2 3
2 4
Output:
1.00

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

double dist(double x1, double y1, double x2, double y2) {

	double d = 0;

	d = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));

	return d;

}
int main()
{
	int n = 0;
	cin >> n;
	double sum = 0, arr[10][2];
	for (int i = 0;i < n;i++)
	{
		for (int j = 0;j < 2;j++)
		{
			cin>>arr[i][j];
		}
	}
	if (n == 1)cout << "0.00" << endl;
	if (n == 2)
	{
		sum = dist(arr[0][0], arr[0][1], arr[1][0], arr[1][1]);
		//printf("%.2lf", sum);
		cout << fixed << setprecision(2) << sum << endl;
	}
	if (n > 2)
	{
		for (int i = 0;i < n - 1;i++)
		{
			for (int j = 0;j < 1;j++)
			{
				sum += dist(arr[i][j], arr[i][j + 1], arr[i + 1][j], arr[i + 1][j + 1]);
			}
		}
		sum += dist(arr[0][0], arr[0][1], arr[n - 1][0], arr[n - 1][1]);
		//printf("%.2lf", sum);
		cout << fixed << setprecision(2) << sum << endl;
	}
	return 0;
}

It is better to divide n into three cases. Don't forget that when n > 2, the graph is closed from beginning to end.  

6. Prime judgment

[2014 grade midterm questions] prime judgment.

Problem Description: a natural number greater than 1 can only be divided by 1 and itself, and cannot be divided by other natural numbers other than 0. Then the number is called prime. Write a function to judge whether an integer is prime.

Note: the judging prime part must be written as another function independent of the main() function.

Input: a natural number greater than 1

Output: output Y/N according to whether it is prime or not

Example 1:

Input: 3

Output: Y

Example 2:

Input: 51

Output: N

#include <iostream>

using namespace std;

char su(int x)
{
	char ch = 'Y';
	for (int i = 2;i < x;i++)
	{
		if (x % i == 0)
		{
			ch = 'N';
			break;
		}
		else continue;
	}
	return ch;
}
int main()
{
	int m;
	cin >> m;
	cout << su(m) << endl;
	return 0;
}

When judging prime numbers, i in the for loop must start from 2, because any digital module 1 is 0, which can not meet our requirements for judging prime numbers

Keywords: C++ Back-end

Added by langer on Tue, 30 Nov 2021 13:42:21 +0200