目錄
- 描述
- 輸入
- 輸出
- 樣例輸入
- 樣例輸出
- 思路
- 方法一
- 方法二
- Code
- C
- C++
總時間限制: 1000ms 內存限制: 65536kB
描述
孫老師講授的《計算概論》這門課期中考試剛剛結束,他想知道考試中取得的最高分數。因為人數比較多,他覺得這件事情交給計算機來做比較方便。你能幫孫老師解決這個問題嗎?
輸入
輸入兩行,第一行為整數n(1 <= n < 100),表示參加這次考試的人數.第二行是這n個學生的成績,相鄰兩個數之間用單個空格隔開。所有成績均為0到100之間的整數。
輸出
輸出一個整數,即最高的成績。
樣例輸入
5
85 78 90 99 60
樣例輸出
99
思路
方法一
遍歷所有成績,找出最大值。(不推薦)
方法二
第一個數為最大值,后面的數與最大值比較,如果大于最大值則更新最大值。
Code
C
#include <stdio.h>
#include <math.h>
int main() {int n, a, max = 0;scanf("%d", &n);for(int i = 1; i <= n; i++) {scanf("%d", &a);if(max < a) {max = a;}}printf("%d", max);
}
C++
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {int n, max, num;cin >> n;for(int i = 1; i <= n; i++) {if(i == 1) cin >> max;else {cin >> num;max = num > max? num: max;}}cout << max;
}