您的位置:首页 > 其它

Sicily.1046. Plane Spotting(排序)

2012-12-27 16:26 357 查看
/*1046. Plane Spotting
大意:给出一串时间段内飞过的飞机数量。按照规定顺序排序得出
结果
*/
#include <iostream>
#include <algorithm>
#include <stdlib.h>
using namespace std;
//用系统函数sort,定义比较函数compare,按照指定顺序排序
struct planPeriod{
int start;
int end;
double aver;
};

bool compare(planPeriod a, planPeriod b)
{
if(a.aver > b.aver)
return 1;
if(a.aver == b.aver && a.end - a.start > b.end - b.start)
return 1;
if(a.aver == b.aver && a.end - a.start == b.end - b.start && a.end < b.end)
return 1;
return 0;
}
int main()
{
int N;
cin >> N;
int pN, topN, minN;
planPeriod p[100000];
for(int i=1; i<=N; i++)
{
int plane[300];
cin >> pN >> topN >> minN;
for(int k=0; k<pN; k++)
cin >> plane[k];
int c=0;
for(int sampleN=minN; sampleN <= pN; sampleN++)
{
for(int t =0; t+sampleN <= pN; t++, c++)
{
double sum=0;
for(int k=t; k-t < sampleN; k++)
{
sum += plane[k];
}
p[c].start = t+1;
p[c].end = t+sampleN;
p[c].aver = sum/sampleN;
}
}

sort(p,p+c,compare);

cout << "Result for run "<< i << ":" << endl;
for(int k=0; k<topN && k<c; k++)
cout << p[k].start << "-" << p[k].end << endl;

}
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: