1.3.2 Barn Repair

#include<iostream>
#include<algorithm>
#include<fstream>
using namespace std;

bool Cmp(int a, int b)
{
	return a>b;
}
int main()
{
   ifstream fin("barn1.in");  
   ofstream fout("barn1.out");
   int m, s, c, number[201], length[201], sum;
	fin>>m>>s>>c;
	int i;
	for(i=0; i<c; i++)
		fin>>number[i];

	   
			sort(number, number+c);
	sum=number[c-1]-number[0]+1;//求出第一头牛到最后一头牛的距离

	if( c<=m )  fout<<c<<endl;
	else
	{
        
		for(i=0; i<c-1; i++)//求出第i+1头牛到第i头牛的距离
			length[i]=number[i+1]-number[i]-1;

		sort(length, length+c-1, Cmp);//从大到小排序
		for(i=0; i<c-1; i++)
			cout<<length[i]<<endl;

		for(i=0; i<m-1; i++)//减去m-1块
		{
			sum-=length[i];
			cout<<"sum"<<sum<<endl;
		}
		fout<<sum<<endl;	
	}
}
    
     
	

你可能感兴趣的:(1.3.2 Barn Repair)