题目描述
小乐乐的老师BoBo想知道班级中谁的程序设计基础成绩最高,请编程帮他实现。
输入描述:
共n+1行
第一行输入一个数n,代表小乐乐的班级中n个同学。
第二行输入n个数,用空格相隔,代表班级中每个人的数学成绩。
输出描述:
一个整数,代表班级中最高的数学成绩。
第一种想法
根据题目意思
可以定义一个maxn记为最大值
一开始将第一个暂定为最大值
然后读入(n-1)次
然后每一次都比较大小
如果有比maxn大的数字读入
那就将maxn的值更新为当前的这个数字
下面是C++ の代码
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int read()
{
int x=0;
bool f = false;
char ch;
do ch=getchar(),f|=(ch=='-');
while(ch<48||ch>57);
while(ch>47&&ch<58)
x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return f?-x:x;
}//快读
int main()
{
int maxn, i, n = read();
int temp = read();
maxn = temp;
for (i = 1;i < n;i++)
{
temp = read();
if (maxn < temp)
maxn = temp;
}
cout<< maxn;
return 0;
}第二种想法
根据题目意思
将所有的分数先读入到数组里
然后sort排序
经过排序以后为从小到大
(当然也可以从大到小,不过要写一个cmp,比较懒,所以没写)
下面是C++ の代码
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int read()
{
int x=0;
bool f = false;
char ch;
do ch=getchar(),f|=(ch=='-');
while(ch<48||ch>57);
while(ch>47&&ch<58)
x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return f?-x:x;
}//快读
int main()
{
int n = read();
int i, a[n+1];
for (i = 1;i <= n;i++)
a[i] = read();
sort(a+1,a+1+n);
cout << a[n];
return 0;
} 
京公网安备 11010502036488号