uva11729 贪心算法,工作时常问题

uva11729

算法竞赛入门经典版本

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>

using namespace std; 

struct Job{
	int j,b;
	bool operator < (const Job& x) const { // 重写<运算符 
		return j>x.j;
	}
};
int main(){
	int  n,b,j,kase=0;
	freopen("11729.txt","r",stdin);	
	while(scanf("%d",&n)&&n){
		vector<Job> v;
		for(int i=0;i<n;i++){
			cin>>b>>j;
			v.push_back(Job{j,b});
			}
		sort(v.begin(),v.end());    //sort排序使用<运算符 
		int s=0;
		int ans=0;
		for(int i=0;i<n;i++){
			s+=v[i].b;                // 交代任务的时间  
			ans=max(ans,s+v[i].j);    //  更新执行完任务时最晚的时间。 
		} 
		
		cout<< "Case " <<++kase<<": "<<ans<<"\n";

	}
	
	
	
	return 0;
} 

c++ 结构体变量的使用

struct Job{
	int j,b;
	bool operator < (const Job& x) const { 
		return j>x.j;
	}
};

struct Node{
	bool have_value;
	int v;
	Node *left,*right;
	Node():have_value(false),left(NULL),right(NULL){}   // 构造函数
};

//上面的Node():have_value(false),left(NULL),right(NULL){} 为
Node(){
    this.have_value=false;
    left=NULL;
    right=NULL;
}

posted @ 2020-03-05 10:00  痴痴那  阅读(114)  评论(0)    收藏  举报