C/C++ Programming Learning-Week 4 ③ Highest score

Topic link

Title description

The mid-term exam of "Introduction to Computing" taught by Mr. Suan Toujun has just ended. He wants to know the highest score obtained in the exam.

Because of the large number of people, he felt it was more convenient to leave this task to the computer. Can you help Teacher Suantoujun solve this problem?

Input format
Enter two lines, the first line is an integer n (1 ≤ n <100), which represents the number of people taking the exam.
The second line is the results of the n students, separated by a single space between two adjacent numbers. All grades are integers between 0 and 100.

Output format
Output an integer, which is the highest score.

Sample Input

5
85 78 90 99 60

Sample Output

99

Ideas

Idea ①: Define a variable to store the highest score. When encountering a variable larger than this variable, perform an assignment operation.
Idea ②: Output the largest number after sorting. Sort from big to small or from small to big (actually the time complexity is higher).

C language code:

#include<stdio.h>
int main()
{
    
    
	int n, maxn = 0;
	scanf("%d", &n);
	while(n--)
	{
    
    
		int a;
		scanf("%d", &a);
		if(a > maxn) maxn = a;
	}
	printf("%d", maxn);
	return 0;
}

C++ code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
	int m, n;
	while(cin >> n)
	{
    
    
		int maxn = 0;
		for(int i = 0; i < n; i++)
		{
    
    
			cin >> m;
			if(m > maxn) maxn = m;
		}
		cout << maxn << endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_44826711/article/details/112907342