NYOJ--811--变态最大值

/*
	Name: NYOJ--811--变态最大值
	Author: shen_渊 
	Date: 17/04/17 15:49
	Description: 看到博客上这道题浏览量最高,原来的代码就看不下去了 o(╯□╰)o 
*/

#include<cstring>
#include<iostream> 
#include<algorithm>
using namespace std;
struct group{
	int first,second,third;
	int max_value;//三个数中的最大值 
	bool operator<(const group &a)const{
		return max_value>a.max_value;
	}
}num[4000]; 
int main()
{
	int n,i,j;
	while(cin>>n){
		memset(num,0,sizeof(num));
		i = n/3;
		for(j=0; j<i; ++j){
			cin>>num[j].first>>num[j].second>>num[j].third;
			if((j+1)%2){
				num[j].max_value = (num[j].max_value = num[j].first>num[j].second?num[i].first:num[j].second)
				>num[j].third?num[j].max_value:num[j].third;
			}else{
				num[j].max_value = (num[j].max_value = num[j].first>num[j].second?num[j].second:num[j].first)
				>num[j].third?num[j].third:num[j].max_value;
			}
		}
		sort(num,num+i);
		cout<<num[0].max_value<<endl;
	}
	return 0;
}
/*
	Name: NYOJ--811--变态最大值 
	Author: shen_渊 
	Date: 17/04/17 16:12
	Description: 整理了一下以前的代码 
*/
#include<stdio.h>
#include<malloc.h>
int smax(int a,int b,int c){
	int max;
	max = (max = a>b?a:b)>c?max:c;
	return max;
}
int smin(int a,int b,int c){
	int max;
	max = (max = a<b?a:b)<c?max:c;
	return max;
}
int main()
{
	int N;
	int n;
	int *a,*b;
	int mark = 0;
	while(scanf("%d",&N) != EOF){
		fflush(stdin);//清空输入缓冲区 
		n = N/3;
		a=(int *)malloc(sizeof(int)*N);
		b=(int *)malloc(sizeof(int)*n);
		int i=0,j=0;
		for(i=0;i<N;i++)scanf("%d",&a[i]);
		i=0;
		while(i<N){
			if(mark%2){
				b[j++]=smin(a[i],a[i+1],a[i+2]);
				mark++;
			}
			else{
				b[j++]=smax(a[i],a[i+1],a[i+2]);
				mark++;
			}
			i += 3;
		}
		int d=b[0];
		for(i=1;i<n;i++)if(d < b[i])d=b[i];
		printf("%d\n",d);
		mark = 0;
	}
	return 0;
}



posted @ 2016-07-30 09:44  朤尧  阅读(262)  评论(0编辑  收藏  举报