变态最大值
Time Limit:1000MS Memory Limit:65536K
Total Submit:27 Accepted:20
Description
Yougth讲课的时候考察了一下求三个数最大值这个问题,没想到大家掌握的这么烂,幸好在他的帮助下大家算是解决了这个问题,但是问题又来了。
他想在一组数中找一个数,这个数可以不是这组数中的最大的,但是要是相对比较大的,但是满足这个条件的数太多了,怎么办呢?他想到了一个办法,把这一组数从开始把每相邻三个数分成一组(组数是从1开始),奇数组的求最大值,偶数组的求最小值,然后找出这些值中的最大值。
Input
有多组测试数据,以文件结束符为标志。
每组测试数据首先一个N,是数组中数的个数。(0 < N < 10000,为降低题目难度,N是3的倍数)
然后是数组中的这些数。
Output
输出包括一行,就是其中的最大值。
Sample Input
3
4 5 6
6
1 2 3 7 9 5
Sample Output
6
5
Source
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AK1167 {
class Program {
static int max(int a, int b, int c) {
int max = a;
if (b > max) max = b;
if (c > max) max = c;
return max;
}
static int min(int a, int b, int c) {
int min = a;
if (b < min) min = b;
if (c < min) min = c;
return min;
}
static void Main(string[] args) {
string sb;
while ((sb = Console.ReadLine()) != null) {
int n = int.Parse(sb);
int[] a = new int[11000];
string[] s = Console.ReadLine().Split();
for (int i = 0; i < n; i++)
a[i] = int.Parse(s[i]);
int x, maxx = -100000000;
for (int i = 0; i < n - 2; i += 3) {
if ((i / 3) % 2 == 0)//奇数组求最大
x = max(a[i], a[i + 1], a[i + 2]);
else//偶数组求最小
x = min(a[i], a[i + 1], a[i + 2]);
if (x > maxx) maxx = x;//找最大
}
Console.WriteLine(maxx);
}
}
}
}