uva11078
题意是说,一教授为低年级学生开设的课程,会有已经修过类似课程的高年级学生选修,教授想看一下高年级学生与低年级学生之间的最大考试分差。输入的学生成绩是按照以学生从高年级到低年级排序的,如学生i和学生j(i < j),则学生i的年级要高于学生j(成绩不一定)。注意最大考试分差的计算,不用取绝对值。
题目简单,思路也简单,每一次比较时,在记录最大分差的同时,记录最高分,以便用于下一次比较。最后输出最大分差即可。
不得不说,python的执行效率还是低不少,不知道哪里可以改进一下。。。
python版本AC代码
testcase = int(input())
while testcase > 0:
testcase -= 1
n = int(input())
maxdif = maxv = -float('inf')
for i in range(n):
tmp = int(input())
maxdif = max(maxdif,maxv-tmp)
maxv = max(maxv,tmp)
print(maxdif)
C++版本AC代码
#include <iostream>
#include<cstdio>
using namespace std;
//#define ZANGFONG
const int INF = 0x3f3f3f3f;
int main()
{
#ifdef ZANGFONG
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // ZANGFONG
int testcase,i,n,maxamount;
int maxv,c;
scanf("%d\n",&testcase);
while(testcase--)
{
scanf("%d\n",&n);
maxamount = maxv = -INF;
for(i = 0; i < n; i++)
{
scanf("%d\n",&c);
maxamount = max(maxamount, maxv - c);
maxv = max(maxv,c);
}
printf("%d\n",maxamount);
}
return 0;
}