Algorithm Design and Analysis Chapter One Algorithm Overview Assignment

Multiple choice

2-1
Which of the following functions is O(N)? (4 points)
A. 2NlogN
B. logN 2
C. N (lie) 2
D. N 2 /2

2-2
Given an N×N×N three-dimensional array A, the time complexity of finding the smallest element without changing the array is: (4 points)

A. O (N 2 )
B. O (nlogn)
C. O (N 3 logN)
D. O(N​3)

2-4
The time complexity of the following code segment is (). (4 points)

x=0;
for( i=1; i<n; i++ )
    for ( j=1; j<=n-i; j++ )
        x++;

A. O(n)
B. O(n​2​​)
C. O(n​3​)
D. O(2​n​​)

2-5
following code

if ( A > B ) {
    for ( i=0; i<N*N/100; i++ )
        for ( j=N*N; j>i; j-- )
            A += B;
}
else {
    for ( i=0; i<N*2; i++ )
        for ( j=N*3; j>i; j-- )
            A += B;
}

The time complexity of is: (4 points)

A. O(N​3)
B. O(N​4)
C. O(N5​)
D. O(N6)

2-6
following code

for(i=0; i<n; i++)
  for(j=i; j>0; j/=2)
     printf(“%d\n”, j);

The time complexity of is: (4 points)

A. O(N×i)
B. O(N)
C. O(N​2)
D. O (NlogN)

The inner loop is divided by 2 each time, only log2 is the bottom, N times;

Programming questions

7-1 Find the maximum value and its subscript (80 points)

This question requires writing a program to find the maximum value of a given n number and its corresponding minimum subscript (subscript starts from 0).

Input format:
Input a positive integer n (1<n≤10) in the first line. Enter n integers on the second line, separated by spaces.

Output format:
output the maximum value and the minimum subscript of the maximum value in one line, separated by a space.

Input sample:

6
2 8 10 1 9 10

Sample output:

10 2
#include<iostream>
using namespace std;

int main()
{
    
    
	int n;
	cin >> n;
	int A[n];
	for(int i=0; i<n; i++){
    
    
		cin >> A[i];
	}
	int max=A[0];
	int max_key = 0;
	for (int j=1; j<n; j++){
    
    
		if(A[j] > max){
    
    
			max = A[j];
			max_key = j;
		}
	} 
	cout << max << " " << max_key;
}

Guess you like

Origin blog.csdn.net/Jessieeeeeee/article/details/111773098